fn

module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 2, 2023 License: BSD-3-Clause

README

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 https://github.com/mariomac/go-stream-benchmarks/pull/1

Directories

Path Synopsis
Package constraints defines a set of useful constraints to be used with type parameters.
Package constraints defines a set of useful constraints to be used with type parameters.
Package seqio is an experimental package providing Seq interoperability with standard Go IO interfaces like io.Reader and bufio.Scanner.
Package seqio is an experimental package providing Seq interoperability with standard Go IO interfaces like io.Reader and bufio.Scanner.
Package seqjson is an experimental package exposing json.Decoder as a Seq
Package seqjson is an experimental package exposing json.Decoder as a Seq
Package fnmath is a small suite of mathematical utilities working on seq.Seq
Package fnmath is a small suite of mathematical utilities working on seq.Seq
Package opt implements "optional" values.
Package opt implements "optional" values.
Package seq provides functions and data structures for incorporating functional programming ideas into standard Go programs.
Package seq provides functions and data structures for incorporating functional programming ideas into standard Go programs.
Package slice is a lightweight package of functions for transforming standard in-memory Go structures.
Package slice is a lightweight package of functions for transforming standard in-memory Go structures.
Package fntesting is an experimental package provided some testing utilities for Seqs.
Package fntesting is an experimental package provided some testing utilities for Seqs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL