-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
53 lines (48 loc) · 1.39 KB
/
map.go
File metadata and controls
53 lines (48 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package xiter
import (
"iter"
)
// Map applies the provided function to each element and returns a new sequence
// with the results.
func Map[E1, E2 any](s iter.Seq[E1], f func(E1) E2) iter.Seq[E2] {
return func(yield func(E2) bool) {
for e := range s {
if !yield(f(e)) {
return
}
}
}
}
// Map2 applies the provided function to each element of a key/value sequence
// and returns a new key/value sequence with the results.
func Map2[K1, V1, K2, V2 any](s iter.Seq2[K1, V1], f func(K1, V1) (K2, V2)) iter.Seq2[K2, V2] {
return func(yield func(K2, V2) bool) {
for k, v := range s {
if !yield(f(k, v)) {
return
}
}
}
}
// MapWhile applies the provided function to each element and returns a new sequence
// with the results, until the function returns false.
func MapWhile[E1, E2 any](s iter.Seq[E1], f func(E1) (E2, bool)) iter.Seq[E2] {
return func(yield func(E2) bool) {
for e1 := range s {
if e2, ok := f(e1); !ok || !yield(e2) {
return
}
}
}
}
// MapWhile2 applies the provided function to each element of a key/value sequence
// and returns a new key/value sequence with the results, until the function returns false.
func MapWhile2[K1, V1, K2, V2 any](s iter.Seq2[K1, V1], f func(K1, V1) (K2, V2, bool)) iter.Seq2[K2, V2] {
return func(yield func(K2, V2) bool) {
for k1, v1 := range s {
if k2, v2, ok := f(k1, v1); !ok || !yield(k2, v2) {
return
}
}
}
}