Permalink
Browse files

Implement curry

  • Loading branch information...
rboles committed Feb 26, 2014
1 parent 3fbe819 commit 85a1ef37c2bd6fb0da4c602793bca76f1096f71f
Showing with 22 additions and 4 deletions.
  1. +22 −4 exercises/src/main/scala/fpinscala/gettingstarted/GettingStarted.scala
@@ -172,14 +172,32 @@ object PolymorphicFunctions {
// Note that `=>` associates to the right, so we could
// write the return type as `A => B => C`
- def curry[A,B,C](f: (A, B) => C): A => (B => C) =
- ???
+ def curry[A,B,C](f: (A, B) => C): A => (B => C) = {
+ def g(a: A): B => C = {
+ def h(b: B): C = {
+ f(a, b)
+ }
+ h
+ }
+ g
+ }
+
+ // A compact curry with a function literal
+ def curryCompact1[A, B, C](f: (A, B) => C): A => (B => C) = {
+ def g(a: A): B => C = {
+ (b: B) => f(a, b)
+ }
+ g
+ }
+
+ // Super compact'ed with 2 funciton literals
+ def currySuperCompact[A, B, C](f: (A, B) => C): A => (B => C) =
+ (a: A) => (b: B) => f(a, b)
// NB: The `Function2` trait has a `curried` method already
// Exercise 5: Implement `uncurry`
- def uncurry[A,B,C](f: A => B => C): (A, B) => C =
- ???
+ def uncurry[A,B,C](f: A => B => C): (A, B) => C = ???
/*
NB: There is a method on the `Function` object in the standard library,

0 comments on commit 85a1ef3

Please sign in to comment.