Stephen Diehl's protolude library is excellent. I highly recommend it as a default prelude for Haskell.
It's so good and so close to what I want that whenever I do come across something missing it's as obvious as a glitch in good music.
Here's what I'd like:
(<<$>>) :: (Functor f, Functor g) => (a -> b) -> f (g a) -> f (g b)
(<<$>>) = fmap . fmap
I do this enough that I'd like it to hand. Pi Delport suggested the syntax & that it should be in a standard library somewhere.
I want for
to be flip fmap
and forA
to be what's currently for
(protolude/protolude#19).
From the pretty-show library.
A very simple pretty printer, great for debugging data structures. Would have to be adjusted for better string types for protolude.
Like traceShow
, but pretty. Doesn't actually exist yet.
guarded :: Alternative f => (a -> Bool) -> a -> f a
guarded p x = bool empty (pure x) (p x)
Another great idea from Pi Delport.
Some addendums, for completeness!
Working with multiple levels of Applicative
Two levels:
Three levels:
And so on, following the same pattern.
(Yes,
purer
andpurerer
are a bit silly. Suggestions for better names welcome. :)guardedA
, to go withguarded
Definition:
These are useful whenever you want the presence or absence of some value to be predicated on some function of it.
For example, here's a definition of filter as a guarded identity fold, in both pure and Applicative versions:
Applicative function composition:
<.>
to go with<$>
For the sake of intuition and symmetry, it really makes sense to have a
<.>
operator that is to<$>
what.
is to$
.This allows you to do the same kinds of code restructuring as you're accustomed to with
$
and.
:foo x = f $ g x
→foo = f . g
foo x = f <$> g x
→foo = f <.> g
Example: