Skip to content

Instantly share code, notes, and snippets.

@edofic
Created May 27, 2016 08:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edofic/e0bca60725ea1dab32c70061a353b1ff to your computer and use it in GitHub Desktop.
Save edofic/e0bca60725ea1dab32c70061a353b1ff to your computer and use it in GitHub Desktop.
Structural function composition in Scala
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