Skip to content
The Pipeline Operator
step 1/5

Reading — step 1 of 5

Learn

~1 min readFunctions

F#'s killer feature is the pipeline operator |>. It feeds a value into a function as the LAST argument:

5 |> square    // same as: square 5

Why? Because chained pipelines read top-to-bottom, left-to-right:

[1; 2; 3; 4; 5]
|> List.filter (fun x -> x % 2 = 0)
|> List.map (fun x -> x * x)
|> List.sum
// 20

Without pipelines:

List.sum (List.map (fun x -> x * x) (List.filter (fun x -> x % 2 = 0) [1; 2; 3; 4; 5]))

The pipeline reads as a recipe; the nested form reads inside-out.

Reverse pipe <| exists for readability when the function is long:

printfn "%A" <| List.sort xs

Function composition >>:

let doubleThenSquare = (*) 2 >> square
doubleThenSquare 3       // (3 * 2)² = 36

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…