Last active
June 28, 2017 07:54
-
-
Save kastoestoramadus/00a6b6add0285c09d622bf1b9a185d92 to your computer and use it in GitHub Desktop.
WhyFoldRight works here
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
def takeWhileR[A](s: Stream[A], f: A => Boolean): Stream[A] = | |
s.foldRight(Stream[A]())((h, t) => | |
if (f(h)) Stream(h) #::: t | |
else Stream.apply()) | |
def takeWhileL[A](s: Stream[A], f: A => Boolean): Stream[A] = | |
s.foldLeft(Stream[A]())((t, h) => | |
if (f(h)) Stream(h) #::: t | |
else Stream.apply()) | |
takeWhileR[Int](Stream(1,2,3,4), _ < 3).toList | |
takeWhileL[Int](Stream(1,2,3,4), _ < 3).toList | |
// I know why, and you? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment