77 "github.com/BooleanCat/go-functional/internal/assert"
88 "github.com/BooleanCat/go-functional/iter"
99 "github.com/BooleanCat/go-functional/iter/filters"
10+ "github.com/BooleanCat/go-functional/option"
1011)
1112
1213func ExampleFilter () {
@@ -21,6 +22,24 @@ func ExampleFilter() {
2122 // None
2223}
2324
25+ func ExampleFilterMap () {
26+ selectAndTripleOdds := func (x int ) option.Option [int ] {
27+ if x % 2 == 0 {
28+ return option .None [int ]()
29+ }
30+ return option .Some (x * 3 )
31+ }
32+
33+ triples := iter .FilterMap [int ](
34+ iter .Take [int ](iter .Count (), 6 ),
35+ selectAndTripleOdds ,
36+ )
37+
38+ fmt .Println (iter .Collect (triples ))
39+
40+ // Output: [3 9 15]
41+ }
42+
2443func ExampleExclude () {
2544 filtered := iter .Exclude [int ](iter .Lift ([]int {0 , 1 , 0 , 2 }), filters .IsZero [int ])
2645 fmt .Println (filtered .Next ())
@@ -52,3 +71,38 @@ func TestExclude(t *testing.T) {
5271 assert .Equal (t , evens .Next ().Unwrap (), 1 )
5372 assert .Equal (t , evens .Next ().Unwrap (), 3 )
5473}
74+
75+ func TestFilterMap (t * testing.T ) {
76+ selectEvenAndDouble := func (x int ) option.Option [int ] {
77+ if x % 2 > 0 {
78+ return option .None [int ]()
79+ }
80+
81+ return option .Some (x * 2 )
82+ }
83+
84+ fltMap := iter .FilterMap [int ](
85+ iter .Lift ([]int {1 , 2 , 3 , 4 , 5 , 6 }),
86+ selectEvenAndDouble ,
87+ )
88+ result := iter .Collect (fltMap )
89+
90+ assert .SliceEqual (t , result , []int {4 , 8 , 12 })
91+ }
92+
93+ func TestFilterMapEmpty (t * testing.T ) {
94+ selectEvenAndDouble := func (x int ) option.Option [int ] {
95+ if x % 2 > 0 {
96+ return option .None [int ]()
97+ }
98+
99+ return option .Some (x * 2 )
100+ }
101+
102+ fltMapEmpty := iter .FilterMap [int ](
103+ iter .Exhausted [int ](),
104+ selectEvenAndDouble ,
105+ )
106+
107+ assert .Empty (t , iter .Collect (fltMapEmpty ))
108+ }
0 commit comments