Last active
August 29, 2015 14:27
-
-
Save InvisibleTech/fb7814a50b5f1de38aa7 to your computer and use it in GitHub Desktop.
Showing code from Functional Programming in Scala Book Code
This file contains hidden or 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
/* | |
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