| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Overture
Contents
Description
- identity :: a -> a
- always :: a -> b -> a
- compose :: (a -> b) -> (b -> c) -> a -> c
- (.>) :: (a -> b) -> (b -> c) -> a -> c
- (<.) :: (b -> c) -> (a -> b) -> a -> c
- apply :: a -> (a -> b) -> b
- (|>) :: a -> (a -> b) -> b
- (<|) :: (a -> b) -> a -> b
- apply' :: a -> (a -> b) -> b
- (!>) :: a -> (a -> b) -> b
- (<!) :: (a -> b) -> a -> b
Identity function
Use this function to always return the value it was given.
>>>identity TrueTrue
This is useful when constructing a function that returns another function.
>>>let f x = if even x then recip else identity>>>f 2 100.1>>>f 3 1010.0
Constant function
Use this function to always return some value, regardless of what the other argument is.
>>>always True FalseTrue
This can be useful with higher-order functions. For example, this creates
a list of three Trues.
>>>map (always True) [1 .. 3][True,True,True]
Function composition
compose :: (a -> b) -> (b -> c) -> a -> c Source
Use this function to combine two other functions. The result of
will be a new function that first applies compose f gf and then
applies g. In other words, is the same as compose f g xg (f x).
For instance, the following example will first add one and then multiply
by two.
>>>let f = compose (+ 1) (* 2)>>>f 38
You can compose many functions together, but it quickly becomes unwieldy. This example does what the previous one did and then cubes the result.
>>>let g = compose (compose (+ 1) (* 2)) (^ 3)>>>g 3512
This is like the function form of the . operator from the Prelude.
(.>) :: (a -> b) -> (b -> c) -> a -> c infixl 9 Source
Left-associative compose operator.
Use this operator to combine two other functions in a more natural way
than with compose. The result of f will be a new function that
first applies .> gf and then applies g. Here is the same example from
above rewritten using this operator.
>>>let f = (+ 1) .> (* 2)>>>f 38
When reading code, it is useful to pronounce this operator as "and then". So the above example could be read as: Add one, and then multiply by two".
When composing many functions, it's easier to use this operator than
compose. Compare this with the earlier example.
>>>let g = (+ 1) .> (* 2) .> (^ 3)>>>g 3512
This is like a flipped version of . operator from the Prelude.
(<.) :: (b -> c) -> (a -> b) -> a -> c infixr 9 Source
Right-associative compose operator.
Sometimes it is more convenient to combine functions in the opposite
direction as .>. The result of g will be a new function that
applies <. fg but first applies f. Here is the same example from before
rewritten again.
>>>let f = (* 2) <. (+ 1)>>>f 38
When reading code, it is useful to pronounce this operator as "but first". So the above example could be read as: "Multiply by two, but first add one".
Using this operator also leads to more readable code than using compose
with many functions.
>>>let g = (^ 3) <. (* 2) <. (+ 1)>>>g 3512
Function application
apply :: a -> (a -> b) -> b Source
This function isn't usually necessary since is the same as
apply x ff x.
>>>apply (apply 3 (+ 1)) (* 2)8
However it can come in handy when working with higher-order functions.
>>>map (apply 3) [(+ 1), (* 2)][4,6]
(|>) :: a -> (a -> b) -> b infixl 0 Source
Left-associative apply operator.
Since this operator has such low precedence, it can be used to remove parentheses in complicated expressions.
>>>3 |> (+ 1) |> (* 2)8
When reading code, it is useful to pronounce this operator as "pipe into". So the above example can be read as: "Three piped into plus one, piped into times two".
It can also be used with higher-order functions, although apply might be
clearer.
>>>map (3 |>) [(+ 1), (* 2)][4,6]
This is like a flipped version of the $ operator from the Prelude.
(<|) :: (a -> b) -> a -> b infixr 0 Source
Right-associative apply operator.
Like |>, this operator also has low precedence. Use it to remove
parentheses.
>>>(* 2) <| (+ 1) <| 38
When reading code, it is useful to pronounce this operator as "pipe from". So the above example can be read as: "Times two piped from plus one, piped from 3".
With higher-order functions, it can be a convenient alternative to
flip .apply
>>>map (<| 3) [(+ 1), (* 2)][4,6]