Sitemap

Functional Programming in Go: An Adventure with gofn and pipe 🚀

2 min readFeb 26, 2024
Press enter or click to view image in full size

Hello, code explorers! Today, we’re embarking on a fascinating journey through the world of functional programming in Go, using the gofn and pipe libraries. 🧙‍♂️✨

Introduction to Functional Go

By nature, Go isn’t a purely functional language like Haskell or Erlang, but that doesn’t mean we can’t apply functional concepts to write concise, readable, and efficient code. And that’s where gofn and pipe come in, turning our adventure into something even more exciting!

What Are gofn and pipe?

  • gofn: A library that facilitates the application of functional concepts in Go.
  • pipe: An extension that allows chaining functions elegantly, drawing inspiration from the concept of pipes in languages like Elixir.

A Practical Quest

To illustrate the magic, let’s tackle a common challenge: filtering even numbers from a slice of integers, doubling their values, and displaying the result. Sounds simple? With gofn and pipe, it's even simpler!

Setting the Stage

Before we begin, make sure you have the libraries installed. If not, a simple go get does the trick:

go get github.com/devalexandre/gofn
go get github.com/devalexandre/pipe/v1

The Enchanted Code

Now, with our magic wands (or keyboards 🧙‍♂️⌨️) at the ready, let’s dive into the code:

package main

import (
"fmt"
"github.com/devalexandre/gofn/pipe"
v1 "github.com/devalexandre/pipe/v1"
)

func main() {
data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

// Defining the pipeline
process := v1.Pipe(
pipe.Filter(func(i int) bool { return i%2 == 0 }),
pipe.Map(func(i int) int { return i * 2 }),
)

// Applying the pipeline to the data
result, err := process(data)
if err != nil {
fmt.Println("Error processing:", err)
return
}

fmt.Println(result) // Expected: [4 8 12 16 20]
}

Deciphering the Spell

What we just witnessed is pure functional magic:

  1. Filter (Filter) the even numbers.
  2. Transform (Map) each number by doubling its value.
  3. All this with a simple chaining of functions, thanks to the powerful Pipe.

And the result? An elegant sequence of transformed numbers, exactly as we expected!

Why Is This Awesome? 🌟

  • Readability: The code speaks for itself. Filtering and mapping are intuitive concepts, making our logic clear as day.
  • Reusability: The functions are small and focused. We can reuse them in different parts of our application.
  • Testability: Each function can be tested in isolation, making tests simpler and more robust.

Embarking on the Adventure

We hope this little adventure has illuminated the path to explore functional programming in Go. The world is vast and full of wonders to discover. With gofn and pipe, you're well-equipped to tackle many challenges with style and efficiency.

So, grab your wand (or keyboard), invest some magic (and logic), and watch your Go applications shine like never before! 💻

--

--

Alexandre E Souza
Alexandre E Souza

Written by Alexandre E Souza

Microservice Evangelist | Go Lover | JS Lover

Responses (1)