Skip to content

Commit d8002c2

Browse files
committed
Implement ops.Unwrap{Option|Result}
1 parent 207868b commit d8002c2

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

iter/filters/filters.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,31 @@ package filters
22

33
import "constraints"
44

5+
// IsZero is a filter intended for use with iter.Filter that returns true when
6+
// the provided value is equal to its zero value.
57
func IsZero[T comparable](t T) bool {
68
var u T
79
return t == u
810
}
911

12+
// GreaterThan creates a filter for use with iter.Filter that returns true when
13+
// a value is greater than the provided threshold.
1014
func GreaterThan[T constraints.Ordered](t T) func(T) bool {
1115
return func(s T) bool {
1216
return s > t
1317
}
1418
}
1519

20+
// LessThan creates a filter for use with iter.Filter that returns true when a
21+
// value is less than the provided threshold.
1622
func LessThan[T constraints.Ordered](t T) func(T) bool {
1723
return func(s T) bool {
1824
return s < t
1925
}
2026
}
2127

28+
// And aggregates multiple filters for use with iter.Filter (and iter.Exclude)
29+
// to create a filter that returns true when all provided filters return true.
2230
func And[T constraints.Ordered](filters ...func(T) bool) func(T) bool {
2331
return func(t T) bool {
2432
for _, filter := range filters {

iter/ops/ops.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package ops
2+
3+
import (
4+
"github.com/BooleanCat/go-functional/option"
5+
"github.com/BooleanCat/go-functional/result"
6+
)
7+
8+
// UnwrapOption may be used as an operation for iter.Map in order to unwrap
9+
// all options in an iterator.
10+
func UnwrapOption[T any](o option.Option[T]) T {
11+
return o.Unwrap()
12+
}
13+
14+
// UnwrapResult may be used as an operation for iter.Map in order to unwrap
15+
// all results in an iterator.
16+
func UnwrapResult[T any](r result.Result[T]) T {
17+
return r.Unwrap()
18+
}

iter/ops/ops_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package ops_test
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/BooleanCat/go-functional/internal/assert"
9+
"github.com/BooleanCat/go-functional/iter"
10+
"github.com/BooleanCat/go-functional/iter/ops"
11+
"github.com/BooleanCat/go-functional/option"
12+
"github.com/BooleanCat/go-functional/result"
13+
)
14+
15+
func TestUnwrapOption(t *testing.T) {
16+
options := iter.Lift([]option.Option[int]{
17+
option.Some(4),
18+
option.Some(6),
19+
option.Some(-1),
20+
})
21+
22+
integers := iter.Map[option.Option[int]](options, ops.UnwrapOption[int])
23+
24+
assert.SliceEqual(t, iter.Collect[int](integers), []int{4, 6, -1})
25+
}
26+
27+
func TestUnwrapOptionPanic(t *testing.T) {
28+
defer func() {
29+
assert.Equal(t, fmt.Sprint(recover()), "called `Option.Unwrap()` on a `None` value")
30+
}()
31+
32+
iter.Collect[int](
33+
iter.Map[option.Option[int]](
34+
iter.Lift(
35+
[]option.Option[int]{option.None[int]()},
36+
),
37+
ops.UnwrapOption[int],
38+
),
39+
)
40+
41+
t.Error("did not panic")
42+
}
43+
44+
func TestUnwrapResult(t *testing.T) {
45+
results := iter.Lift([]result.Result[int]{
46+
result.Ok(4),
47+
result.Ok(6),
48+
result.Ok(-1),
49+
})
50+
51+
integers := iter.Map[result.Result[int]](results, ops.UnwrapResult[int])
52+
53+
assert.SliceEqual(t, iter.Collect[int](integers), []int{4, 6, -1})
54+
}
55+
56+
func TestUnwrapResultPanic(t *testing.T) {
57+
defer func() {
58+
assert.Equal(t, fmt.Sprint(recover()), "called `Result.Unwrap()` on an `Err` value")
59+
}()
60+
61+
iter.Collect[int](
62+
iter.Map[result.Result[int]](
63+
iter.Lift(
64+
[]result.Result[int]{result.Err[int](errors.New("oops"))},
65+
),
66+
ops.UnwrapResult[int],
67+
),
68+
)
69+
70+
t.Error("did not panic")
71+
}

0 commit comments

Comments
 (0)