Fn is library for golang that enable you to blend functional programming techniques with standard idiomatic Go code.
We are inspired by Clojure, Vavr, and the Java Streams APIs that were introduced back in Java 8, and want to provide something of similar spirit that makes it even more fun to write Go code.
You will find comprehensive documentation in our docs folder, or you might want to dive directly into our simple examples.
import (
"fmt""github.com/kamstrup/fn/seq"
)
funcprintGreenTeam() {
blueTeam:=seq.SliceOfArgs("Alan", "Betty")
redTeam:=seq.SliceOfArgs("Maria", "Bob")
// Let's create a set of names for the people on the blue and red teamsallTeamMembers:=seq.ConcatOf(blueTeam, redTeam)
nameSet:=seq.Reduce(seq.MakeSet[string], nil, allTeamMembers).Or(nil)
// We need 2 members for the green team, that are not already on the blue or red teamgreenTeam:=seq.SliceOfArgs("Betty", "Maurice", "Bob", "Charles", "Inga").
Where(seq.Not(nameSet.Contains)).
Limit(2).
ToSlice()
iflen(greenTeam) !=2 {
panic("not enough team members for the green team")
}
// Members on the green team are assigned player numbers starting from 10greenTeamNumbers:=seq.ZipOf[int, string](seq.RangeFrom(10), greenTeam)
greenTeamNumbers.ForEach(func (member seq.Tuple[int, string]) {
fmt.Println("Name:", member.Value(), "Number:", member.Key())
})
// Prints:// Name: Maurice Number: 10// Name: Charles Number: 11
}If the foundational functional data structures and algorithms is not done carefully, execution speed and memory usage will suffer. Fn() is designed to make the best of what the Go runtime can provide. Initial benchmarks puts it as a top contender among Golang functional libraries. See benchmarks here mariomac/go-stream-benchmarks#1