This repository was archived by the owner on Feb 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.go
More file actions
59 lines (53 loc) · 1.53 KB
/
map.go
File metadata and controls
59 lines (53 loc) · 1.53 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
package functools
import (
"strings"
"github.com/se-dev-pion/functools/types"
)
func Map4Slice[T, U any, E ~[]T, R []U](handler types.FuncT2R[T, U], entry E) types.FuncNone2T[R] {
return func() R {
output := make(R, len(entry))
for i, item := range entry {
output[i] = handler(item)
}
return output
}
}
func Map4String(handler types.FuncT2T[string], entry string) types.FuncNone2T[string] {
return func() string {
var builder strings.Builder
builder.Grow(len(entry))
for _, charCode := range entry {
builder.WriteString(handler(string(charCode)))
}
return builder.String()
}
}
func Map4Chan[T, U any, E ~chan T, R chan U](handler types.FuncT2R[T, U], entry E) types.FuncNone2T[R] {
return func() R {
output := make(R, cap(entry))
for _, item := range extractChanElements(entry) {
output <- handler(item)
}
return output
}
}
// Map creates a new types.Sequence composed of elements transformed from the input types.Sequence.
func Map[R types.Sequence[U] | ~string, T, U any, E types.Sequence[T] | ~string](handler types.FuncT2R[T, U], entry E) types.FuncNone2T[R] {
v := any(entry)
switch e := v.(type) {
case []T:
return any(Map4Slice(handler, e)).(types.FuncNone2T[R])
case string:
if _, ok := any(*new(T)).(string); !ok {
goto END
}
if _, ok := any(*new(U)).(string); !ok {
goto END
}
return any(Map4String(types.FuncT2T[string](any(handler).(types.FuncT2R[string, string])), e)).(types.FuncNone2T[R])
case chan T:
return any(Map4Chan(handler, e)).(types.FuncNone2T[R])
}
END:
return nil
}