Generic function for multiple generators in for comprehensions in Scala

michal

Let's say I want to create all possible combinations of letters "a" and "b". For combinations of length 2 using for-comprehensions it will be:

for {
   x <- Seq("a", "b") 
   y <- Seq("a", "b")
} yield x + y

And for combinations of length 3 it will be:

for {
   x <- Seq("a", "b") 
   y <- Seq("a", "b")
   z <- Seq("a", "b")
} yield x + y + z

Pretty similar. Is this possible to abstract this pattern and write generic function? I can think of such signature:

def genericCombine[A,B](length: Int, elements: Seq[A])(reducer: Seq[A] => B): Seq[B]

How can I parametrize number of generators used in for comprehension?

Travis Brown

This is more like permutations with replacement than combinations, and a recursive implementation is fairly straightforward:

def select(n: Int)(input: List[String]): List[String] =
  if (n == 1) input else for {
    c <- input
    s <- select(n - 1)(input)
  } yield c + s

Which works as expected:

scala> select(2)(List("a", "b"))
res0: List[String] = List(aa, ab, ba, bb)

scala> select(3)(List("a", "b"))
res1: List[String] = List(aaa, aab, aba, abb, baa, bab, bba, bbb)

(You should of course check for invalid input in a real application.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Multiple for comprehensions within a map in Scala

From Dev

Scala For Comprehensions and For Loops

From Dev

Scala For Comprehensions and For Loops

From Dev

Scala cast to generic type (for generic numerical function)

From Dev

Scala Generic function assuming type

From Dev

Writing a generic function of TraversableOnce in Scala?

From Dev

Odd issue with generic function in Scala

From Dev

scala generic function `not found: type ?`

From Dev

Scala Generic function assuming type

From Dev

Writing a generic function of TraversableOnce in Scala?

From Dev

to build a generic cast function in scala

From Dev

Merge multiple list comprehensions

From Dev

Comprehensions with multiple input sets

From Dev

Condensing Multiple List Comprehensions

From Dev

Merge multiple list comprehensions

From Dev

List comprehensions with multiple conditions

From Dev

Scala Generic Function... using T in Function

From Dev

Evaluate String as a generic function or function call in Scala

From Dev

Scala Yield generators

From Dev

Generic function with multiple closures not working

From Dev

Generic function with multiple closures not working

From Dev

Combining Scala Futures and collections in for comprehensions

From Dev

How to inherit generic trait multiple times in Scala?

From Java

Why do list comprehensions write to the loop variable, but generators don't?

From Dev

How to implement that generic function with TypeTag in Scala?

From Dev

Scala: generic function with nested case classes

From Dev

How to get the actual type of a generic function in Scala?

From Dev

Scala: having a function with generic return type

From Dev

Scala pattern matching error in generic function

Related Related

HotTag

Archive