Skip to content

Instantly share code, notes, and snippets.

@InvisibleTech
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save InvisibleTech/fb7814a50b5f1de38aa7 to your computer and use it in GitHub Desktop.
Save InvisibleTech/fb7814a50b5f1de38aa7 to your computer and use it in GitHub Desktop.
Showing code from Functional Programming in Scala Book Code
/*
Taken from https://github.com/fpinscala/fpinscala/blob/master/answers/src/main/scala/fpinscala/datastructures/List.scala#L159
This code is not mine - I only posted it here to share it. It is part of the answer key solutions from http://www.manning.com/bjarnason/
Paul Chiusano is one of the authors and has posted on topics such as Scala's limited type inferecing:
http://pchiusano.blogspot.com/2011/05/making-most-of-scalas-extremely-limited.html
*/
@annotation.tailrec
def foldLeft[A,B](l: List[A], z: B)(f: (B, A) => B): B = l match {
case Nil => z
case Cons(h,t) => foldLeft(t, f(z,h))(f)
}
def foldRightViaFoldLeft_NoReverse[A,B](ls: List[A], z: B)(f: (A,B) => B): B =
foldLeft(ls, (b:B) => b) ((g,a) => b => g(f(a,b))) (z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment