Skip to content

Instantly share code, notes, and snippets.

@aphexmunky
Created April 25, 2015 20:53
Show Gist options
  • Select an option

  • Save aphexmunky/e5aed266f6a868f12988 to your computer and use it in GitHub Desktop.

Select an option

Save aphexmunky/e5aed266f6a868f12988 to your computer and use it in GitHub Desktop.
Functional Programming in Scala chapter 2
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)
}
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
}
}
def curry[A,B,C](f: (A, B) => C): A => (B => C) = a => b => f(a,b)
def uncurry[A,B,C](f: A => B => C): (A, B) => C = (a,b) => f(a)(b)
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