IBM / fp-go

functional programming library for golang
Apache License 2.0
1.6k stars 49 forks source link

fp-ts bind #98

Closed darky closed 7 months ago

darky commented 7 months ago

Hello! Thanks for fp-go! It's real FP revolution in Go 😄

fp-ts contains bind for any Monad Example, https://gcanti.github.io/fp-ts/modules/Either.ts.html#bind

It's allows to pass value through pipe, which very useful

Maybe it's possible to implement it somehow in fp-go?

CarstenLeue commented 7 months ago

Hi @darky I have started to implement the do functions for Option, Either and IO. More to come. You can check on the https://github.com/IBM/fp-go/tree/cleue-bind branch.

The implementation is not identical to the fp-ts one since it should be type safe, but go does not have a way to create ad-hoc types with keys that are passed in dynamically.

So the approach I took is similar to the pattern used in optics. Instead of passing in the name of a field as a first parameter and assume that the bound state is a record, we assume that the state is of an arbitrary type S1 and binding the new value results in a type S2. So the first parameter to the do functions is a function of the form

func(T) func(S1) S2

where

this follows the design for the setters in optics that have the signature

func(T) func(S) S

i.e. a special case of the above.

Let me know what you think or if you have other suggestions.

darky commented 7 months ago

Great, thanks! It's best possible solution for Go at this moment IMO! 👍

CarstenLeue commented 7 months ago

I merged the bind branch, I believe I covered all monads. Pls. let me know if anything is missing.