Created
April 25, 2015 20:53
-
-
Save aphexmunky/e5aed266f6a868f12988 to your computer and use it in GitHub Desktop.
Functional Programming in Scala chapter 2
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
| def fib(n: Int): Int = { | |
| def go(first: Int, second: Int, iteration: Int): Int = iteration match { | |
| case _ if iteration == n => first + second | |
| case _ => go(second, first + second, iteration + 1) | |
| } | |
| go(0, 1, 1) | |
| } |
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
| def isSorted[A](as: Array[A], ordered: (A,A) => Boolean): Boolean = { | |
| if (as.length > 1) { | |
| ordered(as.head, as.tail.head) && isSorted(as.tail, ordered) | |
| } else { | |
| true | |
| } | |
| } |
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
| def curry[A,B,C](f: (A, B) => C): A => (B => C) = a => b => f(a,b) |
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
| def uncurry[A,B,C](f: A => B => C): (A, B) => C = (a,b) => f(a)(b) |
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
| def compose[A,B,C](f: B => C, g: A => B): A => C = x => f(g(x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment