
My first golang package
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.

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.