Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/scala/stdlib/PartiallyAppliedFunctions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ object PartiallyAppliedFunctions
multiplyCurriedFour(4) should be(res4)
}

/**
* Currying also allows you to compose functions that have multiple parameter, since normally it's not possible:
*/
def curriedForComposition(res0: Int, res1: Int) {
def multiply(x: Int, y: Int) = x * y
val multiplyCurried = (multiply _).curried
def addOne(x: Int) = x + 1

val composedFunction = addOne _ compose multiplyCurried(2)
composedFunction(3) should be(res0)

val andThenComposedFunction = multiplyCurried(2) andThen addOne
andThenComposedFunction(4) should be(res1)
}

/** Currying allows you to create specialized versions of generalized functions:
*/
def specializedVersionPartiallyAppliedFunctions(res0: List[Int], res1: List[Int]) {
Expand Down
9 changes: 9 additions & 0 deletions src/test/scala/stdlib/PartiallyAppliedFunctionsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ class PartiallyAppliedFunctionsSpec extends Spec with Checkers {
)
)
}

def `composing` = {
check(
Test.testSuccess(
PartiallyAppliedFunctions.curriedForComposition _,
7 :: 9 :: HNil
)
)
}
}