Below I’ve produced what I believe to be a summary of the standard $
like operators for various classes in Haskell. There’s some gaps however. Following the applicative pattern, you would think those operators would be $$
and <$$>
however I didn’t see operators like that on Hoogle or Hayoo. Could someone fill in the gaps with the most commonly used operators for such gaps?
| Function first | Op | Function second | Op |
----------------------------------------------------------------------------------
| Plain | (a -> b) -> a -> b | $ | a -> (a -> b) -> b | |
| Functor | (a -> b) -> f a -> f b | <$> | f a -> (a -> b) -> f b | |
| Applicative | f (a -> b) -> f a -> f b | <*> | f a -> f (a -> b) -> f b | <**> |
| Monad | (a -> m b) -> m a -> m b | =<< | m a -> (a -> m b) -> m b | >>= |
-----------------------------------------------------------------------------------
1
There isn’t one sadly. People usually use the left column with .
instead of separate operators. I sometimes see people use
|> :: a -> (a -> b) -> b
as a pipe line-esque operator F# and OCaml like. Generally, however, people use .
instead so
a |> f |> g
is written as
g . f $ a
This also applies to <$>
a |$> f |$> g
could be written as
g . f <$> a
This is actually faster (by a factor of 2x) since <$>
can be quite expensive for large collections.
3
The lens
package provides these operators:
(&) :: a -> (a -> b) -> b
(<&>) :: Functor f => f a -> (a -> b) -> f b
From the lens documentation on (&)
:
This is the flipped version of
($)
, which is more common in languages like F# as(|>)
where it is needed for inference. Here it is supplied for notational convenience and given a precedence that allows it to be nested inside uses of($)
.
Since 7.8 (&)
can be found in Data.Function too.