Composition in F#
>> glues functions into new functions, no argument named.
// >> glues functions into new functions, no argument in sight.
let gramsOfKg kg = kg * 1000.0
let jars grams = grams / 340.0
// The composed pipeline is itself a value.
let jarsOfKg = gramsOfKg >> jars >> floor
printfn "Jars from 12.4 kg: %.0f" (jarsOfKg 12.4)
printfn "Jars from 30.0 kg: %.0f" (jarsOfKg 30.0)
// Composition and piping meet: build once, apply anywhere.
let describe = jarsOfKg >> sprintf "yields %.0f jars"
[8.5; 15.3] |> List.map describe |> List.iter (printfn "%s")
How it works
gramsOfKg >> jars >> flooris a function value.- Composed pipelines apply anywhere a function fits.
List.map describereuses the composition across a list.
Keywords and builtins used here
let
The run, in numbers
- Lines
- 13
- Characters to type
- 489
- Tokens
- 87
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 80 seconds.
Step 3 of 3 in Functions, step 6 of 27 in Language basics.