| Safe Haskell | Safe |
|---|---|
| Language | Haskell98 |
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
Identity function.
This is like the id function from the Prelude.
This function returns the value it was given. The result of
is identity xx.
>>>identity TrueTrue
This is useful when constructing functions that return other functions.
>>>let f x = if x then recip else identity>>>f False 1010.0>>>f True 100.1
Constant function
Constant function.
This is like the const function from the Prelude.
This function takes two arguments and always returns the first. In other
words, it ignores its second argument. The result of is always x yx.
>>>always True undefinedTrue
This can be useful with higher-order functions like map.
>>>map (always True) [1 .. 3][True,True,True]
Function composition
compose :: (a -> b) -> (b -> c) -> a -> c Source
Function composition.
This is like the . operator from the Prelude.
This function combines two other functions. The result of
is a new function the applies compose f gf first and then applies g. In other
words, is the same as compose f g xg (f x).
>>>let f = compose succ recip>>>f 90.1
Composing many functions together quickly becomes unwieldy. Use .> or
<. instead.
>>>let g = succ `compose` recip `compose` negate>>>g 9-0.1
(.>) :: (a -> b) -> (b -> c) -> a -> c infixl 9 Source
Left-associative compose operator.
This is like a flipped version of . operator from the Prelude.
This operator combines two other functions more naturally than compose.
The result of f is a new function that applies .> gf first and then
applies g.
>>>let f = succ .> recip>>>f 90.1
When reading code, pronounce this operator as "and then". So the above example could be read as: "Add one, and then take the reciprocal."
Thanks to its high precedence, composing many functions together is easy with this operator.
>>>let g = succ .> recip .> negate>>>g 9-0.1
(<.) :: (b -> c) -> (a -> b) -> a -> c infixr 9 Source
Right-associative compose operator.
This is like the . operator from the Prelude.
Sometimes it is more convenient to combine functions from right to left.
The result of g is a new function that applies <. fg but first
applies f.
>>>let f = recip <. succ>>>f 90.1
Pronounce this operator as "but first" when reading code. The example above could be read as: "Take the reciprocal, but first add one."
Composing many functions together is easy with this operator thanks to its high precedence.
>>>let g = negate <. recip <. succ>>>g 9-0.1
Function application
apply :: a -> (a -> b) -> b Source
Function application.
This is like the $ operator from the Prelude.
This function applies an argument to function.
>>>apply 4 succ5
Using this function to apply many arguments is cumbersome. Use |> or <|
instead.
>>>4 `apply` succ `apply` recip0.2
This function usually isn't necessary since is the same as
apply x ff x. However it can come in handy when working with higher-order
functions.
>>>map (apply 4) [succ, recip][5.0,0.25]
(|>) :: a -> (a -> b) -> b infixl 0 Source
Left-associative apply operator.
This is like a flipped version of the $ operator from the Prelude.
This operator applies an argument to a function. The result of x
is |> ff x.
>>>4 |> succ5
Since this operator has such low precedence, it can be used to remove parentheses in complicated expressions.
>>>4 |> succ |> recip0.2
When reading code, pronounce this operator as "pipe into". So the above example can be read as: "Four piped into plus one, piped into the reciprocal."
It can also be used with higher-order functions, although apply might be
clearer.
>>>map (4 |>) [succ, recip][5.0,0.25]
(<|) :: (a -> b) -> a -> b infixr 0 Source
Right-associative apply operator.
This is like the $ operator from the Prelude.
Sometimes it is more convenient to apply arguments right to left. The
result of f is <| xf x.
>>>succ <| 45
Like |>, this operator has low precedence so it can be used to remove
parentheses.
>>>recip <| succ <| 40.2
Pronounce this operator as "pipe from" when reading code. The example above can be read as: "The reciprocal piped from plus one, piped from five."
This operator is a convenient alternative to flip .apply
>>>map (<| 4) [succ, recip][5.0,0.25]
Strict function application
apply' :: a -> (a -> b) -> b Source
Strict function application.
This is like the $! operator from the Prelude.
This is the strict version of apply. It evaluates its argument with
seq before applying it to the given function. In other words,
is the same as apply' x fx `seq` .apply x f
>>>apply' undefined (always 0)*** Exception: Prelude.undefined