1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-07-15 01:24:23 +02:00
Files
.github
array
bounded
bytes
cli
constant
constraints
context
ioeither
reader
readereither
readerio
readerioeither
data
exec
file
generic
cancel.go
eq.go
gen.go
reader.go
resource.go
traverse.go
http
cancel.go
doc.go
eq.go
gen.go
reader.go
reader_test.go
resource.go
resource_test.go
traverse.go
type.go
doc.go
either
endomorphism
eq
erasure
errors
exec
function
http
identity
internal
io
ioeither
iooption
iterator
json
lambda
lazy
logging
magma
monoid
number
optics
option
ord
predicate
reader
readereither
readerio
readerioeither
record
resources
retry
samples
semigroup
string
tuple
writer
.gitignore
.releaserc
LICENSE
README.md
go.mod
go.sum
main.go
renovate.json
fp-go/context/readerioeither/generic/traverse.go

130 lines
3.6 KiB
Go
Raw Normal View History

// Copyright (c) 2023 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package generic
import (
"context"
E "github.com/IBM/fp-go/either"
F "github.com/IBM/fp-go/function"
RA "github.com/IBM/fp-go/internal/array"
RR "github.com/IBM/fp-go/internal/record"
)
// MonadTraverseArray transforms an array
func MonadTraverseArray[
AS ~[]A,
GRBS ~func(context.Context) GIOBS,
GRB ~func(context.Context) GIOB,
GIOBS ~func() E.Either[error, BS],
GIOB ~func() E.Either[error, B],
BS ~[]B,
A, B any](as AS, f func(A) GRB) GRBS {
return RA.MonadTraverse[AS](
Of[GRBS, GIOBS, BS],
Map[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GIOBS, func() E.Either[error, func(B) BS], BS, func(B) BS],
Ap[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GRB],
as, f,
)
}
// TraverseArray transforms an array
func TraverseArray[
AS ~[]A,
GRBS ~func(context.Context) GIOBS,
GRB ~func(context.Context) GIOB,
GIOBS ~func() E.Either[error, BS],
GIOB ~func() E.Either[error, B],
BS ~[]B,
A, B any](f func(A) GRB) func(AS) GRBS {
return RA.Traverse[AS](
Of[GRBS, GIOBS, BS],
Map[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GIOBS, func() E.Either[error, func(B) BS], BS, func(B) BS],
Ap[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GRB],
f,
)
}
// SequenceArray converts a homogeneous sequence of either into an either of sequence
func SequenceArray[
AS ~[]A,
GAS ~[]GRA,
GRAS ~func(context.Context) GIOAS,
GRA ~func(context.Context) GIOA,
GIOAS ~func() E.Either[error, AS],
GIOA ~func() E.Either[error, A],
A any](ma GAS) GRAS {
return MonadTraverseArray[GAS, GRAS](ma, F.Identity[GRA])
}
// MonadTraverseRecord transforms a record
func MonadTraverseRecord[K comparable,
AS ~map[K]A,
GRBS ~func(context.Context) GIOBS,
GRB ~func(context.Context) GIOB,
GIOBS ~func() E.Either[error, BS],
GIOB ~func() E.Either[error, B],
BS ~map[K]B,
A, B any](ma AS, f func(A) GRB) GRBS {
return RR.MonadTraverse[AS](
Of[GRBS, GIOBS, BS],
Map[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GIOBS, func() E.Either[error, func(B) BS], BS, func(B) BS],
Ap[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GRB],
ma, f,
)
}
// TraverseRecord transforms a record
func TraverseRecord[K comparable,
AS ~map[K]A,
GRBS ~func(context.Context) GIOBS,
GRB ~func(context.Context) GIOB,
GIOBS ~func() E.Either[error, BS],
GIOB ~func() E.Either[error, B],
BS ~map[K]B,
A, B any](f func(A) GRB) func(AS) GRBS {
return RR.Traverse[AS](
Of[GRBS, GIOBS, BS],
Map[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GIOBS, func() E.Either[error, func(B) BS], BS, func(B) BS],
Ap[GRBS, func(context.Context) func() E.Either[error, func(B) BS], GRB],
f,
)
}
// SequenceRecord converts a homogeneous sequence of either into an either of sequence
func SequenceRecord[K comparable,
AS ~map[K]A,
GAS ~map[K]GRA,
GRAS ~func(context.Context) GIOAS,
GRA ~func(context.Context) GIOA,
GIOAS ~func() E.Either[error, AS],
GIOA ~func() E.Either[error, A],
A any](ma GAS) GRAS {
return MonadTraverseRecord[K, GAS, GRAS](ma, F.Identity[GRA])
}