l3x / learn-fp-go

Learning Functional Programming in Go
97 stars 53 forks source link

error monad ? #4

Closed gdiazlo closed 6 years ago

gdiazlo commented 6 years ago

While reading the book, page 452, I came across this file:

learn-fp-go/4-purely-functional/ch10-monads/01_car_steps/src/workflow/monad.go

To me looks like the error monad described in: https://github.com/onemouth/go-errormonad/blob/master/monad.go

is it related?

l3x commented 6 years ago

Had never seen that code until just now.

They do look very similar. Thank you for sharing that. They both adhere to the idiomatic Go return pattern of (returnValue, err)

However, it's best to steer clear of type Any interface{} because the reflection required to determine the type of the data hurts performance (and because it's not type safe).

Also, being a stickler for naming, I much prefer starting by Getting the initial data input (rather than Returning) and proceeding the the Next step (rather than Bind, Bind, Bind....)

So, "yes" they are related in the that they both implement a monadic pattern, which lifts each step where the data can be transformed and then returned to the pure monadic flow.