Skip to content

Instantly share code, notes, and snippets.

View xavierclements's full-sized avatar

Xavier Clements xavierclements

View GitHub Profile
@xavierclements
xavierclements / laziness.scala
Created December 31, 2014 15:28
fpinscala - Chapter 5
package fpinscala.laziness
import Stream._
trait Stream[+A] {
def foldRight[B](z: => B)(f: (A, => B) => B): B = // The arrow `=>` in front of the argument type `B` means that the function `f` takes its second argument by name and may choose not to evaluate it.
this match {
case Cons(h,t) => f(h(), t().foldRight(z)(f)) // If `f` doesn't evaluate its second argument, the recursion never occurs.
case _ => z