Created
May 27, 2016 08:24
-
-
Save edofic/e0bca60725ea1dab32c70061a353b1ff to your computer and use it in GitHub Desktop.
Structural function composition in Scala
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
scala> val f = (_: Int) + 1 | |
f: Int => Int = <function1> | |
scala> val g = (_: Int).toString | |
g: Int => String = <function1> | |
scala> case class Compose[a,b,c](f: a => b, g: b => c) extends Function[a,c] { | |
| def apply(a: a): c = g(f(a)) | |
| } | |
defined class Compose | |
scala> val h = Compose(f,g) | |
h: Compose[Int,Int,String] = <function1> | |
scala> h(2) | |
res0: String = 3 | |
scala> val Compose(x,y) = h | |
x: Int => Int = <function1> | |
y: Int => String = <function1> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment