foldable

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2022 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Class

type Class[
	A any,
	B any,
] interface {
	//Fold(TM) M
	//FoldMap(func(A) M, TA) M
	Fold(monoid.Class[A], data.Data[A]) A
	FoldMap(monoid.Class[B], func(A) B, data.Data[A]) B

	//FoldR(func(A, B) B, B, TA) B
	//FoldL(func(B, A) B, B, TA) B
	FoldR(func(A, B) B, B, data.Data[A]) B
	FoldL(func(B, A) B, B, data.Data[A]) B
}

type Type

type Type[
	A any,
	B any,
] struct{}

func NewType

func NewType[
	A any,
	B any,
]() Type[A, B]
Example
package main

import (
	"fmt"

	"github.com/calebcase/base/data/eq"
	"github.com/calebcase/base/data/foldable"
	"github.com/calebcase/base/data/function"
	"github.com/calebcase/base/data/list"
)

func main() {
	{
		t := foldable.NewType[int, int]()

		l := list.List[int]{1, 2, 3}

		sum := func(x, y int) int {
			return x + y
		}

		fmt.Println(t.FoldL(sum, 0, l))
		fmt.Println(t.FoldR(sum, 0, l))
	}

	{
		t := foldable.NewType[int, string]()

		l := list.List[int]{1, 2, 3}

		comma := func(x string, y int) string {
			if x == "" {
				return fmt.Sprint(y)
			}

			return x + ", " + fmt.Sprint(y)
		}

		fmt.Println(t.FoldL(comma, "", l))
	}

	{
		t := foldable.NewType[list.List[int], list.List[int]]()

		ll := list.List[list.List[int]]{{1, 2, 3}, {4, 5}, {6}, {}}

		lt := list.NewType[int](eq.NewType(eq.Comparable[int]))

		fmt.Println(t.Fold(lt, ll))
		fmt.Println(t.FoldMap(lt, function.Id[list.List[int]], ll))
	}

}
Output:

6
6
1, 2, 3
[1 2 3 4 5 6]
[1 2 3 4 5 6]

func (Type[A, B]) Fold

func (t Type[A, B]) Fold(ma monoid.Class[A], ta data.Data[A]) A

func (Type[A, B]) FoldL

func (t Type[A, B]) FoldL(f func(B, A) B, z B, ta data.Data[A]) B

func (Type[A, B]) FoldMap

func (t Type[A, B]) FoldMap(mb monoid.Class[B], f func(A) B, ta data.Data[A]) B

func (Type[A, B]) FoldR

func (t Type[A, B]) FoldR(f func(A, B) B, z B, ta data.Data[A]) B

Jump to

Keyboard shortcuts

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