Skip to content

kamstrup/fn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fn(), Functional Programming for Golang

Go Report Card PkgGoDev

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.

Documentation

You will find comprehensive documentation in our docs folder, or you might want to dive directly into our simple examples.

Examples

import (
    "fmt"
	
    "github.com/kamstrup/fn/seq"
)

func printGreenTeam() {
    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 teams
    allTeamMembers := 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 team
    greenTeam := seq.SliceOfArgs("Betty", "Maurice", "Bob", "Charles", "Inga").
        Where(seq.Not(nameSet.Contains)).
        Limit(2).
        ToSlice()
    
    if len(greenTeam) != 2 {
        panic("not enough team members for the green team")
    }
    
    // Members on the green team are assigned player numbers starting from 10
    greenTeamNumbers := 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
}

Performance

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