-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
56 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package usage | ||
|
||
/* | ||
Original artical for this: http://www.codecommit.com/blog/scala/function-currying-in-scala | ||
It turns out that the best rationale for using currying has to do with general and specialized functions. | ||
It would be nice to define a function for the general case, | ||
but allow users to specialize the function and then used the specialized version on different data sets. | ||
Take the following code as an exampl | ||
*/ | ||
|
||
class Curry { | ||
def process[A](filter: A => Boolean)(list: List[A]): List[A] = { | ||
lazy val recurse = process(filter) _ //the underscore just tells the compiler to treat the suffixed value as a functional, rather than a method to be evaluated. | ||
|
||
list match { | ||
case head :: tail => if (filter(head)) { | ||
head :: recurse(tail) | ||
} else { | ||
recurse(tail) | ||
} | ||
|
||
case Nil => Nil | ||
} | ||
} | ||
|
||
val even = (a: Int) => a % 2 == 0 | ||
val processEvens = process(even) _ | ||
|
||
val numbersAsc = 1 :: 2 :: 3 :: 4 :: 5 :: Nil | ||
val numbersDesc = 5 :: 4 :: 3 :: 2 :: 1 :: Nil | ||
|
||
processEvens(numbersAsc) // [2, 4] | ||
processEvens(numbersDesc) // [4, 2] | ||
|
||
} |
This file contains 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