Skip to content

Instantly share code, notes, and snippets.

@kastoestoramadus
Last active June 28, 2017 07:54
Show Gist options
  • Save kastoestoramadus/00a6b6add0285c09d622bf1b9a185d92 to your computer and use it in GitHub Desktop.
Save kastoestoramadus/00a6b6add0285c09d622bf1b9a185d92 to your computer and use it in GitHub Desktop.
WhyFoldRight works here
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