foldable

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Concat added in v0.0.9

func Concat[
	A any,
	FA Class[
		A,
		A,
		monoid.Class[A],
		monoid.Class[A],
		eq.Class[A],
		ord.Class[A],
		data.Data[A],
	],
	MA monoid.Class[A],
	DA data.Data[A],
](fa FA, ma MA, ta DA) A
Example (Either)
package main

import (
	"fmt"

	"github.com/calebcase/base/data/either"
	"github.com/calebcase/base/data/foldable"
	"github.com/calebcase/base/data/list"
)

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

	ll := either.Left[list.List[int], list.List[int]]{
		Value: list.List[int]{42},
	}

	lt := list.NewType[int]()

	// FIXME: This should return empty list, but instead it returns the
	// same as Right.
	fmt.Println(foldable.Concat[list.List[int]](t, lt, ll))

}
Output:

[]
Example (Listlist)
package main

import (
	"fmt"

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

func main() {
	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]()

	fmt.Println(foldable.Concat[list.List[int]](t, lt, ll))

}
Output:

[1 2 3 4 5 6]
Example (Maybe)
package main

import (
	"fmt"

	"github.com/calebcase/base/data/foldable"
	"github.com/calebcase/base/data/list"
	"github.com/calebcase/base/data/maybe"
)

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

	jl := maybe.Just[list.List[int]]{
		Value: list.List[int]{1, 2, 3},
	}

	lt := list.NewType[int]()

	fmt.Println(foldable.Concat[list.List[int]](t, lt, jl))

}
Output:

[1 2 3]

Types

type Class

type Class[
	A any,
	B any,

	MA monoid.Class[A],
	MB monoid.Class[B],

	EA eq.Class[A],
	OA ord.Class[A],

	TA T[A],
] interface {
	Fold(MA, TA) A
	FoldMap(MB, func(A) B, TA) B

	FoldR(func(A, B) B, B, TA) B
	FoldL(func(B, A) B, B, TA) B

	ToList(TA) []A
	Null(TA) bool
	Length(TA) int

	Elem(EA, A, TA) bool

	Maximum(OA, TA) A
	Minimum(OA, TA) A
}

type T added in v0.0.6

type T[T any] interface{}

type Type

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

func NewType

func NewType[
	A any,
	B any,
]() Type[A, B]

func (Type[A, B]) Elem added in v0.0.6

func (t Type[A, B]) Elem(ea eq.Class[A], a A, ta data.Data[A]) bool
Example (Int)
package main

import (
	"fmt"

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

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

	l := list.List[int]{1, 2, 3}
	e := eq.NewType(eq.Comparable[int])

	fmt.Println(t.Elem(e, 2, l))
	fmt.Println(t.Elem(e, 10, l))

}
Output:

true
false
Example (String)
package main

import (
	"fmt"

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

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

	l := list.List[string]{
		"a",
		"needle",
		"in",
		"a",
		"haystack",
	}
	e := eq.NewType(eq.Comparable[string])

	fmt.Println(t.Elem(e, "needle", l))
	fmt.Println(t.Elem(e, "gold", l))

}
Output:

true
false

func (Type[A, B]) Fold

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

import (
	"fmt"

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

func main() {
	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]()

	fmt.Println(t.Fold(lt, ll))

}
Output:

[1 2 3 4 5 6]

func (Type[A, B]) FoldL

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

import (
	"fmt"

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

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

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

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

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

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

}
Output:

1, 2, 3

func (Type[A, B]) FoldMap

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

import (
	"fmt"

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

func main() {
	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]()

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

}
Output:

[1 2 3 4 5 6]

func (Type[A, B]) FoldR

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

import (
	"fmt"

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

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

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

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

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

}
Output:

6

func (Type[A, B]) Length added in v0.0.6

func (t Type[A, B]) Length(ta data.Data[A]) int
Example (List)
package main

import (
	"fmt"

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

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

	fmt.Println(t.Length(list.List[int]{1, 2, 3}))
	fmt.Println(t.Length(list.List[int]{}))

}
Output:

3
0
Example (Maybe)
package main

import (
	"fmt"

	"github.com/calebcase/base/data/foldable"
	"github.com/calebcase/base/data/maybe"
)

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

	fmt.Println(t.Length(maybe.Just[int]{Value: 2}))
	fmt.Println(t.Length(maybe.Nothing[int]{}))

}
Output:

1
0

func (Type[A, B]) Maximum added in v0.0.6

func (t Type[A, B]) Maximum(oa ord.Class[A], ta data.Data[A]) A
Example (Int)
package main

import (
	"fmt"

	"github.com/calebcase/base/data/foldable"
	"github.com/calebcase/base/data/list"
	"github.com/calebcase/base/data/ord"
)

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

	l := list.List[int]{1, 2, 3}
	o := ord.NewType(ord.Ordered[int])

	fmt.Println(t.Maximum(o, l))

}
Output:

3
Example (String)
package main

import (
	"fmt"

	"github.com/calebcase/base/data/foldable"
	"github.com/calebcase/base/data/list"
	"github.com/calebcase/base/data/ord"
)

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

	l := list.List[string]{"a", "b", "c"}
	o := ord.NewType(ord.Ordered[string])

	fmt.Println(t.Maximum(o, l))

}
Output:

c

func (Type[A, B]) Minimum added in v0.0.6

func (t Type[A, B]) Minimum(oa ord.Class[A], ta data.Data[A]) A
Example (Int)
package main

import (
	"fmt"

	"github.com/calebcase/base/data/foldable"
	"github.com/calebcase/base/data/list"
	"github.com/calebcase/base/data/ord"
)

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

	l := list.List[int]{1, 2, 3}
	o := ord.NewType(ord.Ordered[int])

	fmt.Println(t.Minimum(o, l))

}
Output:

1
Example (String)
package main

import (
	"fmt"

	"github.com/calebcase/base/data/foldable"
	"github.com/calebcase/base/data/list"
	"github.com/calebcase/base/data/ord"
)

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

	l := list.List[string]{"a", "b", "c"}
	o := ord.NewType(ord.Ordered[string])

	fmt.Println(t.Minimum(o, l))

}
Output:

a

func (Type[A, B]) Null added in v0.0.6

func (t Type[A, B]) Null(ta data.Data[A]) bool
Example (List)
package main

import (
	"fmt"

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

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

	fmt.Println(t.Null(list.List[int]{1, 2, 3}))
	fmt.Println(t.Null(list.List[int]{}))

}
Output:

false
true
Example (Maybe)
package main

import (
	"fmt"

	"github.com/calebcase/base/data/foldable"
	"github.com/calebcase/base/data/maybe"
)

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

	fmt.Println(t.Null(maybe.Just[int]{Value: 2}))
	fmt.Println(t.Null(maybe.Nothing[int]{}))

}
Output:

false
true

func (Type[A, B]) ToList added in v0.0.6

func (t Type[A, B]) ToList(ta data.Data[A]) (result []A)
Example
package main

import (
	"fmt"

	"github.com/calebcase/base/data/foldable"
	"github.com/calebcase/base/data/maybe"
)

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

	fmt.Println(t.ToList(maybe.Just[int]{Value: 2}))

}
Output:

[2]

Jump to

Keyboard shortcuts

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