-
-
Notifications
You must be signed in to change notification settings - Fork 953
Expand file tree
/
Copy pathtype_manipulation.go
More file actions
89 lines (76 loc) 路 2.52 KB
/
Copy pathtype_manipulation.go
File metadata and controls
89 lines (76 loc) 路 2.52 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//go:build go1.23
package it
import (
"iter"
"github.com/samber/lo"
)
// ToSeqPtr returns a sequence of pointers to each value.
// Play: https://go.dev/play/p/70BcKpDcOKm
func ToSeqPtr[T any](collection iter.Seq[T]) iter.Seq[*T] {
return Map(collection, lo.ToPtr)
}
// FromSeqPtr returns a sequence with the pointer values.
// Returns a zero value in case of a nil pointer element.
// Play: https://go.dev/play/p/_eO6scpLcBF
func FromSeqPtr[T any](collection iter.Seq[*T]) iter.Seq[T] {
return Map(collection, lo.FromPtr)
}
// FromSeqPtrOr returns a sequence with the pointer values or the fallback value.
// Play: https://go.dev/play/p/LJ17S4pvOjo
func FromSeqPtrOr[T any](collection iter.Seq[*T], fallback T) iter.Seq[T] {
return Map(collection, func(x *T) T { return lo.FromPtrOr(x, fallback) })
}
// ToAnySeq returns a sequence with all elements mapped to `any` type.
// Play: https://go.dev/play/p/ktE4IMXDMxv
func ToAnySeq[T any](collection iter.Seq[T]) iter.Seq[any] {
return Map(collection, func(x T) any { return x })
}
// FromAnySeq returns a sequence with all elements mapped to a type.
// Panics on type conversion failure.
// Play: https://go.dev/play/p/wnOma1j5Uzu
func FromAnySeq[T any](collection iter.Seq[any]) iter.Seq[T] {
return Map(collection, func(item any) T {
if t, ok := item.(T); ok {
return t
}
panic("it.FromAnySeq: type conversion failed")
})
}
// Empty returns an empty sequence.
// Play: https://go.dev/play/p/Y9yplM3vGFe
func Empty[T any]() iter.Seq[T] {
return func(yield func(T) bool) {}
}
// IsEmpty returns true if the sequence is empty.
// Will iterate at most once.
// Play: https://go.dev/play/p/krZ-laaVi2C
func IsEmpty[T any](collection iter.Seq[T]) bool {
for range collection {
return false
}
return true
}
// IsNotEmpty returns true if the sequence is not empty.
// Will iterate at most once.
// Play: https://go.dev/play/p/jMAjbDvrdPu
func IsNotEmpty[T any](collection iter.Seq[T]) bool {
return !IsEmpty(collection)
}
// CoalesceSeq returns the first non-empty sequence.
// Will iterate through each sub-sequence at most once.
// Play: https://go.dev/play/p/wep4z2KJLCO
func CoalesceSeq[T any](v ...iter.Seq[T]) (iter.Seq[T], bool) {
for i := range v {
for range v[i] {
return v[i], true
}
}
return Empty[T](), false
}
// CoalesceSeqOrEmpty returns the first non-empty sequence.
// Will iterate through each sub-sequence at most once.
// Play: https://go.dev/play/p/3KZPAFRQr8B
func CoalesceSeqOrEmpty[T any](v ...iter.Seq[T]) iter.Seq[T] {
result, _ := CoalesceSeq(v...)
return result
}