Wednesday, December 13, 2017

Defining backticks using "pipe" operations

(Thanks to Simon Fowler and others on the Links team for the discussion leading to this...)

In Haskell, you can use a 2-argument function as if it were an infix operator by putting "backticks" around it like this:

ghci> let plus x y = x + y
ghci> 1 `plus` 2

In a number of functional languages such as F# and OCaml, the following "pipe" operations have become popular (I'll continue using Haskell syntax, though):

ghci> let (|>) x f = f x
ghci> let (<|) f x = f x
ghci> [1,2,3] |> map (plus 1)

Read more »

Labels: ,