gigobyte / purify

Functional programming library for TypeScript - https://gigobyte.github.io/purify/
ISC License
1.5k stars 58 forks source link

apChain? #639

Closed TheWix closed 12 months ago

TheWix commented 1 year ago

This is an interesting one and I haven't seen such a function elsewhere, but how would one handle using ap() with a function that returns an effect? I am finding myself constantly calling join because I am composing Either with functions that also return Either? Is something like apChain a thing? Or am I doing something wrong?

gigobyte commented 1 year ago

Hey, can you share some examples?

TheWix commented 1 year ago

Sorry @gigobyte , just seeing this. Here's a simple example:

declare const div: (a: number) => (b: number) => Either<string, number>

declare const a: Either<string, number>
declare const b: Either<string, number>

const res = b.ap(a.map(div))

image

This makes sense to me. We have map for functions that don't return effects, and chain (composition of map and join), but I haven't found anything similar for applicatives. I even asked on the functional programming slack and got no answer. I also haven't seen an equivalent in fp-ts. I feel like I am missing something...

gigobyte commented 1 year ago

Yeah I don't think there's a function for what you're trying to do and my FP is getting rusty so I can't think of anything clever with the existing methods.

TheWix commented 1 year ago

Yea, the only thing I can think of is adding a function apChain that is similar to just chain where it is just ap followed by join.