Created
May 4, 2015 19:18
-
-
Save Kornel/810bf42e97f01b93fe8d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.kornel.collatz | |
object CollatzScala extends App { | |
import StreamTakeWhileInclusive.StreamTWI | |
val start = 15 | |
val collatz = Stream.iterate(15) { | |
case n if n % 2 == 0 => n / 2 | |
case n => 3 * n + 1 | |
} | |
collatz | |
.takeWhileInc(_ != 1) | |
.foreach(println) | |
} | |
object StreamTakeWhileInclusive { | |
implicit class StreamTWI[A](s: Stream[A]) { | |
def takeWhileInc(p: A => Boolean) = { | |
val (left, right) = s.span(p) | |
left :+ right.head | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment