diff --git a/src/main/scala/stdlib/PartiallyAppliedFunctions.scala b/src/main/scala/stdlib/PartiallyAppliedFunctions.scala index 7fba07cf..9615a696 100644 --- a/src/main/scala/stdlib/PartiallyAppliedFunctions.scala +++ b/src/main/scala/stdlib/PartiallyAppliedFunctions.scala @@ -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]) { diff --git a/src/test/scala/stdlib/PartiallyAppliedFunctionsSpec.scala b/src/test/scala/stdlib/PartiallyAppliedFunctionsSpec.scala index 6cb3664b..b6287305 100644 --- a/src/test/scala/stdlib/PartiallyAppliedFunctionsSpec.scala +++ b/src/test/scala/stdlib/PartiallyAppliedFunctionsSpec.scala @@ -47,4 +47,13 @@ class PartiallyAppliedFunctionsSpec extends Spec with Checkers { ) ) } + + def `composing` = { + check( + Test.testSuccess( + PartiallyAppliedFunctions.curriedForComposition _, + 7 :: 9 :: HNil + ) + ) + } }