typestar

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

  1. weightsKg |> List.filter ... |> List.length reads as a sentence.
  2. The pipeless version nests inside out — same code, harder read.
  3. Pipelines end in any function: |> printfn closes the loop.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Functions, step 5 of 27 in Language basics.

← Previous Next →

The pipeline in other languages