forked from bcmills/go2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.go
45 lines (37 loc) · 1.26 KB
/
map.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
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build ignore
// map illustrates the awkwardness of functional APIs in the absence of
// assignability and/or subtyping constraints.
package main
// Map returns a new slice containing the result of applying f to each element
// in src.
func Map[T1, T2 any](src []T1, f func(T1) T2) []T2 {
dst := make([]T2, 0, len(src))
return AppendMapped(dst, src, f)
}
// AppendMapped applies f to each element in src, appending each result to dst
// and returning the resulting slice.
func AppendMapped[T1, T2 any](dst []T2, src []T1, f func(T1) T2) []T2 {
for _, x := range src {
dst = append(dst, f(x))
}
return dst
}
func recv[T any](x <-chan T) T {
return <-x
}
func main() {
var chans []chan int
// To map the recv function over a slice of bidirectional channels,
// we need to wrap it: even though the element type "chan int"
// is assignable to the argument type, the two function types are distinct.
//
// (There is no way for Map to declare an argument type “assignable from T1”,
// so it must use “exactly T1” instead.)
vals := Map(chans, func(x chan int) int {
return recv(x)
})
_ = vals
}