Skip to content

Instantly share code, notes, and snippets.

@jchapuis
Created November 24, 2020 19:50
Show Gist options
  • Save jchapuis/87e3ca6327592f8307dee685a1579442 to your computer and use it in GitHub Desktop.
Save jchapuis/87e3ca6327592f8307dee685a1579442 to your computer and use it in GitHub Desktop.
Generate all possible combinations of letters in scala lazily
implicit class RichLazyStringList(list: LazyList[String]) {
def productWith(other: LazyList[String]): LazyList[String] =
for {
a <- list
b <- other
} yield a + b
}
LazyList
.from(1)
.map(charCount => LazyList.fill(charCount)(LazyList.from('a' to 'z').map(_.toString)))
.flatMap(_.reduceLeft(_.productWith(_)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment