dogs

package module
v0.0.0-...-020adb6 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2023 License: Apache-2.0 Imports: 0 Imported by: 0

README

dogs

ci status Go Reference

logo

Make Go functional with dogs

Caution

This is a highly-experimental package. Any changes will be made in a backward-incompatible manner.

This package will not compile without gotip since type parameters are not supported currently in any of Go releases.

Probably you don't need this even after type parameters becomes GA. Even if you feel you do, maybe you shouldn't use this. It's against Go's philosophy.

Features

We will continue to implement more utility types and functions.

Type classes

  • Eq
  • Ord
  • Semigroup
  • Monoid

Data types

  • Pair
  • List
  • Slice
  • Map
  • Set
  • Iterator

Examples

More examples here.

FizzBuzz

func main() {
	monoid := option.DeriveMonoid[string](algebra.DeriveAdditiveSemigroup[string]())
	fizzBuzz := func(i int) string {
		fizz := option.Filter(option.Some[string]("Fizz"), func(_ string) bool { return i%3 == 0 })
		buzz := option.Filter(option.Some[string]("Buzz"), func(_ string) bool { return i%5 == 0 })
		return option.UnwrapOr(monoid.Combine(fizz, buzz), fmt.Sprint(i))
	}
	it := iterator.Map(iterator.Range[int](1, 15), fizzBuzz)
	iterator.ForEach(it, func(s string) { fmt.Println(s) })
}

Fibonacci

func main() {
	type Pair = pair.Pair[int, int]
	it := iterator.Unfold(
		Pair{1, 1},
		func(p Pair) (Pair, int, bool) {
			a, b := p.Values()
			return Pair{b, a + b}, a, true
		},
	)
	iterator.ForEach(
		iterator.Take(it, 5),
		func(i int) { fmt.Println(i) },
	)
}

Acknowledgements

This library is inspired mainly by:

and many other functional languages.

License

Distributed under the Apache License, Version 2.0. See LICENSE for more information.

Documentation

Overview

Package dogs provides some types that are commonly used.

Directories

Path Synopsis
classes
cmp
cmd
Package examples contains several examples of procedural vs functional programming in Go.
Package examples contains several examples of procedural vs functional programming in Go.
types
set

Jump to

Keyboard shortcuts

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