1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-11-23 22:14:53 +02:00
Files
fp-go/v2/either/array.go
Dr. Carsten Leue 8e7fc699a1 fix: some performance optimizations
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2025-11-05 16:55:19 +01:00

165 lines
5.5 KiB
Go

// 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 either
import (
F "github.com/IBM/fp-go/v2/function"
RA "github.com/IBM/fp-go/v2/internal/array"
)
// TraverseArrayG transforms an array by applying a function that returns an Either to each element.
// If any element produces a Left, the entire result is that Left (short-circuits).
// Otherwise, returns Right containing the array of all Right values.
// The G suffix indicates support for generic slice types.
//
// Example:
//
// parse := func(s string) either.Either[error, int] {
// v, err := strconv.Atoi(s)
// return either.FromError(v, err)
// }
// result := either.TraverseArrayG[[]string, []int](parse)([]string{"1", "2", "3"})
// // result is Right([]int{1, 2, 3})
func TraverseArrayG[GA ~[]A, GB ~[]B, E, A, B any](f func(A) Either[E, B]) func(GA) Either[E, GB] {
return RA.Traverse[GA](
Of[E, GB],
Map[E, GB, func(B) GB],
Ap[GB, E, B],
f,
)
}
// TraverseArray transforms an array by applying a function that returns an Either to each element.
// If any element produces a Left, the entire result is that Left (short-circuits).
// Otherwise, returns Right containing the array of all Right values.
//
// Example:
//
// parse := func(s string) either.Either[error, int] {
// v, err := strconv.Atoi(s)
// return either.FromError(v, err)
// }
// result := either.TraverseArray(parse)([]string{"1", "2", "3"})
// // result is Right([]int{1, 2, 3})
func TraverseArray[E, A, B any](f func(A) Either[E, B]) func([]A) Either[E, []B] {
return TraverseArrayG[[]A, []B](f)
}
// TraverseArrayWithIndexG transforms an array by applying an indexed function that returns an Either.
// The function receives both the index and the element.
// If any element produces a Left, the entire result is that Left (short-circuits).
// The G suffix indicates support for generic slice types.
//
// Example:
//
// validate := func(i int, s string) either.Either[error, string] {
// if len(s) > 0 {
// return either.Right[error](fmt.Sprintf("%d:%s", i, s))
// }
// return either.Left[string](fmt.Errorf("empty at index %d", i))
// }
// result := either.TraverseArrayWithIndexG[[]string, []string](validate)([]string{"a", "b"})
// // result is Right([]string{"0:a", "1:b"})
func TraverseArrayWithIndexG[GA ~[]A, GB ~[]B, E, A, B any](f func(int, A) Either[E, B]) func(GA) Either[E, GB] {
return RA.TraverseWithIndex[GA](
Of[E, GB],
Map[E, GB, func(B) GB],
Ap[GB, E, B],
f,
)
}
// TraverseArrayWithIndex transforms an array by applying an indexed function that returns an Either.
// The function receives both the index and the element.
// If any element produces a Left, the entire result is that Left (short-circuits).
//
// Example:
//
// validate := func(i int, s string) either.Either[error, string] {
// if len(s) > 0 {
// return either.Right[error](fmt.Sprintf("%d:%s", i, s))
// }
// return either.Left[string](fmt.Errorf("empty at index %d", i))
// }
// result := either.TraverseArrayWithIndex(validate)([]string{"a", "b"})
// // result is Right([]string{"0:a", "1:b"})
func TraverseArrayWithIndex[E, A, B any](f func(int, A) Either[E, B]) func([]A) Either[E, []B] {
return TraverseArrayWithIndexG[[]A, []B](f)
}
//go:inline
func SequenceArrayG[GA ~[]A, GOA ~[]Either[E, A], E, A any](ma GOA) Either[E, GA] {
return TraverseArrayG[GOA, GA](F.Identity[Either[E, A]])(ma)
}
// SequenceArray converts a homogeneous sequence of Either into an Either of sequence.
// If any element is Left, returns that Left (short-circuits).
// Otherwise, returns Right containing all the Right values.
//
// Example:
//
// eithers := []either.Either[error, int]{
// either.Right[error](1),
// either.Right[error](2),
// either.Right[error](3),
// }
// result := either.SequenceArray(eithers)
// // result is Right([]int{1, 2, 3})
//
//go:inline
func SequenceArray[E, A any](ma []Either[E, A]) Either[E, []A] {
return SequenceArrayG[[]A](ma)
}
// CompactArrayG discards all Left values and keeps only the Right values.
// The G suffix indicates support for generic slice types.
//
// Example:
//
// eithers := []either.Either[error, int]{
// either.Right[error](1),
// either.Left[int](errors.New("error")),
// either.Right[error](3),
// }
// result := either.CompactArrayG[[]either.Either[error, int], []int](eithers)
// // result is []int{1, 3}
//
//go:inline
func CompactArrayG[A1 ~[]Either[E, A], A2 ~[]A, E, A any](fa A1) A2 {
return RA.Reduce(fa, func(out A2, value Either[E, A]) A2 {
return MonadFold(value, F.Constant1[E](out), F.Bind1st(RA.Append[A2, A], out))
}, make(A2, 0, len(fa)))
}
// CompactArray discards all Left values and keeps only the Right values.
//
// Example:
//
// eithers := []either.Either[error, int]{
// either.Right[error](1),
// either.Left[int](errors.New("error")),
// either.Right[error](3),
// }
// result := either.CompactArray(eithers)
// // result is []int{1, 3}
//
//go:inline
func CompactArray[E, A any](fa []Either[E, A]) []A {
return CompactArrayG[[]Either[E, A], []A](fa)
}