Debian Codegraph @

maps/linkedhashmap/enumerable.go

raw symbols SCIP
1 // Copyright (c) 2015, Emir Pasic. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package linkedhashmap
6
7 import "github.com/emirpasic/gods/v2/containers"
8
9 // Assert Enumerable implementation
10 var _ containers.EnumerableWithKey[string, int] = (*Map[string, int])(nil)
11
12 // Each calls the given function once for each element, passing that element's key and value.
13 func (m *Map[K, V]) Each(f func(key K, value V)) {
14 iterator := m.Iterator()
15 for iterator.Next() {
16 f(iterator.Key(), iterator.Value())
17 }
18 }
19
20 // Map invokes the given function once for each element and returns a container
21 // containing the values returned by the given function as key/value pairs.
22 func (m *Map[K, V]) Map(f func(key1 K, value1 V) (K, V)) *Map[K, V] {
23 newMap := New[K, V]()
24 iterator := m.Iterator()
25 for iterator.Next() {
26 key2, value2 := f(iterator.Key(), iterator.Value())
27 newMap.Put(key2, value2)
28 }
29 return newMap
30 }
31
32 // Select returns a new container containing all elements for which the given function returns a true value.
33 func (m *Map[K, V]) Select(f func(key K, value V) bool) *Map[K, V] {
34 newMap := New[K, V]()
35 iterator := m.Iterator()
36 for iterator.Next() {
37 if f(iterator.Key(), iterator.Value()) {
38 newMap.Put(iterator.Key(), iterator.Value())
39 }
40 }
41 return newMap
42 }
43
44 // Any passes each element of the container to the given function and
45 // returns true if the function ever returns true for any element.
46 func (m *Map[K, V]) Any(f func(key K, value V) bool) bool {
47 iterator := m.Iterator()
48 for iterator.Next() {
49 if f(iterator.Key(), iterator.Value()) {
50 return true
51 }
52 }
53 return false
54 }
55
56 // All passes each element of the container to the given function and
57 // returns true if the function returns true for all elements.
58 func (m *Map[K, V]) All(f func(key K, value V) bool) bool {
59 iterator := m.Iterator()
60 for iterator.Next() {
61 if !f(iterator.Key(), iterator.Value()) {
62 return false
63 }
64 }
65 return true
66 }
67
68 // Find passes each element of the container to the given function and returns
69 // the first (key,value) for which the function is true or nil,nil otherwise if no element
70 // matches the criteria.
71 func (m *Map[K, V]) Find(f func(key K, value V) bool) (k K, v V) {
72 iterator := m.Iterator()
73 for iterator.Next() {
74 if f(iterator.Key(), iterator.Value()) {
75 return iterator.Key(), iterator.Value()
76 }
77 }
78 return k, v
79 }