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

fix: add compact methods

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-09-08 22:54:58 +02:00
parent cf1c886f48
commit 4f04344cda
7 changed files with 116 additions and 1 deletions

View File

@@ -44,3 +44,21 @@ func SequenceRecordG[GA ~map[K]A, GOA ~map[K]Option[A], K comparable, A any](ma
func SequenceRecord[K comparable, A any](ma map[K]Option[A]) Option[map[K]A] {
return SequenceRecordG[map[K]A](ma)
}
func upsertAtReadWrite[M ~map[K]V, K comparable, V any](r M, k K, v V) M {
r[k] = v
return r
}
// CompactRecordG discards the noe values and keeps the some values
func CompactRecordG[M1 ~map[K]Option[A], M2 ~map[K]A, K comparable, A any](m M1) M2 {
bnd := F.Bind12of3(upsertAtReadWrite[M2])
return RR.ReduceWithIndex(m, func(key K, m M2, value Option[A]) M2 {
return MonadFold(value, F.Constant(m), bnd(m, key))
}, make(M2))
}
// CompactRecord discards the noe values and keeps the some values
func CompactRecord[K comparable, A any](m map[K]Option[A]) map[K]A {
return CompactRecordG[map[K]Option[A], map[K]A](m)
}