Skip to content

Commit

Permalink
change EphemeralStream.foldLeft to use a tail recursive loop
Browse files Browse the repository at this point in the history
(cherry picked from commit d4be530)
  • Loading branch information
purefn authored and xuwei-k committed Apr 24, 2016
1 parent df11861 commit f48255e
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions core/src/main/scala/scalaz/EphemeralStream.scala
Expand Up @@ -36,13 +36,11 @@ sealed abstract class EphemeralStream[A] {
if (isEmpty) z else f(head())(tail().foldRight(z)(f))

def foldLeft[B](z: => B)(f: (=> B) => (=> A) => B): B = {
var t = this
var acc = z
while (!t.isEmpty) {
acc = f(acc)(t.head())
t = t.tail()
}
acc
@annotation.tailrec
def loop(t: EphemeralStream[A], acc: B): B =
if (t.isEmpty) acc
else loop(t.tail(), f(acc)(t.head()))
loop(this, z)
}

def filter(p: A => Boolean): EphemeralStream[A] = {
Expand Down

0 comments on commit f48255e

Please sign in to comment.