Skip to content

Commit 52dbd47

Browse files
committed
Provide ref and deref ops for Map
1 parent b5f639d commit 52dbd47

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

it/op/op.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,13 @@ package op
44
func Add[V ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~string | ~float32 | ~float64](a, b V) V {
55
return a + b
66
}
7+
8+
// Ref returns a reference to the provided value.
9+
func Ref[V any](v V) *V {
10+
return &v
11+
}
12+
13+
// Deref returns the value pointed to by the provided pointer.
14+
func Deref[V any](v *V) V {
15+
return *v
16+
}

it/op/op_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,20 @@ func ExampleAdd_string() {
1717
fmt.Println(it.Fold(slices.Values([]string{"a", "b", "c"}), op.Add, ""))
1818
// Output: abc
1919
}
20+
21+
func ExampleRef() {
22+
refs := slices.Collect(it.Map(slices.Values([]int{5, 6, 7}), op.Ref))
23+
fmt.Println(*refs[0], *refs[1], *refs[2])
24+
// Output: 5 6 7
25+
}
26+
27+
func ExampleDeref() {
28+
intRef := func(a int) *int {
29+
return &a
30+
}
31+
32+
values := slices.Values([]*int{intRef(4), intRef(5), intRef(6)})
33+
34+
fmt.Println(slices.Collect(it.Map(values, op.Deref)))
35+
// Output: [4 5 6]
36+
}

0 commit comments

Comments
 (0)