Skip to content

Commit a68e323

Browse files
committed
fix: add filterable to Either and Result
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
1 parent 61b9484 commit a68e323

16 files changed

Lines changed: 3330 additions & 33 deletions

File tree

v2/array/array.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/IBM/fp-go/v2/internal/array"
2222
M "github.com/IBM/fp-go/v2/monoid"
2323
"github.com/IBM/fp-go/v2/option"
24-
"github.com/IBM/fp-go/v2/tuple"
24+
"github.com/IBM/fp-go/v2/pair"
2525
)
2626

2727
// From constructs an array from a set of variadic arguments
@@ -163,11 +163,11 @@ func FilterMapWithIndex[A, B any](f func(int, A) Option[B]) Operator[A, B] {
163163
return G.FilterMapWithIndex[[]A, []B](f)
164164
}
165165

166-
// FilterChain maps an array with an iterating function that returns an [Option] of an array. It keeps only the Some values discarding the Nones and then flattens the result.
166+
// ChainOptionK maps an array with an iterating function that returns an [Option] of an array. It keeps only the Some values discarding the Nones and then flattens the result.
167167
//
168168
//go:inline
169-
func FilterChain[A, B any](f option.Kleisli[A, []B]) Operator[A, B] {
170-
return G.FilterChain[[]A](f)
169+
func ChainOptionK[A, B any](f option.Kleisli[A, []B]) Operator[A, B] {
170+
return G.ChainOptionK[[]A](f)
171171
}
172172

173173
// FilterMapRef filters an array using a predicate on pointers and maps the matching elements using a function on pointers.
@@ -453,15 +453,15 @@ func Size[A any](as []A) int {
453453
// the second contains elements for which it returns true.
454454
//
455455
//go:inline
456-
func MonadPartition[A any](as []A, pred func(A) bool) tuple.Tuple2[[]A, []A] {
456+
func MonadPartition[A any](as []A, pred func(A) bool) pair.Pair[[]A, []A] {
457457
return G.MonadPartition(as, pred)
458458
}
459459

460460
// Partition creates two new arrays out of one, the left result contains the elements
461461
// for which the predicate returns false, the right one those for which the predicate returns true
462462
//
463463
//go:inline
464-
func Partition[A any](pred func(A) bool) func([]A) tuple.Tuple2[[]A, []A] {
464+
func Partition[A any](pred func(A) bool) func([]A) pair.Pair[[]A, []A] {
465465
return G.Partition[[]A](pred)
466466
}
467467

v2/array/array_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
"github.com/IBM/fp-go/v2/internal/utils"
2525
N "github.com/IBM/fp-go/v2/number"
2626
O "github.com/IBM/fp-go/v2/option"
27+
"github.com/IBM/fp-go/v2/pair"
2728
S "github.com/IBM/fp-go/v2/string"
28-
T "github.com/IBM/fp-go/v2/tuple"
2929
"github.com/stretchr/testify/assert"
3030
)
3131

@@ -163,11 +163,11 @@ func TestPartition(t *testing.T) {
163163
return n > 2
164164
}
165165

166-
assert.Equal(t, T.MakeTuple2(Empty[int](), Empty[int]()), Partition(pred)(Empty[int]()))
167-
assert.Equal(t, T.MakeTuple2(From(1), From(3)), Partition(pred)(From(1, 3)))
166+
assert.Equal(t, pair.MakePair(Empty[int](), Empty[int]()), Partition(pred)(Empty[int]()))
167+
assert.Equal(t, pair.MakePair(From(1), From(3)), Partition(pred)(From(1, 3)))
168168
}
169169

170-
func TestFilterChain(t *testing.T) {
170+
func TestChainOptionK(t *testing.T) {
171171
src := From(1, 2, 3)
172172

173173
f := func(i int) O.Option[[]string] {
@@ -177,7 +177,7 @@ func TestFilterChain(t *testing.T) {
177177
return O.None[[]string]()
178178
}
179179

180-
res := FilterChain(f)(src)
180+
res := ChainOptionK(f)(src)
181181

182182
assert.Equal(t, From("a1", "b1", "a3", "b3"), res)
183183
}

v2/array/generic/array.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
FC "github.com/IBM/fp-go/v2/internal/functor"
2222
M "github.com/IBM/fp-go/v2/monoid"
2323
O "github.com/IBM/fp-go/v2/option"
24-
"github.com/IBM/fp-go/v2/tuple"
24+
"github.com/IBM/fp-go/v2/pair"
2525
)
2626

2727
// Of constructs a single element array
@@ -215,7 +215,7 @@ func Filter[AS ~[]A, PRED ~func(A) bool, A any](pred PRED) func(AS) AS {
215215
return FilterWithIndex[AS](F.Ignore1of2[int](pred))
216216
}
217217

218-
func FilterChain[GA ~[]A, GB ~[]B, A, B any](f func(a A) O.Option[GB]) func(GA) GB {
218+
func ChainOptionK[GA ~[]A, GB ~[]B, A, B any](f func(a A) O.Option[GB]) func(GA) GB {
219219
return F.Flow2(
220220
FilterMap[GA, []GB](f),
221221
Flatten[[]GB],
@@ -234,7 +234,7 @@ func FilterMapWithIndex[GA ~[]A, GB ~[]B, A, B any](f func(int, A) O.Option[B])
234234
return F.Bind2nd(MonadFilterMapWithIndex[GA, GB, A, B], f)
235235
}
236236

237-
func MonadPartition[GA ~[]A, A any](as GA, pred func(A) bool) tuple.Tuple2[GA, GA] {
237+
func MonadPartition[GA ~[]A, A any](as GA, pred func(A) bool) pair.Pair[GA, GA] {
238238
left := Empty[GA]()
239239
right := Empty[GA]()
240240
array.Reduce(as, func(c bool, a A) bool {
@@ -246,10 +246,10 @@ func MonadPartition[GA ~[]A, A any](as GA, pred func(A) bool) tuple.Tuple2[GA, G
246246
return c
247247
}, true)
248248
// returns the partition
249-
return tuple.MakeTuple2(left, right)
249+
return pair.MakePair(left, right)
250250
}
251251

252-
func Partition[GA ~[]A, A any](pred func(A) bool) func(GA) tuple.Tuple2[GA, GA] {
252+
func Partition[GA ~[]A, A any](pred func(A) bool) func(GA) pair.Pair[GA, GA] {
253253
return F.Bind2nd(MonadPartition[GA, A], pred)
254254
}
255255

v2/array/generic/zip.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package generic
1818
import (
1919
F "github.com/IBM/fp-go/v2/function"
2020
N "github.com/IBM/fp-go/v2/number"
21-
T "github.com/IBM/fp-go/v2/tuple"
21+
"github.com/IBM/fp-go/v2/pair"
2222
)
2323

2424
// ZipWith applies a function to pairs of elements at the same index in two arrays, collecting the results in a new array. If one
@@ -34,19 +34,19 @@ func ZipWith[AS ~[]A, BS ~[]B, CS ~[]C, FCT ~func(A, B) C, A, B, C any](fa AS, f
3434

3535
// Zip takes two arrays and returns an array of corresponding pairs. If one input array is short, excess elements of the
3636
// longer array are discarded
37-
func Zip[AS ~[]A, BS ~[]B, CS ~[]T.Tuple2[A, B], A, B any](fb BS) func(AS) CS {
38-
return F.Bind23of3(ZipWith[AS, BS, CS, func(A, B) T.Tuple2[A, B]])(fb, T.MakeTuple2[A, B])
37+
func Zip[AS ~[]A, BS ~[]B, CS ~[]pair.Pair[A, B], A, B any](fb BS) func(AS) CS {
38+
return F.Bind23of3(ZipWith[AS, BS, CS, func(A, B) pair.Pair[A, B]])(fb, pair.MakePair[A, B])
3939
}
4040

4141
// Unzip is the function is reverse of [Zip]. Takes an array of pairs and return two corresponding arrays
42-
func Unzip[AS ~[]A, BS ~[]B, CS ~[]T.Tuple2[A, B], A, B any](cs CS) T.Tuple2[AS, BS] {
42+
func Unzip[AS ~[]A, BS ~[]B, CS ~[]pair.Pair[A, B], A, B any](cs CS) pair.Pair[AS, BS] {
4343
l := len(cs)
4444
as := make(AS, l)
4545
bs := make(BS, l)
4646
for i := range l {
4747
t := cs[i]
48-
as[i] = t.F1
49-
bs[i] = t.F2
48+
as[i] = pair.Head(t)
49+
bs[i] = pair.Tail(t)
5050
}
51-
return T.MakeTuple2(as, bs)
51+
return pair.MakePair(as, bs)
5252
}

v2/array/zip.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package array
1717

1818
import (
1919
G "github.com/IBM/fp-go/v2/array/generic"
20-
T "github.com/IBM/fp-go/v2/tuple"
20+
"github.com/IBM/fp-go/v2/pair"
2121
)
2222

2323
// ZipWith applies a function to pairs of elements at the same index in two arrays,
@@ -55,8 +55,8 @@ func ZipWith[FCT ~func(A, B) C, A, B, C any](fa []A, fb []B, f FCT) []C {
5555
// // Result: [(a, 1), (b, 2)]
5656
//
5757
//go:inline
58-
func Zip[A, B any](fb []B) func([]A) []T.Tuple2[A, B] {
59-
return G.Zip[[]A, []B, []T.Tuple2[A, B]](fb)
58+
func Zip[A, B any](fb []B) func([]A) []pair.Pair[A, B] {
59+
return G.Zip[[]A, []B, []pair.Pair[A, B]](fb)
6060
}
6161

6262
// Unzip is the reverse of Zip. It takes an array of pairs (tuples) and returns
@@ -78,6 +78,6 @@ func Zip[A, B any](fb []B) func([]A) []T.Tuple2[A, B] {
7878
// ages := result.Tail // [30, 25, 35]
7979
//
8080
//go:inline
81-
func Unzip[A, B any](cs []T.Tuple2[A, B]) T.Tuple2[[]A, []B] {
81+
func Unzip[A, B any](cs []pair.Pair[A, B]) pair.Pair[[]A, []B] {
8282
return G.Unzip[[]A, []B](cs)
8383
}

v2/array/zip_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"fmt"
2020
"testing"
2121

22-
T "github.com/IBM/fp-go/v2/tuple"
22+
"github.com/IBM/fp-go/v2/pair"
2323
"github.com/stretchr/testify/assert"
2424
)
2525

@@ -40,7 +40,7 @@ func TestZip(t *testing.T) {
4040

4141
res := Zip[string](left)(right)
4242

43-
assert.Equal(t, From(T.MakeTuple2("a", 1), T.MakeTuple2("b", 2), T.MakeTuple2("c", 3)), res)
43+
assert.Equal(t, From(pair.MakePair("a", 1), pair.MakePair("b", 2), pair.MakePair("c", 3)), res)
4444
}
4545

4646
func TestUnzip(t *testing.T) {
@@ -51,6 +51,6 @@ func TestUnzip(t *testing.T) {
5151

5252
unzipped := Unzip(zipped)
5353

54-
assert.Equal(t, right, unzipped.F1)
55-
assert.Equal(t, left, unzipped.F2)
54+
assert.Equal(t, right, pair.Head(unzipped))
55+
assert.Equal(t, left, pair.Tail(unzipped))
5656
}

v2/di/erasure/injector.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ var (
8787
// assembleProviders constructs the provider map for item and non-item providers
8888
assembleProviders = F.Flow3(
8989
A.Partition(isItemProvider),
90-
T.Map2(collectProviders, collectItemProviders),
91-
T.Tupled2(mergeProviders.Concat),
90+
pair.BiMap(collectProviders, collectItemProviders),
91+
pair.Paired(mergeProviders.Concat),
9292
)
9393
)
9494

0 commit comments

Comments
 (0)