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:
33
internal/record/record.go
Normal file
33
internal/record/record.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package record
|
||||
|
||||
func Reduce[M ~map[K]V, K comparable, V, R any](r M, f func(R, V) R, initial R) R {
|
||||
current := initial
|
||||
for _, v := range r {
|
||||
current = f(current, v)
|
||||
}
|
||||
return current
|
||||
}
|
||||
|
||||
func ReduceWithIndex[M ~map[K]V, K comparable, V, R any](r M, f func(K, R, V) R, initial R) R {
|
||||
current := initial
|
||||
for k, v := range r {
|
||||
current = f(k, current, v)
|
||||
}
|
||||
return current
|
||||
}
|
||||
|
||||
func ReduceRef[M ~map[K]V, K comparable, V, R any](r M, f func(R, *V) R, initial R) R {
|
||||
current := initial
|
||||
for _, v := range r {
|
||||
current = f(current, &v) // #nosec G601
|
||||
}
|
||||
return current
|
||||
}
|
||||
|
||||
func ReduceRefWithIndex[M ~map[K]V, K comparable, V, R any](r M, f func(K, R, *V) R, initial R) R {
|
||||
current := initial
|
||||
for k, v := range r {
|
||||
current = f(k, current, &v) // #nosec G601
|
||||
}
|
||||
return current
|
||||
}
|
93
internal/record/traverse.go
Normal file
93
internal/record/traverse.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package record
|
||||
|
||||
import (
|
||||
F "github.com/ibm/fp-go/function"
|
||||
)
|
||||
|
||||
// createEmpty creates a new empty, read-write map
|
||||
// this is different to Empty which creates a new read-only empty map
|
||||
func createEmpty[N ~map[K]A, K comparable, A any]() N {
|
||||
return make(N)
|
||||
}
|
||||
|
||||
// inserts the key/value pair into a read-write map for performance
|
||||
// order of parameters is adjusted to be curryable
|
||||
func addKey[N ~map[K]A, K comparable, A any](key K, m N, value A) N {
|
||||
m[key] = value
|
||||
return m
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
We need to pass the members of the applicative explicitly, because golang does neither support higher kinded types nor template methods on structs or interfaces
|
||||
|
||||
HKTRB = HKT<MB>
|
||||
HKTA = HKT<A>
|
||||
HKTB = HKT<B>
|
||||
HKTAB = HKT<func(A)B>
|
||||
*/
|
||||
func traverseWithIndex[MA ~map[K]A, MB ~map[K]B, K comparable, A, B, HKTB, HKTAB, HKTRB any](
|
||||
_of func(MB) HKTRB,
|
||||
_map func(HKTRB, func(MB) func(B) MB) HKTAB,
|
||||
_ap func(HKTAB, HKTB) HKTRB,
|
||||
|
||||
ta MA, f func(K, A) HKTB) HKTRB {
|
||||
// this function inserts a value into a map with a given key
|
||||
cb := F.Curry3(addKey[MB, K, B])
|
||||
|
||||
return ReduceWithIndex(ta, func(k K, r HKTRB, a A) HKTRB {
|
||||
return _ap(
|
||||
_map(r, cb(k)),
|
||||
f(k, a),
|
||||
)
|
||||
}, _of(createEmpty[MB]()))
|
||||
}
|
||||
|
||||
func MonadTraverse[MA ~map[K]A, MB ~map[K]B, K comparable, A, B, HKTB, HKTAB, HKTRB any](
|
||||
_of func(MB) HKTRB,
|
||||
_map func(HKTRB, func(MB) func(B) MB) HKTAB,
|
||||
_ap func(HKTAB, HKTB) HKTRB,
|
||||
|
||||
r MA, f func(A) HKTB) HKTRB {
|
||||
return traverseWithIndex(_of, _map, _ap, r, F.Ignore1st[K](f))
|
||||
}
|
||||
|
||||
func TraverseWithIndex[MA ~map[K]A, MB ~map[K]B, K comparable, A, B, HKTB, HKTAB, HKTRB any](
|
||||
_of func(MB) HKTRB,
|
||||
_map func(HKTRB, func(MB) func(B) MB) HKTAB,
|
||||
_ap func(HKTAB, HKTB) HKTRB,
|
||||
|
||||
f func(K, A) HKTB) func(MA) HKTRB {
|
||||
|
||||
return func(ma MA) HKTRB {
|
||||
return traverseWithIndex(_of, _map, _ap, ma, f)
|
||||
}
|
||||
}
|
||||
|
||||
// HKTA = HKT<A>
|
||||
// HKTB = HKT<B>
|
||||
// HKTAB = HKT<func(A)B>
|
||||
// HKTRB = HKT<MB>
|
||||
func Traverse[MA ~map[K]A, MB ~map[K]B, K comparable, A, B, HKTB, HKTAB, HKTRB any](
|
||||
_of func(MB) HKTRB,
|
||||
_map func(HKTRB, func(MB) func(B) MB) HKTAB,
|
||||
_ap func(HKTAB, HKTB) HKTRB,
|
||||
|
||||
f func(A) HKTB) func(MA) HKTRB {
|
||||
|
||||
return func(ma MA) HKTRB {
|
||||
return MonadTraverse(_of, _map, _ap, ma, f)
|
||||
}
|
||||
}
|
||||
|
||||
// HKTA = HKT[A]
|
||||
// HKTAA = HKT[func(A)MA]
|
||||
// HKTRA = HKT[MA]
|
||||
func Sequence[MA ~map[K]A, MKTA ~map[K]HKTA, K comparable, A, HKTA, HKTAA, HKTRA any](
|
||||
_of func(MA) HKTRA,
|
||||
_map func(HKTRA, func(MA) func(A) MA) HKTAA,
|
||||
_ap func(HKTAA, HKTA) HKTRA,
|
||||
|
||||
ma MKTA) HKTRA {
|
||||
return MonadTraverse(_of, _map, _ap, ma, F.Identity[HKTA])
|
||||
}
|
Reference in New Issue
Block a user