How to compose values and functions using common operators
In Object Oriented Programming a common task is to compose objects (values). In Functional Programming it is as common task to compose values as well as functions.
We are used to compose values from our experience of other programming languages
using operators like
/ and so on.
As functional programming composes functions as well as values it's not surprising
there are common operators for function composition like
F# forward piping is preferred over reverse piping because:
- Type inference (generally) flows from left-to-right so it's natural for values and functions to flow left-to-right as well
<<should have right associativity but in
F#they are left associative which forces us to insert ()
- Mixing forward and reverse piping generally don't work because they have the same precedence.
As Monads (like
List<'T>) are commonly used in functional programming
there are also common but less known operators to compose functions working with
To new functional programmers function composition using operators might seem opaque
and obscure but that is because the meaning of these operators aren't as commonly
known as operators working on values. However, with some training using
>=> becomes as natural as using
Latebinding in F# using ? operator
In a statically typed language like
F# we work with types well-known at
compile-time. We consume external data sources in a type-safe manner using type
However, occassionally there's need to use late binding (like
For instance when working with
JSON documents that have no well-defined schema.
To simplify working with late binding
F# provides supports dynamic lookup operators
It turns out that the
F# support for late binding is simple yet flexible.