F# pipelines and composition
last modified December 2, 2026
In this article we chain F# functions with the pipe operators and combine them into new functions with the composition operators.
A program often passes a value through several functions, each working on the result of the previous one. Written as nested calls, the steps read from the inside out. A pipeline writes them in the order in which they happen. The pipe and composition operators are ordinary F# operators defined in the core library; they rely on the partial application of functions.
The pipe operator
The forward pipe operator |> passes the value on its left to
the function on its right: x |> f is the same as
f x.
let square x = x * x let increment x = x + 1 let a = 4 |> square let b = 4 |> square |> increment let c = 4 |> increment |> square printfn "a = %d, b = %d, c = %d" a b c 7 |> square |> printfn "7 squared is %d"
We pipe a number through two small functions in different orders.
let b = 4 |> square |> increment let c = 4 |> increment |> square
The pipe is left-associative, so the steps run from left to right:
b is the square of 4 plus one, and c is the square
of 5.
7 |> square |> printfn "7 squared is %d"
The last step of a pipeline is often a function that prints the result.
printfn "7 squared is %d" is a partially applied function that
waits for the integer.
λ dotnet fsi pipe.fsx a = 16, b = 17, c = 25 7 squared is 49
Pipelines instead of nested calls
A pipeline is easier to read than nested function calls when there are several steps.
let square x = x * x
let increment x = x + 1
let negate x = -x
let half x = x / 2
let nested = half (increment (negate (square 5)))
let piped =
5
|> square
|> negate
|> increment
|> half
printfn "nested: %d" nested
printfn "piped: %d" piped
We compute the same value with nested calls and with a pipeline.
let nested = half (increment (negate (square 5)))
The nested calls are read from the innermost one outward, and the parentheses must be counted.
let piped =
5
|> square
|> negate
|> increment
|> half
The pipeline lists the steps in the order they run. Putting each step on its own line is the usual layout; it also makes it easy to add, remove or reorder steps.
λ dotnet fsi nested.fsx nested: -12 piped: -12
Pipes and partial application
A function with several parameters can take part in a pipeline when all but the last argument are given. The piped value becomes the last argument.
let add x y = x + y
let multiply x y = x * y
let subtractFrom x y = x - y
let clamp low high x = max low (min high x)
let result =
10
|> add 5
|> multiply 3
|> subtractFrom 100
printfn "result: %d" result
for n in [-20; 35; 140] do
n |> multiply 2 |> clamp 0 100 |> printfn "%d -> %d" n
let title =
" the old man and the sea "
|> fun s -> s.Trim()
|> fun s -> s.ToUpper()
|> fun s -> s.Replace(" ", "_")
printfn "%s" title
We use functions with two and three parameters in pipelines, and lambdas for method calls.
let result =
10
|> add 5
|> multiply 3
|> subtractFrom 100
add 5 is a function waiting for one more number. The last step
computes subtractFrom 100 45, that is 100 - 45.
The piped value always goes last, which is why F# functions take the data
they work on as their last parameter.
n |> multiply 2 |> clamp 0 100 |> printfn "%d -> %d" n
The format string has two placeholders; the first is filled by
n, the second by the piped value.
|> fun s -> s.Trim() |> fun s -> s.ToUpper()
.NET methods are not curried functions, so we wrap them in
lambdas. The type of s is known
from the piped value, so no annotation is needed. This is another advantage of
pipes: type inference works from left to right, and the value comes first.
λ dotnet fsi partial.fsx result: 55 -20 -> 0 35 -> 70 140 -> 100 THE_OLD_MAN_AND_THE_SEA
Pipelines with lists
Pipelines are most common with collections. The functions of the
List module take the list as their last parameter, so they fit
into pipelines.
let numbers = [1..10]
let evens = numbers |> List.filter (fun n -> n % 2 = 0)
printfn "%A" evens
let total =
numbers
|> List.filter (fun n -> n % 2 = 0)
|> List.map (fun n -> n * n)
|> List.sum
printfn "sum of even squares: %d" total
let words = ["falcon"; "owl"; "sparrow"; "eagle"; "tit"]
words
|> List.filter (fun w -> w.Length > 3)
|> List.map (fun w -> w.ToUpper())
|> List.sortDescending
|> List.iter (printfn "%s")
We filter, transform and sum a list of numbers, and process a list of words.
let total =
numbers
|> List.filter (fun n -> n % 2 = 0)
|> List.map (fun n -> n * n)
|> List.sum
List.filter keeps the elements for which the function returns
true, List.map applies a function to each element,
and List.sum adds them up. The pipeline computes the sum of the
squares of the even numbers.
|> List.sortDescending |> List.iter (printfn "%s")
List.iter calls a function for each element; here it prints the
words, one per line. The List module is covered in
F# list.
λ dotnet fsi lists.fsx [2; 4; 6; 8; 10] sum of even squares: 220 SPARROW FALCON EAGLE
The backward pipe
The backward pipe operator <| passes the value on its right
to the function on its left: f <| x is f x. It
has a low precedence, so it is used to avoid parentheses around the last
argument.
let square x = x * x let increment x = x + 1 printfn "%d" (square 4) printfn "%d" <| square 4 let a = increment (square 3) let b = increment <| square 3 printfn "a = %d, b = %d" a b printfn "%s" <| "falcon".ToUpper() [1..5] |> List.map square |> List.sum |> printfn "sum of squares: %d" printfn "sum of squares: %d" <| List.sum (List.map square [1..5])
Each pair of lines computes the same value with parentheses and with the backward pipe.
printfn "%d" (square 4) printfn "%d" <| square 4
Everything to the right of <| is evaluated first and then
passed to printfn "%d".
printfn "sum of squares: %d" <| List.sum (List.map square [1..5])
The backward pipe helps in short expressions. For longer chains, the forward pipeline above it is easier to read.
λ dotnet fsi backward.fsx 16 16 a = 10, b = 10 FALCON sum of squares: 55 sum of squares: 55
Function composition
A pipe applies functions to a value. The composition operator
>> combines two functions into a new function, without a
value: f >> g is a function that applies f
and then g. The << operator composes in the
opposite direction: f << g applies g first.
let square x = x * x
let increment x = x + 1
let negate x = -x
let squareThenIncrement = square >> increment
let incrementThenSquare = square << increment
printfn "%d" (squareThenIncrement 5)
printfn "%d" (incrementThenSquare 5)
let pipeline = square >> negate >> increment
printfn "%d" (pipeline 3)
let trim (s: string) = s.Trim()
let upper (s: string) = s.ToUpper()
let exclaim (s: string) = s + "!"
let shout = trim >> upper >> exclaim
printfn "%s" (shout " hello there ")
printfn "%s" (" we are leaving " |> shout)
We build new functions from small ones.
let squareThenIncrement = square >> increment let incrementThenSquare = square << increment
The first function computes 5 * 5 + 1, the second (5 + 1) * (5 + 1).
let shout = trim >> upper >> exclaim
A chain of compositions builds a new function step by step, like a pipeline that is stored for later use. The composed function can itself be used in a pipeline.
λ dotnet fsi composition.fsx 26 36 -8 HELLO THERE! WE ARE LEAVING!
Point-free style
A function defined by composition does not name its argument. This is called point-free style; the "point" is the argument.
let square x = x * x
let sumOfSquaresPiped xs =
xs |> List.map square |> List.sum
let sumOfSquares = List.map square >> List.sum
printfn "%d" (sumOfSquaresPiped [1; 2; 3])
printfn "%d" (sumOfSquares [1; 2; 3])
let countLong = List.filter (fun (w: string) -> w.Length > 4) >> List.length
let longest = List.maxBy (fun (w: string) -> w.Length)
let words = ["sky"; "cloud"; "thunder"; "rain"; "lightning"]
printfn "long words: %d" (countLong words)
printfn "longest: %s" (longest words)
let isEven n = n % 2 = 0
let isOdd = isEven >> not
printfn "%b %b" (isOdd 7) (isOdd 10)
We define the same function with a pipeline and in point-free style, and a few more point-free functions.
let sumOfSquaresPiped xs =
xs |> List.map square |> List.sum
let sumOfSquares = List.map square >> List.sum
The two definitions are equivalent. The second composes the partially
applied List.map square with List.sum.
let isEven n = n % 2 = 0 let isOdd = isEven >> not
Composing with not negates a predicate. Point-free style is
concise for short chains; when a definition becomes hard to follow, naming the
argument and using a pipeline is clearer.
λ dotnet fsi point_free.fsx 14 14 long words: 3 longest: lightning true false
Defining an operator
The pipe and composition operators are defined in F# itself, and we can define our own operators the same way. An operator is a function whose name is a sequence of symbol characters, put in parentheses in the definition.
let (%%) a b = ((a % b) + b) % b
printfn "-17 %% 5 = %d" (-17 % 5)
printfn "-17 %%%% 5 = %d" (-17 %% 5)
let (|>!) x f =
printfn "value: %A" x
f x
let square x = x * x
let increment x = x + 1
let result =
3
|>! square
|>! increment
|>! square
printfn "result: %d" result
let hours = 25 %% 24
let minutes = -15 %% 60
printfn "%d:%02d" hours minutes
We define a remainder operator that never returns a negative number, and a pipe that prints each value it passes on.
let (%%) a b = ((a % b) + b) % b
The built-in % returns -2 for -17 % 5.
Our %% operator returns 3, which is what we want
for clock arithmetic and for indexes that wrap around. In the format string,
each % must be doubled.
let (|>!) x f =
printfn "value: %A" x
f x
This operator works like |>, but it prints the value before
passing it on. Inserted into a pipeline, it shows the intermediate results,
which helps when debugging.
λ dotnet fsi operator.fsx -17 % 5 = -2 -17 %% 5 = 3 value: 3 value: 9 value: 10 result: 100 1:45
Source
F# language reference: Function composition and pipelining
F# language reference: Operator overloading
In this article we have chained functions with the forward and backward
pipes, composed them with >> and <<,
written point-free functions and defined our own operators. Functions and
partial application are covered in F#
functions, anonymous functions in F#
lambda, and the built-in operators in F#
operators.
Author
List all F# tutorials.