mirror of
https://github.com/IBM/fp-go.git
synced 2025-08-10 22:31:32 +02:00
fix: add missing withIndex methods
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
@@ -42,6 +42,16 @@ func TraverseArray[GB ~func() B, GBS ~func() BBS, AAS ~[]A, BBS ~[]B, A, B any](
|
||||
)
|
||||
}
|
||||
|
||||
func TraverseArrayWithIndex[GB ~func() B, GBS ~func() BBS, AAS ~[]A, BBS ~[]B, A, B any](f func(int, A) GB) func(AAS) GBS {
|
||||
return RA.TraverseWithIndex[AAS](
|
||||
Of[GBS, BBS],
|
||||
Map[GBS, func() func(B) BBS, BBS, func(B) BBS],
|
||||
Ap[GBS, func() func(B) BBS, GB],
|
||||
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
func SequenceArray[GA ~func() A, GAS ~func() AAS, AAS ~[]A, GAAS ~[]GA, A any](tas GAAS) GAS {
|
||||
return MonadTraverseArray[GA, GAS](tas, F.Identity[GA])
|
||||
}
|
||||
@@ -66,6 +76,16 @@ func TraverseRecord[GB ~func() B, GBS ~func() MB, MA ~map[K]A, MB ~map[K]B, K co
|
||||
)
|
||||
}
|
||||
|
||||
// TraverseRecordWithIndex transforms a record using an IO transform an IO of a record
|
||||
func TraverseRecordWithIndex[GB ~func() B, GBS ~func() MB, MA ~map[K]A, MB ~map[K]B, K comparable, A, B any](f func(K, A) GB) func(MA) GBS {
|
||||
return RR.TraverseWithIndex[MA](
|
||||
Of[GBS, MB],
|
||||
Map[GBS, func() func(B) MB, MB, func(B) MB],
|
||||
Ap[GBS, func() func(B) MB, GB],
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
func SequenceRecord[GA ~func() A, GAS ~func() AAS, AAS ~map[K]A, GAAS ~map[K]GA, K comparable, A any](tas GAAS) GAS {
|
||||
return MonadTraverseRecord[GA, GAS](tas, F.Identity[GA])
|
||||
}
|
||||
|
@@ -29,6 +29,12 @@ func TraverseArray[A, B any](f func(A) IO[B]) func([]A) IO[[]B] {
|
||||
return G.TraverseArray[IO[B], IO[[]B], []A](f)
|
||||
}
|
||||
|
||||
// TraverseArrayWithIndex applies a function returning an [IO] to all elements in an array and the
|
||||
// transforms this into an [IO] of that array
|
||||
func TraverseArrayWithIndex[A, B any](f func(int, A) IO[B]) func([]A) IO[[]B] {
|
||||
return G.TraverseArrayWithIndex[IO[B], IO[[]B], []A](f)
|
||||
}
|
||||
|
||||
// SequenceArray converts an array of [IO] to an [IO] of an array
|
||||
func SequenceArray[A any](tas []IO[A]) IO[[]A] {
|
||||
return G.SequenceArray[IO[A], IO[[]A]](tas)
|
||||
@@ -38,12 +44,18 @@ func MonadTraverseRecord[K comparable, A, B any](tas map[K]A, f func(A) IO[B]) I
|
||||
return G.MonadTraverseRecord[IO[B], IO[map[K]B]](tas, f)
|
||||
}
|
||||
|
||||
// TraverseArray applies a function returning an [IO] to all elements in a record and the
|
||||
// TraverseRecord applies a function returning an [IO] to all elements in a record and the
|
||||
// transforms this into an [IO] of that record
|
||||
func TraverseRecord[K comparable, A, B any](f func(A) IO[B]) func(map[K]A) IO[map[K]B] {
|
||||
return G.TraverseRecord[IO[B], IO[map[K]B], map[K]A](f)
|
||||
}
|
||||
|
||||
// TraverseRecordWithIndex applies a function returning an [IO] to all elements in a record and the
|
||||
// transforms this into an [IO] of that record
|
||||
func TraverseRecordWithIndex[K comparable, A, B any](f func(K, A) IO[B]) func(map[K]A) IO[map[K]B] {
|
||||
return G.TraverseRecordWithIndex[IO[B], IO[map[K]B], map[K]A](f)
|
||||
}
|
||||
|
||||
// SequenceRecord converts a record of [IO] to an [IO] of a record
|
||||
func SequenceRecord[K comparable, A any](tas map[K]IO[A]) IO[map[K]A] {
|
||||
return G.SequenceRecord[IO[A], IO[map[K]A]](tas)
|
||||
|
Reference in New Issue
Block a user