Reputation: 521
I was just looking up the implementation of the maximum function in haskell:
maximum :: forall a . Ord a => t a -> a
maximum = fromMaybe (errorWithoutStackTrace "maximum: empty structure") .
getMax . foldMap (Max #. (Just :: a -> Maybe a))
Could somebody explain this piece of code to me?
In particular it is not clear to me how this part
Max #. (Just :: a -> Maybe a)
works. What does #. mean? To me it looks like something similar to function composition but typing
:t #.
in the REPL gives an error. Also I think the argument for foldMap should be of the form
Monoid m => a -> m
But I can't see where the monoid structure comes from here.
Upvotes: 1
Views: 1089
Reputation: 48591
#.
is an operator that can make coercions easier to use.
f #. g
means
g
, coerced to the type off . g
Similarly,
f .# g
means
f
, coerced to the type off . g
In most cases, f #. g
only makes sense if f
is some combination of newtype constructors, newtype accessors, and fmap
.
The reason for using #.
in maximum
us probably over-zealousness on my part. It can almost certainly be replaced with .
without any problem.
Upvotes: 4