/
cast.go
88 lines (69 loc) · 2.41 KB
/
cast.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
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
package fuego
// SC is a typed Stream cast function from a non-parameterised Stream[Any] to a parameterised Stream[U].
// SC receives a typed Stream[U].
//
// SC exists to address the current lack of support in Go for parameterised methods.
// See doc.go for more details.
//
// See C for A typed cast.
func SC[U any](from Stream[Any], to Stream[U]) Stream[U] {
toCh := make(chan U, from.concurrency)
to.stream = toCh
go func() {
defer close(to.stream)
for f := range from.stream {
to.stream <- interface{}(f).(U)
}
}()
return to
}
// C is a typed cast function from a non-parameterised Stream[Any] to a parameterised type Stream[U].
// C receives a type U and creates a Stream[U].
//
// C exists to address the current lack of support in Go for parameterised methods.
// See doc.go for more details.
//
// See SC for A Stream cast.
func C[U any](from Stream[Any], to U) Stream[U] {
toCh := make(chan U, from.concurrency)
toStream := NewConcurrentStream(toCh, from.concurrency)
go func() {
defer close(toStream.stream)
for f := range from.stream {
toStream.stream <- interface{}(f).(U)
}
}()
return toStream
}
// CC is a typed cast function from a non-parameterised Stream[Any] to a parameterised type ComparableStream[U].
// CC receives a type U and creates a ComparableStream[U].
//
// CC exists to address the current lack of support in Go for parameterised methods and a performance issue with Go 1.18.
// See doc.go for more details.
func CC[U Comparable](from Stream[Any], to U) ComparableStream[U] {
toCh := make(chan U, from.concurrency)
toStream := NewConcurrentStream(toCh, from.concurrency)
go func() {
defer close(toStream.stream)
for f := range from.stream {
toStream.stream <- interface{}(f).(U)
}
}()
return ComparableStream[U]{toStream}
}
// MC is a typed cast function from a non-parameterised Stream[Any] to a parameterised type MathableStream[U].
// MC receives a type U and creates a MathableStream[U].
//
// MC exists to address the current lack of support in Go for parameterised methods and a performance issue with Go 1.18.
// See doc.go for more details.
func MC[U Mathable](from Stream[Any], to U) MathableStream[U] {
toCh := make(chan U, from.concurrency)
toStream := NewConcurrentStream(toCh, from.concurrency)
go func() {
defer close(toStream.stream)
for f := range from.stream {
toStream.stream <- interface{}(f).(U)
}
}()
return MathableStream[U]{toStream}
}