Skip to content

Commit 0a02284

Browse files
committed
doc: add doc and examples for lom.Filter
1 parent d406060 commit 0a02284

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,15 @@ Mutable: like `lo.Filter()`, but the slice is updated in place.
334334
import lom "github.com/samber/lo/mutable"
335335

336336
list := []int{1, 2, 3, 4}
337-
lom.Filter(list, func(x int) bool {
337+
newList := lom.Filter(list, func(x int) bool {
338338
return x%2 == 0
339339
})
340-
// []int{2, 4, 6, 8}
340+
341+
list
342+
// []int{2, 4, 3, 4}
343+
344+
newList
345+
// []int{2, 4}
341346
```
342347

343348
### Map

mutable/slice_example_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,32 @@ package mutable
22

33
import "fmt"
44

5+
func ExampleFilter() {
6+
list := []int{1, 2, 3, 4}
7+
8+
newList := Filter(list, func(nbr int) bool {
9+
return nbr%2 == 0
10+
})
11+
12+
fmt.Printf("%v\n%v", list, newList)
13+
// Output:
14+
// [2 4 3 4]
15+
// [2 4]
16+
}
17+
18+
func ExampleFilterI() {
19+
list := []int{1, 2, 3, 4}
20+
21+
newList := Filter(list, func(nbr int) bool {
22+
return nbr%2 == 0
23+
})
24+
25+
fmt.Printf("%v\n%v", list, newList)
26+
// Output:
27+
// [2 4 3 4]
28+
// [2 4]
29+
}
30+
531
func ExampleMap() {
632
list := []int{1, 2, 3, 4}
733

mutable/slice_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ func TestFilter(t *testing.T) {
1010
t.Parallel()
1111
is := assert.New(t)
1212

13-
r1 := Filter([]int{1, 2, 3, 4}, func(x int) bool {
13+
input1 := []int{1, 2, 3, 4}
14+
r1 := Filter(input1, func(x int) bool {
1415
return x%2 == 0
1516
})
1617

18+
is.Equal(input1, []int{2, 4, 3, 4})
1719
is.Equal(r1, []int{2, 4})
1820

19-
r2 := Filter([]string{"", "foo", "", "bar", ""}, func(x string) bool {
21+
input2 := []string{"", "foo", "", "bar", ""}
22+
r2 := Filter(input2, func(x string) bool {
2023
return len(x) > 0
2124
})
2225

26+
is.Equal(input2, []string{"foo", "bar", "", "bar", ""})
2327
is.Equal(r2, []string{"foo", "bar"})
2428
}
2529

0 commit comments

Comments
 (0)