Scala Language Functions Composition

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Function composition allows for two functions to operate and be viewed as a single function. Expressed in mathematical terms, given a function f(x) and a function g(x), the function h(x) = f(g(x)).

When a function is compiled, it is compiled to a type related to Function1. Scala provides two methods in the Function1 implementation related to composition: andThen and compose. The compose method fits with the above mathematical definition like so:

val f: B => C = ...
val g: A => B = ...

val h: A => C = f compose g

The andThen (think h(x) = g(f(x))) has a more 'DSL-like' feeling:

val f: A => B = ...
val g: B => C = ...

val h: A => C = f andThen g

A new anonymous function is allocated with that is closed over f and g. This function is bound to the new function h in both cases.

def andThen(g: B => C): A => C = new (A => C){
  def apply(x: A) = g(self(x))
}

If either f or g works via a side-effect, then calling h will cause all side-effects of f and g to happen in the order. The same is true of any mutable state changes.



Got any Scala Language Question?