November 8th, 2024

Functional Programming in Go

The article highlights functional programming in Go, focusing on map, filter, and reduce operations. It explains first-class functions and provides code examples, promoting their use for elegant and efficient coding.

Read original articleLink Icon
Functional Programming in Go

The article discusses the concept of functional programming in Go, emphasizing the use of functions as primary control structures rather than just organizing imperative code. It introduces three key operations: map, filter, and reduce, which allow for elegant solutions to various programming problems. The author explains how to implement these operations in Go, starting with the definition of first-class functions, which can be passed as arguments or returned as results. The Map function applies a transformation function to each element of a slice, while the Filter function retains elements based on a specified condition. The Reduce function combines elements of a slice into a single value through a specified operation. The article provides code examples for each operation, demonstrating their utility and conciseness compared to traditional looping methods. The author concludes by encouraging readers to explore these functional programming techniques and their applications in Go, highlighting the potential for more elegant and efficient code.

- Functional programming in Go emphasizes using functions as primary control structures.

- Key operations include map, filter, and reduce, which simplify data manipulation.

- First-class functions in Go can be passed as arguments or returned from other functions.

- The article provides practical code examples for implementing map, filter, and reduce.

- The author encourages exploring functional programming techniques for more elegant code solutions.

Link Icon 8 comments
By @eska - 6 months
I think shoehorning fp onto go just results in people having a wrong first impression of fp, damaging them for years or forever. fp is so much more than map, filter and reduce.
By @SleekoNiko - 6 months
I've tried going down this route myself by using libraries like fp-go[1]. I think this particular project has done a great job, but I ultimately opted not to pursue functional programming in Go for a few reasons:

- Ergonomics weren't the greatest when working with monads such as options and results. I think pattern matching is needed here, but concepts like those go against Go's core design philosophy.

- I suspect there are several cases where runtime performance is an issue, but admittedly did not investigate this.

- Perhaps most importantly, it deviates from the way most people read and write Go, and less importantly, LLMs struggle too.

Because of these reasons, I came to the conclusion that the advantages were not worth the trade-offs.

Perhaps somebody will create a garbage-collected Rust-like language in the future and bridge the gap between the two languages.

[1] - https://github.com/IBM/fp-go

By @freitzzz - 6 months
Function operations are possible in Go like it’s possible in any language that define function in the syntax. But functional programming goes beyond functions, and Go was not designed to support it.

I’m a fan of Go and FP and would love to see someone bridge other FP aspects like monads, in a way that feels natural in Go.

By @librasteve - 6 months
I think there is a genuine tension between languages that can do functional and languages that are functional. Sometimes having functional there as one of the tools, but not the only tool, is best.

For example raku (and perl before it) has very good support with map/grep/closures, etc.

https://rakujourney.wordpress.com/2024/10/12/raku-burritos/

Says…

And let’s end with a quote (sadly I did not record the originator)…

I think i just expressed my thought in a wrong way, haha. I am a functional freak, and the first thing i did was check out Raku’s functional patterns. I was amazed. Raku can be extremely functional, but in my opinion language can be called functional when there’s no other way other than functional for the most part. Raku has great functional support, but the language doesn’t force you into anything, you can do basically anything! A sandbox language, and i am loving it.

anon

By @ojhughes - 6 months
Take a look at the Ginkgo testing framework to see the horrors of shoe horning FP into Go

https://onsi.github.io/ginkgo/#writing-specs

By @Insanity - 6 months
Hey, I wrote a book with that exact same title as the blog post.. :) not sure if HN frowns upon this kind of self-advertising in comments, but well.. you can find it on Amazon / Packt, if you are interested lol