Looking for point-free map

Hi everybody!
I have a lot of function calls like

create :: Handle -> ...
create h a = note INSERT_FAILED <$> h.insert a
-- with
Handle = {insert::Article -> Aff (Maybe Article)}

or with more parameters.
A simple equivalent would be:

mapPointFree :: Int -> Effect Int
mapPointFree a = add 1 <$> pure a

Does somebody know how to get rid of the parameter(s) on both sides?

2 Likes

These are equivalent:

mapPointFree a = add 1 <$> pure a
mapPointFree = map (add 1) <<< pure
mapPointFree = pure >>> map (add 1)

So you could make either of these modifications:

create h a = note INSERT_FAILED <$> h.insert a
create h = map (note INSERT_FAILED) <<< h.insert
create h = h.insert >>> map (note INSERT_FAILED)

But the point-free version ends up adding a few characters.

If you share some more examples of how this code is duplicated, we could probably come up with a more concise helper-function. For example:

helper error action a = note error <$> action a

create h = helper INSERT_FAILED h.insert
2 Likes

I reckon <<< is always a good idea when converting to point-free and using prefix notation instead of infix. And for two parameter, it’s <.. from the PointFree module.
My code is most of the time Api calls Application,Application calls Database, Database returns an array, head returns Maybe.
Two examples:

create :: Handle -> Article -> Aff (Either ApplicationError Article)
create h a = note INSERT_FAILED <$> h.insert a

addComment :: Handle -> Slug -> Comment -> Aff (Either ApplicationError Comment)
addComment h s c = note INSERT_FAILED <$> h.insertComment s c

and after converting it:

import PointFree ((<.), (<..))

create :: Handle -> Article -> Aff (Either ApplicationError Article)
create h = map (note INSERT_FAILED) <. h.insert

addComment :: Handle -> Slug -> Comment -> Aff (Either ApplicationError Comment)
addComment h = map (note INSERT_FAILED) <.. h.insertComment

looks ok. Thank you.

1 Like