Skip to content

composition #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
42 changes: 42 additions & 0 deletions src/main/scala/stdlib/Composition.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* scala-exercises - exercises-stdlib
* Copyright (C) 2015-2016 47 Degrees, LLC. <http://www.47deg.com>
*/

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)
}
}
1 change: 1 addition & 0 deletions src/main/scala/stdlib/StdLib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ object StdLib extends org.scalaexercises.definitions.Library {
PatternMatching,
CaseClasses,
Ranges,
Composition,
PartiallyAppliedFunctions,
PartialFunctions,
Implicits,
Expand Down
33 changes: 33 additions & 0 deletions src/test/scala/stdlib/CompositionSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* scala-exercises - exercises-stdlib
* Copyright (C) 2015-2016 47 Degrees, LLC. <http://www.47deg.com>
*/

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
)
)
}

}