-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadaptors.go
44 lines (40 loc) · 848 Bytes
/
adaptors.go
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
package oniichan
// Slice returns a slice with elements received from the input channel.
// TODO: Perhaps rename to Collect?
func Slice[T any](in <-chan T) []T {
var s []T
for e := range in {
s = append(s, e)
}
return s
}
// SliceFrom returns a slice containing the input arguments.
func SliceFrom[T any](in ...T) []T {
s := make([]T, len(in))
for i, e := range in {
s[i] = e
}
return s
}
// Chan creates a channel that will produce elements from the input slice.
func Chan[T any](in []T) <-chan T {
ch := make(chan T)
go func() {
for _, v := range in {
ch <- v
}
close(ch)
}()
return ch
}
// Chan creates a channel that will produce elements from the input arguments.
func ChanFrom[T any](in ...T) <-chan T {
ch := make(chan T)
go func() {
defer close(ch)
for _, v := range in {
ch <- v
}
}()
return ch
}