5
Every so often I have a function of type a -> b
and a function of type b -> b -> c
and I would like a function of type a -> a -> c
. For example if I wanted to check the second element of each two tuples were equal
snd :: (a , b) -> b
(==) :: Eq a => a -> a -> Bool
And I want something of type
Eq b => (a, b) -> (c, b) -> Bool
The best way I have found to do this so far is
f g z=(.g).z.g
Which is ok, but it feels unneccessarily complex for something that is this simple. The pointfree representation is even worse:
flip=<<((.).).(.).flip(.)
Additionally it feels like this idiom should be representable with some sort of abstraction (it feels a lot like (<*>)
). I attempted to find a replacement by abstracting our functions to functors, the result is not very enlightening
flip=<<(fmap.).fmap.flip fmap ::
Functor f => f a -> (a -> a -> b) -> f (f b)
Is there a shorter way to express this idea? Particularly the pointfree version?
4
There is
– flawr – 2019-12-22T18:02:58.810on
but you need toimport Data.Function
.