Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Any ¶
Any returns true if the predicate is true for any of the elements in the slice of fields of a struct. For example:
- Any([]int{1, 2, 3}, func(x any) bool { return x.(int) == 2 }) = true
- Any([]int{1, 2, 3}, func(x any) bool { return x.(int) == 4 }) = false
Example (Struct) ¶
xs := &AStruct{1, "2", 3}
f := func(x any) bool {
switch x := x.(type) {
case int:
return x == 2
case string:
return x == "2"
}
return false
}
isAny := Any(xs, f)
// isAny = true
fmt.Println(isAny)
Output: true
func Map ¶
Map maps a function over a the elements of a slice, the fields of a struct or the values of a map. For example:
- Map([]int{1, 2, 3}, func(x any) any { return x.(int) + 1 }).([]int) = []int{2,3,4}
- See Tests for an example of how to map over a struct
The following is not possible, because we can only Map back to the same types:
- Map([]{1,2,3}, func(x int) string { return fmt.Sprintf("%d", x) }) = []{"1","2","3"}
Example ¶
xs := []int{1, 2, 3}
f := func(x any) any { return x.(int) + 1 }
ys := Map(xs, f).([]int)
fmt.Println(ys)
Output: [2 3 4]
Example (Struct) ¶
xs := &A{1, "1"}
f := func(x any) any {
switch x := x.(type) {
case int:
return x + 1
case string:
return x + "+1"
}
return x
}
ys := Map(xs, f).(*A)
fmt.Printf("%#v\n", ys)
Output: &reflecttools.A{Field1:2, Field2:"1+1"}
func ZipReduce ¶
func ZipReduce[B comparable](x, y any, innit B, f func(x, y any, acc B) B) B
ZipReduce reduces a function over the elements of two slices or the fields of two structs together It allows you to shortcircuit the reduce by returning false from the function
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.