Giter VIP home page Giter VIP logo

go-functional's Introduction

Functional Programming in Go Actions Status Go Reference

A general purpose library offering functional helpers for Golang.

Note: this library required Go 1.18, which is currently in beta.

// Find the first 5 prime numbers
primes := iter.Take[int](iter.Filter[int](iter.Count(), isPrime), 5)
assert.SliceEqual(t, iter.Collect[int](primes), []int{2, 3, 5, 7, 11})

Read the docs.

Core concepts

This library introduces two core concepts, the Iterator and the Option. Using these two concepts this library derives many pre-defined iterators for use.

The Option

The Option is simply a type that represents the presence or absence of a value. Options behave somewhat like enumerations with two variants: Some(value) and None.

The Iterator

type Iterator[T any] interface {
	Next() option.Option[T]
}

The Iterator is an interface that defines the behaviour of some type that "yields" values. Each call to Next() on an Iterator will yield another value as defined by that specific Iterator. For example, the iterator iter.Count() yields 0, 1, 2, 3, etc. and onwards to infinity (or the integer limit!).

Iterators will yield Some(value) for as long as they have values to yield. Iterators that have exhausted all their values will then always yield None.

This library defines many iterators and a few are demonstrated below. For the entire set simply visit the package documentation.

Iterator showcase

Here are a few trivial example of what's possible using the iterators in this library.

// All even natural numbers (2, 4, 6, 8...)
isEven := func(n int) bool { return n%2 == 0 }
evens := iter.Filter[int](iter.Drop[int](iter.Count(), 1), isEven)
// All non-empty lines from a file
lines := iter.Exclude[string](iter.LinesString(file), filters.IsZero[string])
// String representations of numbers
numbers := iter.Map[int, string](iter.Count(), strconv.Itoa)

go-functional's People

Contributors

booleancat avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.