Live data from Hacker News

FP-Go: Functional programming library for Golang

github.com

11–20 of 185 posts

Re: FP-Go: Functional programming library for Golang

#11

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).

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

#13

Earlier 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.

You don't need support. Monad composition isn't a special feature it can be implemented directly.

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

#14

Earlier 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.

Either as a pattern is _used_ everywhere, but the standard library in go doesn't have one (people just use a tuple), so it's _always_ encoded wrong. It's very annoying.

Re: FP-Go: Functional programming library for Golang

#15
post #2

I 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.

They have Either, so kind of. It looks serviceable, if a little messy without language support for safely destructuring its cases.

Re: FP-Go: Functional programming library for Golang

#16
post #12

Potentially 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

Re: FP-Go: Functional programming library for Golang

#17

Earlier quoted context omitted.

Just like FPP is incompatible with C, I agree. And if you want a real taste of FPP go with either Haskell, OCaml or even the good ol' LISP

Or Rust.

My contention that FP has no meaning gains another point of evidence.

Re: FP-Go: Functional programming library for Golang

#19
post #12

Potentially 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

Functional languages typically have much more sophisticated garbage collectors than Go's.
Post reply on HN