Live data from Hacker News

FP-Go: Functional programming library for Golang

github.com

111–120 of 185 posts

Re: FP-Go: Functional programming library for Golang

#111
post #77

oh my, its horrible: client := H.MakeClient(HTTP.DefaultClient) readSinglePost := H.ReadJson[PostItem](client) readSingleCatFact := H.ReadJson[CatFact](client) data := F.Pipe3( T.MakeTuple2("https://jsonplaceholder.typicode.com/posts/1", "https://catfact.ninja/fact"), T.Map2(H.MakeGetRequest, H.MakeGetRequest), R.TraverseTuple2( readSinglePost, readSingleCatFact, ), R.ChainFirstIOK(IO.Logf[T.Tuple2[PostItem, CatFact]…

Actually, this sample is pretty easily followed and readable to anyone familiar with iterators, tuples, filter, map and transduction. Sure the stuff like Pipe3/Map2 is irritating because Go doesn't have function overloading.

Now, try and do the equivalent in "normal" Go code - it will be 3x-5x the lines of code. (Probably more)

Re: FP-Go: Functional programming library for Golang

#112
post #41

Go is not a functional language and its generic type system is very restrictive. Why not use a functional language to begin with?

Not picking sides here (though disclosure: I use and like FP techniques/tools/languages/libraries).

Go provides a set of nice features (fast startup, easy cross-platform building, great tooling, good package management) that can be hard to come by with other languages. It is not unreasonable to want to have your cake (all of the above features) and eat it too (occasionally use functional idioms in addition to the usual imperative ones).

For this reason I try to keep abreast of the various FP libraries in Go, though I have yet to use one in anger.

Re: FP-Go: Functional programming library for Golang

#113
post #98

Earlier quoted context omitted.

I believe these things are mostly productivity sinks, which is why I am such a Go fan and also so sad to see these types of projects in Go. This is exactly what I was afraid of when generics were introduced, and now I get to spend time arguing with people who read some blog post about how functional programming and type theory will save the world, instead of actually being productive. Ugh.

I tend to agree. I’ve done a lot of JS and Python where I end up spending time doing functional tricks basically just because I’m bored and it makes things interesting. With Go when I’m bored I start profiling and making a zero alloc version of a function that only runs once a week.

FP totally works in Python...until you realize how slow function calls in Python are.

Re: FP-Go: Functional programming library for Golang

#114

> If your pure function can return an error, then it will have a (T, error) return value in idiomatic go. In functional style the return value is Either[error, T] because function composition is easier with such a return type. This seems flawed. In idiomatic Go, T and error are always independently observable. The Either monad implies that they are dependent, which is not true.

I spent about 6 years writing Go at $dayjob, and while what you're saying is technically true, idiomatically you also generally wanted to avoid scenarios where you would _want_ to observe them independently. The standard behavior is is if `err != nil`, the result should be ignored.

Re: FP-Go: Functional programming library for Golang

#115

Earlier quoted context omitted.

I've used the slices package and I agree it's useful. I was wondering more about generics use in application code

Before the slices package you had to write those functions for all your types, it's much better now! I also like using generics for API request/response code, ex: https://go.dev/play/p/OWf9eFmg1qF With generics you don't need to return any/interface{} / type assert at runtime

Hmm for this example I would be more inclined to use an output parameter rather than generics

    func Request(req, resp any) error {
        // send request (http, etc)
        b, err := json.Marshal(req)
        if err != nil {
            return err
        }
        log.Printf("sent request %+v - %s", req, b)

        // read response
        if err := json.Unmarshal([]byte(`{"success": false, "error": "invalid login"}`), resp); err != nil {
            return err
        }

        return nil
    }
But I kind of agree it's nicer to use a return value rather than an output parameter. I'm excited to see what other new uses people come up with for generics!

Re: FP-Go: Functional programming library for Golang

#116
post #114

> If your pure function can return an error, then it will have a (T, error) return value in idiomatic go. In functional style the return value is Either[error, T] because function composition is easier with such a return type. This seems flawed. In idiomatic Go, T and error are always independently observable. The Either monad implies that they are dependent, which is not true.

I spent about 6 years writing Go at $dayjob, and while what you're saying is technically true, idiomatically you also generally wanted to avoid scenarios where you would _want_ to observe them independently. The standard behavior is is if `err != nil`, the result should be ignored.

Anything that implements or consumes `io.Reader` or `io.Writer` would dispute that.

Re: FP-Go: Functional programming library for Golang

#117

Love expression-oriented pseudo-FP (F#, Scala, Rust), but I think this recent trend of trying to shoehorn Haskell-lite features into mainstream imperative languages is, to put it gently, extremely awkward. That said, I actually do remember my first exposure to Golang being a blog post about using monads to avoid incessantly typing `if err != nil`. Very much like that original author, my personal values in software en…

totally agree, for me golang is a strongly imperative language and that's ok. I'm willing to be proved wrong, but I would imagine if you want to do functional programming it's going to be a lot easier to just use a different language.

Big time agreement here as well.

I'm biased because I've built a career on Go at this point but the pragmatism and ability to just get things done in Go without faffing about with unnecessary abstractions is I think one of the strongest practical demonstrations of how incredible an imperative language can be, and for me personally at least, no FP language will ever beat the productivity that I can achieve with Go, especially because at least in my problem domain the real world problems always have enough corner cases that FP wouldn't even be useful.

In Go I just systematically eliminate and handle each possible step and state, in a straightforward way, directly deal with the business logic, and then it's done and it works predictably and efficiently for years. Interfaces really are a sufficient form of polymorphism, too.

Re: FP-Go: Functional programming library for Golang

#118
post #109
post #94

Earlier quoted context omitted.

Indeed, this was the exact stuff that I loved not having to deal with in Go: architecture astronauts pushing their code golf on everyone else. I’m quite sad to see this project as it demonstrates that Go is starting to lose many of the characteristics that attracted me to it in the first place. (For some context, I know quite a bit about functional programming and formal type theory, having studied the latter in grad…

"Nobody" uses this stuff in Go. I mean I'm sure it's not literally nobody, but I'm yet to encounter even one example of someone using this stuff in the wild. I'd love to be able to survey the authors of the dozen-ish variations on this posted over the last couple of years (most of the much less elaborate than this) and see how many of them are still using it in their real code. Again I'm sure the answer isn't literal…

Saying no to generics sends a strong signal to FP astronauts to "take it somewhere else". This saying no to a huge number of things is the superpower of Go and its community IMO, but those "go away" signals are getting weaker over time, which increases the likelihood of needing conversations and decisions about when to use what style, which is exactly the sort of thing I enjoy not having to do with Go.

For example: this entire HN thread. And all the other libraries you mention that keep soliciting conversations, nerd sniping people who could be spending that time making better products instead of quibbling over FP code golf. But maybe those folks will always find things to quibble over...

Re: FP-Go: Functional programming library for Golang

#119
post #77

oh my, its horrible: client := H.MakeClient(HTTP.DefaultClient) readSinglePost := H.ReadJson[PostItem](client) readSingleCatFact := H.ReadJson[CatFact](client) data := F.Pipe3( T.MakeTuple2("https://jsonplaceholder.typicode.com/posts/1", "https://catfact.ninja/fact"), T.Map2(H.MakeGetRequest, H.MakeGetRequest), R.TraverseTuple2( readSinglePost, readSingleCatFact, ), R.ChainFirstIOK(IO.Logf[T.Tuple2[PostItem, CatFact]…

Actually, this sample is pretty easily followed and readable to anyone familiar with iterators, tuples, filter, map and transduction. Sure the stuff like Pipe3/Map2 is irritating because Go doesn't have function overloading. Now, try and do the equivalent in "normal" Go code - it will be 3x-5x the lines of code. (Probably more)

it has no error handling. also look at the API, what a joke:

https://godocs.io/github.com/IBM/fp-go/function

and check out the "constants":

https://godocs.io/github.com/IBM/fp-go/function#pkg-variable...

Re: FP-Go: Functional programming library for Golang

#120

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.

To me, the problem is that Go returns two values which makes it hard to compose functions.
Post reply on HN