Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions it/op/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@ package op
func Add[V ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~string | ~float32 | ~float64](a, b V) V {
return a + b
}

// Ref returns a reference to the provided value.
func Ref[V any](v V) *V {
return &v
}

// Deref returns the value pointed to by the provided pointer.
func Deref[V any](v *V) V {
return *v
}
17 changes: 17 additions & 0 deletions it/op/op_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,20 @@ func ExampleAdd_string() {
fmt.Println(it.Fold(slices.Values([]string{"a", "b", "c"}), op.Add, ""))
// Output: abc
}

func ExampleRef() {
refs := slices.Collect(it.Map(slices.Values([]int{5, 6, 7}), op.Ref))
fmt.Println(*refs[0], *refs[1], *refs[2])
// Output: 5 6 7
}

func ExampleDeref() {
intRef := func(a int) *int {
return &a
}

values := slices.Values([]*int{intRef(4), intRef(5), intRef(6)})

fmt.Println(slices.Collect(it.Map(values, op.Deref)))
// Output: [4 5 6]
}
Loading