by andolanra · 5y ago · 0 comments
func getTopUsers(posts []Post) []UserLevelPoints {
return posts.GroupBy(func (v Post) string { return v.Level })
.Values()
.Map(getTopUser)
}
This pipeline style is significantly easier to read (especially without having to put all those extraneous type parameters in your call to compose3!) and doesn't actually lose any of the core advantages of the functional style: purity, testability in isolation, equational reasoning, and so forth. Sure, the Haskell equivalent might use composition… but composition reads more or less naturally in Haskell, and I don't think it does at all here in Go. If your specific approach to "functional programming" makes your code theoretically easier to reason about but practically harder to both read and write, then is it really helping you much? x := foo()
y := bar(x)
z := baz(y)
Wow.Maybe Haskell and F# can elide this kind of thing, but it's not something the Go compiler is going to do.
The final generics design is probably not expressive enough to do it ergonomically in user code neither (building something where the intermediates are a library type with a last `.something()` call to collect a final []T).
No secret sauce, just having the pleasure of chosing multiple backends, including an interpreter.
Bytecode runtime during workflow, optimized native AOT for release and final production tests.
I'm often think FP-first (data science) -- here though, I was surprised by how I went back over the imperative alternative.
Avoiding `append()` isnt worth it in a language where this level of annotation is required to do FP. It's less clear.