Skip to content

Instantly share code, notes, and snippets.

@mbesida
Last active February 19, 2021 12:50
Show Gist options
  • Save mbesida/056a74eaa86b7e77ba622839723aa14a to your computer and use it in GitHub Desktop.
Save mbesida/056a74eaa86b7e77ba622839723aa14a to your computer and use it in GitHub Desktop.
LazyList puzzles

Task: Define factorial and fibonacci sequences as LazyLists

Factorials
val factorials: LazyList[BigInt] =
  BigInt(1) #:: factorials.zip(LazyList.from(2).map(BigInt.apply)).map {
    case (a, b) => a * b
  }
Fibs
val fibs: LazyList[BigInt] =
  BigInt(0) #:: BigInt(1) #:: fibs.zip(fibs.tail).map { case (a, b) => a + b }
factorials.take(10).toList // : List[BigInt] = List(1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800)
fibs.take(10).toList // : List[BigInt] = List(0, 1, 1, 2, 3, 5, 8, 13, 21, 34)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment