1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-08-10 22:31:32 +02:00

initial checkin

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-07-07 22:31:06 +02:00
parent 71c47ca560
commit c07df5c771
128 changed files with 5827 additions and 2 deletions

23
semigroup/apply.go Normal file
View File

@@ -0,0 +1,23 @@
package semigroup
import (
F "github.com/ibm/fp-go/function"
)
/*
*
HKTA = HKT<A>
HKTFA = HKT<func(A)A>
*/
func ApplySemigroup[A, HKTA, HKTFA any](
_map func(HKTA, func(A) func(A) A) HKTFA,
_ap func(HKTFA, HKTA) HKTA,
s Semigroup[A],
) Semigroup[HKTA] {
cb := F.Curry2(s.Concat)
return MakeSemigroup(func(first HKTA, second HKTA) HKTA {
return _ap(_map(first, cb), second)
})
}

21
semigroup/array.go Normal file
View File

@@ -0,0 +1,21 @@
package semigroup
import (
M "github.com/ibm/fp-go/magma"
)
func GenericMonadConcatAll[GA ~[]A, A any](s Semigroup[A]) func(GA, A) A {
return M.GenericMonadConcatAll[GA](M.MakeMagma(s.Concat))
}
func GenericConcatAll[GA ~[]A, A any](s Semigroup[A]) func(A) func(GA) A {
return M.GenericConcatAll[GA](M.MakeMagma(s.Concat))
}
func MonadConcatAll[A any](s Semigroup[A]) func([]A, A) A {
return GenericMonadConcatAll[[]A](s)
}
func ConcatAll[A any](s Semigroup[A]) func(A) func([]A) A {
return GenericConcatAll[[]A](s)
}

View File

@@ -0,0 +1,16 @@
package ord
import (
O "github.com/ibm/fp-go/ord"
S "github.com/ibm/fp-go/semigroup"
)
// Max gets a semigroup where `concat` will return the maximum, based on the provided order.
func Max[A any](o O.Ord[A]) S.Semigroup[A] {
return S.MakeSemigroup(O.Max(o))
}
// Min gets a semigroup where `concat` will return the minimum, based on the provided order.
func Min[A any](o O.Ord[A]) S.Semigroup[A] {
return S.MakeSemigroup(O.Min(o))
}

46
semigroup/semigroup.go Normal file
View File

@@ -0,0 +1,46 @@
package semigroup
import (
F "github.com/ibm/fp-go/function"
M "github.com/ibm/fp-go/magma"
)
type Semigroup[A any] interface {
M.Magma[A]
}
type semigroup[A any] struct {
c func(A, A) A
}
func (self semigroup[A]) Concat(x A, y A) A {
return self.c(x, y)
}
func MakeSemigroup[A any](c func(A, A) A) Semigroup[A] {
return semigroup[A]{c: c}
}
// Reverse returns The dual of a `Semigroup`, obtained by swapping the arguments of `concat`.
func Reverse[A any](m Semigroup[A]) Semigroup[A] {
return MakeSemigroup(M.Reverse[A](m).Concat)
}
// FunctionSemigroup forms a semigroup as long as you can provide a semigroup for the codomain.
func FunctionSemigroup[A, B any](S Semigroup[B]) Semigroup[func(A) B] {
return MakeSemigroup(func(f func(A) B, g func(A) B) func(A) B {
return func(a A) B {
return S.Concat(f(a), g(a))
}
})
}
// First always returns the first argument.
func First[A any]() Semigroup[A] {
return MakeSemigroup(F.First[A, A])
}
// Last always returns the last argument.
func Last[A any]() Semigroup[A] {
return MakeSemigroup(F.Second[A, A])
}

View File

@@ -0,0 +1,21 @@
package semigroup
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFirst(t *testing.T) {
first := First[int]()
assert.Equal(t, 1, first.Concat(1, 2))
}
func TestLast(t *testing.T) {
last := Last[int]()
assert.Equal(t, 2, last.Concat(1, 2))
}