The pipeline in F#
|> feeds the left value forward; data reads in flow order.
// |> feeds the left value to the right function: data flows forward.
let weightsKg = [28.4; 31.0; 19.7; 35.2; 22.1]
let heavyCount =
weightsKg
|> List.filter (fun w -> w > 25.0)
|> List.length
printfn "Hives ready for winter: %d" heavyCount
// Without the pipe the same code reads inside out.
let total = List.sum (List.map (fun w -> w * 1000.0) weightsKg)
printfn "Total grams the hard way: %.0f" total
weightsKg |> List.max |> printfn "Heaviest: %.1f kg"
How it works
weightsKg |> List.filter ... |> List.lengthreads as a sentence.- The pipeless version nests inside out — same code, harder read.
- Pipelines end in any function:
|> printfncloses the loop.
Keywords and builtins used here
funlet
The run, in numbers
- Lines
- 14
- Characters to type
- 460
- Tokens
- 94
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 87 seconds.
Step 2 of 3 in Functions, step 5 of 27 in Language basics.