From a34ef2eebfdef401aedc56582ac943189bfaf0ea Mon Sep 17 00:00:00 2001 From: yaskier Date: Fri, 19 Oct 2018 17:59:06 +0200 Subject: [PATCH] composition --- src/main/scala/stdlib/Composition.scala | 42 +++++++++++++++++++++ src/main/scala/stdlib/StdLib.scala | 1 + src/test/scala/stdlib/CompositionSpec.scala | 33 ++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/main/scala/stdlib/Composition.scala create mode 100644 src/test/scala/stdlib/CompositionSpec.scala diff --git a/src/main/scala/stdlib/Composition.scala b/src/main/scala/stdlib/Composition.scala new file mode 100644 index 00000000..6f5d6429 --- /dev/null +++ b/src/main/scala/stdlib/Composition.scala @@ -0,0 +1,42 @@ +/* + * scala-exercises - exercises-stdlib + * Copyright (C) 2015-2016 47 Degrees, LLC. + */ + +package stdlib + +import org.scalatest._ + +/** @param name composition + * + */ +object Composition + extends FlatSpec + with Matchers + with org.scalaexercises.definitions.Section { + + /** Function `composition` is a functional programming and mathematical transformation that through the combination of two existing functions generates a new function. Mathematically speaking we refer at composition as: `g(x) O f(x) = f(g(x))` + * Scala provides a method called compose that can be used this way: + */ + def composeFunctions(res0: String) { + def f(s: String) = "f(" + s + ")" + def g(s: String) = "g(" + s + ")" + + val composedFunction = f _ compose g _ //Here we compose the functions + + composedFunction("Scala Exercises") should be(res0) + } + + /** + * There is also a method andThen, closely related to compose. + * The only difference between andThen and compose is that the order of evaluation for compose is right to left. + */ + def andThenFunctions(res0: String) { + def f(s: String) = "f(" + s + ")" + def g(s: String) = "g(" + s + ")" + + val composedFunction = g _ andThen f _ + + composedFunction("Scala Exercises") should be(res0) + } +} diff --git a/src/main/scala/stdlib/StdLib.scala b/src/main/scala/stdlib/StdLib.scala index 8ae45e8d..df75dfcc 100644 --- a/src/main/scala/stdlib/StdLib.scala +++ b/src/main/scala/stdlib/StdLib.scala @@ -29,6 +29,7 @@ object StdLib extends org.scalaexercises.definitions.Library { PatternMatching, CaseClasses, Ranges, + Composition, PartiallyAppliedFunctions, PartialFunctions, Implicits, diff --git a/src/test/scala/stdlib/CompositionSpec.scala b/src/test/scala/stdlib/CompositionSpec.scala new file mode 100644 index 00000000..8f61aafc --- /dev/null +++ b/src/test/scala/stdlib/CompositionSpec.scala @@ -0,0 +1,33 @@ +/* + * scala-exercises - exercises-stdlib + * Copyright (C) 2015-2016 47 Degrees, LLC. + */ + +package stdlib + +import org.scalacheck.Shapeless._ +import org.scalaexercises.Test +import org.scalatest.Spec +import org.scalatest.prop.Checkers +import shapeless.HNil + +class CompositionSpec extends Spec with Checkers { + def `compose functions` = { + check( + Test.testSuccess( + Composition.composeFunctions _, + "f(g(Scala Exercises))" :: HNil + ) + ) + } + + def `andthen functions` = { + check( + Test.testSuccess( + Composition.andThenFunctions _, + "f(g(Scala Exercises))" :: HNil + ) + ) + } + +}