This was really fun. I want to refine the algorithm to not need to carry each generated level and keep track of where I am, but I didn't have time to fix it right now.
case class Rational(n: Int, d: Int) { override def toString (): String = s"$n/$d" } def nextLevel (s: LazyList[Rational]): LazyList[Rational] = s flatMap { case Rational(n, d) => LazyList( Rational(n, n + d), Rational(n + d, d) ) } def rationals (from: LazyList[Rational] = LazyList(Rational(1, 1)), pos: Int = 0): LazyList[Rational] = { if (pos >= from.size) rationals (nextLevel(from)) else from(pos) #:: rationals (from, pos + 1) }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Scala
This was really fun. I want to refine the algorithm to not need to carry each generated level and keep track of where I am, but I didn't have time to fix it right now.