Enumerable
Enumerable functions for ordered containers that implement EnumerableWithIndex or EnumerableWithKey interfaces.
EnumerableWithIndex
Enumerable functions for ordered containers whose values can be fetched by an index.
Each
Calls the given function once for each element, passing that element’s index and value.
Each(func(index int, value interface{}))
Map
Invokes the given function once for each element and returns a container containing the values returned by the given function.
Map(func(index int, value interface{}) interface{}) Container
Select
Returns a new container containing all elements for which the given function returns a true value.
Select(func(index int, value interface{}) bool) Container
Any
Passes each element of the container to the given function and returns true if the function ever returns true for any element.
Any(func(index int, value interface{}) bool) bool
All
Passes each element of the container to the given function and returns true if the function returns true for all elements.
All(func(index int, value interface{}) bool) bool
Find
Passes each element of the container to the given function and returns the first (index,value) for which the function is true or -1,nil otherwise if no element matches the criteria.
Find(func(index int, value interface{}) bool) (int, interface{})}
Example:
package main
import (
"fmt"
"github.com/emirpasic/gods/sets/treeset"
)
func printSet(txt string, set *treeset.Set) {
fmt.Print(txt, "[ ")
set.Each(func(index int, value interface{}) {
fmt.Print(value, " ")
})
fmt.Println("]")
}
func main() {
set := treeset.NewWithIntComparator()
set.Add(2, 3, 4, 2, 5, 6, 7, 8)
printSet("Initial", set) // [ 2 3 4 5 6 7 8 ]
even := set.Select(func(index int, value interface{}) bool {
return value.(int)%2 == 0
})
printSet("Even numbers", even) // [ 2 4 6 8 ]
foundIndex, foundValue := set.Find(func(index int, value interface{}) bool {
return value.(int)%2 == 0 && value.(int)%3 == 0
})
if foundIndex != -1 {
fmt.Println("Number divisible by 2 and 3 found is", foundValue, "at index", foundIndex) // value: 6, index: 4
}
square := set.Map(func(index int, value interface{}) interface{} {
return value.(int) * value.(int)
})
printSet("Numbers squared", square) // [ 4 9 16 25 36 49 64 ]
bigger := set.Any(func(index int, value interface{}) bool {
return value.(int) > 5
})
fmt.Println("Set contains a number bigger than 5 is ", bigger) // true
positive := set.All(func(index int, value interface{}) bool {
return value.(int) > 0
})
fmt.Println("All numbers are positive is", positive) // true
evenNumbersSquared := set.Select(func(index int, value interface{}) bool {
return value.(int)%2 == 0
}).Map(func(index int, value interface{}) interface{} {
return value.(int) * value.(int)
})
printSet("Chaining", evenNumbersSquared) // [ 4 16 36 64 ]
}
EnumerableWithKey
Enumerable functions for ordered containers whose values whose elements are key/value pairs.
Each
Calls the given function once for each element, passing that element’s key and value.
Each(func(key interface{}, value interface{}))
Map
Invokes the given function once for each element and returns a container containing the values returned by the given function as key/value pairs.
Map(func(key interface{}, value interface{}) (interface{}, interface{})) Container
Select
Returns a new container containing all elements for which the given function returns a true value.
Select(func(key interface{}, value interface{}) bool) Container
Any
Passes each element of the container to the given function and returns true if the function ever returns true for any element.
Any(func(key interface{}, value interface{}) bool) bool
All
Passes each element of the container to the given function and returns true if the function returns true for all elements.
All(func(key interface{}, value interface{}) bool) bool
Find
Passes each element of the container to the given function and returns the first (key,value) for which the function is true or nil,nil otherwise if no element matches the criteria.
Find(func(key interface{}, value interface{}) bool) (interface{}, interface{})
Example:
package main
import (
"fmt"
"github.com/emirpasic/gods/maps/treemap"
)
func printMap(txt string, m *treemap.Map) {
fmt.Print(txt, " { ")
m.Each(func(key interface{}, value interface{}) {
fmt.Print(key, ":", value, " ")
})
fmt.Println("}")
}
func main() {
m := treemap.NewWithStringComparator()
m.Put("g", 7)
m.Put("f", 6)
m.Put("e", 5)
m.Put("d", 4)
m.Put("c", 3)
m.Put("b", 2)
m.Put("a", 1)
printMap("Initial", m) // { a:1 b:2 c:3 d:4 e:5 f:6 g:7 }
even := m.Select(func(key interface{}, value interface{}) bool {
return value.(int) % 2 == 0
})
printMap("Elements with even values", even) // { b:2 d:4 f:6 }
foundKey, foundValue := m.Find(func(key interface{}, value interface{}) bool {
return value.(int) % 2 == 0 && value.(int) % 3 == 0
})
if foundKey != nil {
fmt.Println("Element with value divisible by 2 and 3 found is", foundValue, "with key", foundKey) // value: 6, index: 4
}
square := m.Map(func(key interface{}, value interface{}) (interface{}, interface{}) {
return key.(string) + key.(string), value.(int) * value.(int)
})
printMap("Elements' values squared and letters duplicated", square) // { aa:1 bb:4 cc:9 dd:16 ee:25 ff:36 gg:49 }
bigger := m.Any(func(key interface{}, value interface{}) bool {
return value.(int) > 5
})
fmt.Println("Map contains element whose value is bigger than 5 is", bigger) // true
positive := m.All(func(key interface{}, value interface{}) bool {
return value.(int) > 0
})
fmt.Println("All map's elements have positive values is", positive) // true
evenNumbersSquared := m.Select(func(key interface{}, value interface{}) bool {
return value.(int) % 2 == 0
}).Map(func(key interface{}, value interface{}) (interface{}, interface{}) {
return key, value.(int) * value.(int)
})
printMap("Chaining", evenNumbersSquared) // { b:4 d:16 f:36 }
}