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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. Find(func(index int, value interface{}) bool) (int, interface{})}

Example:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/emirpasic/gods/sets/treeset"
  5. )
  6. func printSet(txt string, set *treeset.Set) {
  7. fmt.Print(txt, "[ ")
  8. set.Each(func(index int, value interface{}) {
  9. fmt.Print(value, " ")
  10. })
  11. fmt.Println("]")
  12. }
  13. func main() {
  14. set := treeset.NewWithIntComparator()
  15. set.Add(2, 3, 4, 2, 5, 6, 7, 8)
  16. printSet("Initial", set) // [ 2 3 4 5 6 7 8 ]
  17. even := set.Select(func(index int, value interface{}) bool {
  18. return value.(int)%2 == 0
  19. })
  20. printSet("Even numbers", even) // [ 2 4 6 8 ]
  21. foundIndex, foundValue := set.Find(func(index int, value interface{}) bool {
  22. return value.(int)%2 == 0 && value.(int)%3 == 0
  23. })
  24. if foundIndex != -1 {
  25. fmt.Println("Number divisible by 2 and 3 found is", foundValue, "at index", foundIndex) // value: 6, index: 4
  26. }
  27. square := set.Map(func(index int, value interface{}) interface{} {
  28. return value.(int) * value.(int)
  29. })
  30. printSet("Numbers squared", square) // [ 4 9 16 25 36 49 64 ]
  31. bigger := set.Any(func(index int, value interface{}) bool {
  32. return value.(int) > 5
  33. })
  34. fmt.Println("Set contains a number bigger than 5 is ", bigger) // true
  35. positive := set.All(func(index int, value interface{}) bool {
  36. return value.(int) > 0
  37. })
  38. fmt.Println("All numbers are positive is", positive) // true
  39. evenNumbersSquared := set.Select(func(index int, value interface{}) bool {
  40. return value.(int)%2 == 0
  41. }).Map(func(index int, value interface{}) interface{} {
  42. return value.(int) * value.(int)
  43. })
  44. printSet("Chaining", evenNumbersSquared) // [ 4 16 36 64 ]
  45. }

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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. 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.

  1. Find(func(key interface{}, value interface{}) bool) (interface{}, interface{})

Example:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/emirpasic/gods/maps/treemap"
  5. )
  6. func printMap(txt string, m *treemap.Map) {
  7. fmt.Print(txt, " { ")
  8. m.Each(func(key interface{}, value interface{}) {
  9. fmt.Print(key, ":", value, " ")
  10. })
  11. fmt.Println("}")
  12. }
  13. func main() {
  14. m := treemap.NewWithStringComparator()
  15. m.Put("g", 7)
  16. m.Put("f", 6)
  17. m.Put("e", 5)
  18. m.Put("d", 4)
  19. m.Put("c", 3)
  20. m.Put("b", 2)
  21. m.Put("a", 1)
  22. printMap("Initial", m) // { a:1 b:2 c:3 d:4 e:5 f:6 g:7 }
  23. even := m.Select(func(key interface{}, value interface{}) bool {
  24. return value.(int) % 2 == 0
  25. })
  26. printMap("Elements with even values", even) // { b:2 d:4 f:6 }
  27. foundKey, foundValue := m.Find(func(key interface{}, value interface{}) bool {
  28. return value.(int) % 2 == 0 && value.(int) % 3 == 0
  29. })
  30. if foundKey != nil {
  31. fmt.Println("Element with value divisible by 2 and 3 found is", foundValue, "with key", foundKey) // value: 6, index: 4
  32. }
  33. square := m.Map(func(key interface{}, value interface{}) (interface{}, interface{}) {
  34. return key.(string) + key.(string), value.(int) * value.(int)
  35. })
  36. printMap("Elements' values squared and letters duplicated", square) // { aa:1 bb:4 cc:9 dd:16 ee:25 ff:36 gg:49 }
  37. bigger := m.Any(func(key interface{}, value interface{}) bool {
  38. return value.(int) > 5
  39. })
  40. fmt.Println("Map contains element whose value is bigger than 5 is", bigger) // true
  41. positive := m.All(func(key interface{}, value interface{}) bool {
  42. return value.(int) > 0
  43. })
  44. fmt.Println("All map's elements have positive values is", positive) // true
  45. evenNumbersSquared := m.Select(func(key interface{}, value interface{}) bool {
  46. return value.(int) % 2 == 0
  47. }).Map(func(key interface{}, value interface{}) (interface{}, interface{}) {
  48. return key, value.(int) * value.(int)
  49. })
  50. printMap("Chaining", evenNumbersSquared) // { b:4 d:16 f:36 }
  51. }