fp-ts icon indicating copy to clipboard operation
fp-ts copied to clipboard

`flap` should be flipped

Open samhh opened this issue 2 years ago • 5 comments

The current type signature of flap is:

Functor f => a -> f (a -> b) -> f b

It would be better if it were:

Functor f => f (a -> b) -> a -> f b

The issue I'm constantly running into with the current shape is that I usually have a in a pipeline and want to bring my own f (a -> b) i.e. it's stopping me from writing pointfree code:

// Current
flow(f, x => A.flap(x)([g, ...etc]), h)

// Desired
flow(f, A.flap([g, ...etc]), h)

The type system isn't capable of allowing flip to handle this.

The proposed signature would also match Haskell:

samhh avatar Jan 25 '23 12:01 samhh

But in Haskell (or PureScript) you'd almost be using flap by its infix flavor <@> which matches current curried signature from fp-ts, so that:

Apply a value in a computational context to a value in no context. ^1

imcotton avatar Jan 29 '23 22:01 imcotton

Anecdotally the Haskell codebase I work on most has two usages of flap, one infix/flipped and one not.

I'd be open to other suggestions as this has been a recurrent pain point for me with flap in fp-ts.

Given that with the type system our flip isn't as flexible as in other languages, I wonder if fp-ts should adopt a convention for commonly flipped functions. An example would be fp-ts-std/Array::elemV, which has been used a lot in our TS codebase at work.

samhh avatar Feb 03 '23 13:02 samhh

In retrospect there are voidRight and voidLeft pairs in alike also missing in fp-ts:

(<$) :: f a b. Functor f => a -> f b -> f a
($>) :: f a b. Functor f => f a -> b -> f b

Maybe having similar (flapFlipped or flapV) without breaking change of existing flap to compensate the lack of infix operator?

Indeed a reliable flip helps a lot in practices, finding myself also using utils like elemV for missing pieces thanks to fp-ts-std btw.

imcotton avatar Feb 04 '23 10:02 imcotton

I'd love a flapV! I went with "V" as it sort of looks like a mirror, but any naming convention would suffice.

Very much agreed on $> and <$ also. I twitch every time I write map(constant(x)) 😄

samhh avatar Feb 04 '23 12:02 samhh

I'm having exactly the same issue as @samhh. The current param order is yet to work for me.

joshdifabio avatar Mar 22 '23 11:03 joshdifabio