Image for post
Image for post

My first golang package

DI DIO Maximilien
Dec 16, 2018 · 3 min read

Do you like functional programming ?

Golang isn’t the better choice to do functional programming. Go was designed to be simple to learn for everyone and efficient to build complex applications. Go promote idiomatic ways to solve algorithm issues because Golang have to be easy.

This philosophy reminds me KISS principle, (Keep It Simple Stupid) you doesn’t need to use sophisticated stuff to create simple features. But sometimes, i want to use some functional programming skills in Go. This is why i was thinking about the easiest way to use iterators like Map Filter and Reduce in Go.

Implements theses functions isn’t difficult in some language like Javascript, Php because you doesn’t have to explicit the type for collection.

function map($arr, callable $callback) {
$res = array();
for($i = 0; $i < sizeof($arr); $i++) {
$res[] = $callback($arr[$i], $i, $arr);
}
return $res;
}

The same for Java because you can use generic type

public static  <T> List<T> map(List<T> list, Function<T, T> function ) {
List<T> arr = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
arr.add(function.apply(list.get(i)));
}
return arr;
}

Okay Java syntax is a little bit harder but it still possible to implement a stupid Map function.

In golang, you have to declare type of each variable and you doesn’t have generics. (probably the biggest mistakes for me) But you can use empty interface type to create function which able to work with all types availables in your project. But the way to implement Map, Filter and Reduce isn’t easy to understand. I’ve learn everythings from this package https://github.com/yanatan16/itertools.

type Iter chan interface{}

The solution found by Jon Eisen was to create a type to define channel of empty interface. This type is used to build interators functions.

type Mapper func (interface{}) interface{}
// Map an iterator to fn(el) for el in it
func Map(fn Mapper, it Iter) Iter {
c := make(Iter)
go func () {
for el := range it {
c <- fn(el)
}
close(c)
}()
return c
}

So i was very happy to have my functional interators availables for go, but i wanted to fork this library to change some stuff like callback declaration for Map, Filter et Reduce and turn theses functions into methods to allow the possibility to chain method calls.

Image for post
Image for post
Stream golang package logo

I’ve created a package called Stream available on github, refrence to Java Stream API from Java 8. I will not explain every parts of this package, you can see README.md file for more details. Stream provide another way to work with theses interators and provide some stupid function from Javascript world like Every and Some.

s := New(2, 4, 6, 8, 10)isPair := func(current interface{}, index int, all Stream) bool {
return current.(int)%2 == 0
}
result := <-s.Every(isPair) // result = true

You can chain method call to have an another way to think your code.

s := stream.New("Max", "Steve", "Fabio") // Stream of stringminLength := func(current interface{}, index int, all stream.Stream) bool {
return len(current.(string)) > 3
}
toUpper := func(current interface{}, index int, all stream.Stream) interface{} {
return strings.ToUpper(current.(string))
}
s = s.Filter(minLength).Map(toUpper) // STEEVE, FABIO

I will maintain this package and improve it just for fun. Thx for reading and see you soon for another draft.

stupid dev blog

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store