mirror of
https://github.com/IBM/fp-go.git
synced 2025-08-10 22:31:32 +02:00
fix: bug in compact array (#87)
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
@@ -61,14 +61,14 @@ func SequenceArray[E, A any](ma []Either[E, A]) Either[E, []A] {
|
|||||||
return SequenceArrayG[[]A](ma)
|
return SequenceArrayG[[]A](ma)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CompactArrayG discards the none values and keeps the some values
|
// CompactArrayG discards the none values and keeps the right values
|
||||||
func CompactArrayG[A1 ~[]Either[E, A], A2 ~[]A, E, A any](fa A1) A2 {
|
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 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))
|
return MonadFold(value, F.Constant1[E](out), F.Bind1st(RA.Append[A2, A], out))
|
||||||
}, make(A2, len(fa)))
|
}, make(A2, 0, len(fa)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// CompactArray discards the none values and keeps the some values
|
// CompactArray discards the none values and keeps the right values
|
||||||
func CompactArray[E, A any](fa []Either[E, A]) []A {
|
func CompactArray[E, A any](fa []Either[E, A]) []A {
|
||||||
return CompactArrayG[[]Either[E, A], []A](fa)
|
return CompactArrayG[[]Either[E, A], []A](fa)
|
||||||
}
|
}
|
||||||
|
18
either/array_test.go
Normal file
18
either/array_test.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package either
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCompactArray(t *testing.T) {
|
||||||
|
ar := []Either[string, string]{
|
||||||
|
Of[string]("ok"),
|
||||||
|
Left[string]("err"),
|
||||||
|
Of[string]("ok"),
|
||||||
|
}
|
||||||
|
|
||||||
|
res := CompactArray(ar)
|
||||||
|
assert.Equal(t, 2, len(res))
|
||||||
|
}
|
@@ -65,7 +65,7 @@ func SequenceArray[A any](ma []Option[A]) Option[[]A] {
|
|||||||
func CompactArrayG[A1 ~[]Option[A], A2 ~[]A, A any](fa A1) A2 {
|
func CompactArrayG[A1 ~[]Option[A], A2 ~[]A, A any](fa A1) A2 {
|
||||||
return RA.Reduce(fa, func(out A2, value Option[A]) A2 {
|
return RA.Reduce(fa, func(out A2, value Option[A]) A2 {
|
||||||
return MonadFold(value, F.Constant(out), F.Bind1st(RA.Append[A2, A], out))
|
return MonadFold(value, F.Constant(out), F.Bind1st(RA.Append[A2, A], out))
|
||||||
}, make(A2, len(fa)))
|
}, make(A2, 0, len(fa)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// CompactArray discards the none values and keeps the some values
|
// CompactArray discards the none values and keeps the some values
|
||||||
|
@@ -34,3 +34,14 @@ func TestSequenceArray(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(t, res, Of([]int{1, 2}))
|
assert.Equal(t, res, Of([]int{1, 2}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCompactArray(t *testing.T) {
|
||||||
|
ar := []Option[string]{
|
||||||
|
Of("ok"),
|
||||||
|
None[string](),
|
||||||
|
Of("ok"),
|
||||||
|
}
|
||||||
|
|
||||||
|
res := CompactArray(ar)
|
||||||
|
assert.Equal(t, 2, len(res))
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user