nixihz / fn

Functional programming in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Functional programming in Go

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Go 100.0%