I think map() is useful, even if it does not look like Go and rubs a little against the sprit of simplicity of Go. Wish the for loop in Go would return a result, which could accomplish the same but would be a little bit more Go like x := for y := range z { return y } // unclear return :-( If you want Either, use Haskell. There seems also to be a performance problem with map(). It would work better if Go had Iteration…
`Either` works pretty well in Go. I implemented it and it felt reasonably close to Rust/Haskell (without `try!` of course).
FP-Go: Functional programming library for Golang
11–20 of 185 posts
Re: FP-Go: Functional programming library for Golang
#12Re: FP-Go: Functional programming library for Golang
#13Earlier quoted context omitted.
`Either` works pretty well in Go. I implemented it and it felt reasonably close to Rust/Haskell (without `try!` of course).
Either is nearly in the language anyways. The vast majority of pragmatic go functions will return [Result, Error] and or just [Error]. We are only missing support to treat this as a monad.
Not an expert in Go but I think you can do this:
func compose[A any, B any, C any](a func(A) (B, error), b func(B) (C, error)) func(A) (C, error) {
return func(aInp A) (C, error) {
res, err := a(aInp)
if err == nil {
return b(res)
} else {
return *new(C), err
}
}
}
The above is equivalent to haskells fish operator >=>The bind operator (>>=) can be implimented in terms of composition:
func bind[A any, B any, C any](a func(A) (B, error), b func(B) (C, error), aInput A) (C, error) {
return compose[A, B, C](a, b)(aInput)
}Re: FP-Go: Functional programming library for Golang
#14Earlier quoted context omitted.
`Either` works pretty well in Go. I implemented it and it felt reasonably close to Rust/Haskell (without `try!` of course).
Either is nearly in the language anyways. The vast majority of pragmatic go functions will return [Result, Error] and or just [Error]. We are only missing support to treat this as a monad.
Re: FP-Go: Functional programming library for Golang
#15I didn't look too much at this but are they offering an alternative to the (IMO ugly) error checking pattern Go enforces? It's interesting to me shoehorning this into Go in particular. Go is notoriously stringent on how you write code.
Re: FP-Go: Functional programming library for Golang
#16Potentially a silly question: isn't the garbage collection going to become a problem with this style of Go implementation in large software?
Re: FP-Go: Functional programming library for Golang
#17Re: FP-Go: Functional programming library for Golang
#18These abstractions are not native to go. If you miss them, pick a better language.
Re: FP-Go: Functional programming library for Golang
#19Potentially a silly question: isn't the garbage collection going to become a problem with this style of Go implementation in large software?
Why would it be a problem? A lot of functional languages are garbage collected and it hasn’t been a problem for them