github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/core/elvish/hashmap/map.go (about) 1 package hashmap 2 3 // Map is a persistent associative data structure mapping keys to values. It 4 // is immutable, and supports near-O(1) operations to create modified version of 5 // the map that shares the underlying data structure. Because it is immutable, 6 // all of its methods are safe for concurrent use. 7 type Map interface { 8 Len() int 9 // Index returns whether there is a value associated with the given key, and 10 // that value or nil. 11 Index(k interface{}) (interface{}, bool) 12 // Assoc returns an almost identical map, with the given key associated with 13 // the given value. 14 Assoc(k, v interface{}) Map 15 // Dissoc returns an almost identical map, with the given key associated 16 // with no value. 17 Dissoc(k interface{}) Map 18 // Iterator returns an iterator over the map. 19 Iterator() Iterator 20 } 21 22 // Iterator is an iterator over map elements. It can be used like this: 23 // 24 // for it := m.Iterator(); it.HasElem(); it.Next() { 25 // key, value := it.Elem() 26 // // do something with elem... 27 // } 28 type Iterator interface { 29 // Elem returns the current key-value pair. 30 Elem() (interface{}, interface{}) 31 // HasElem returns whether the iterator is pointing to an element. 32 HasElem() bool 33 // Next moves the iterator to the next position. 34 Next() 35 } 36 37 // HasKey reports whether a Map has the given key. 38 func HasKey(m Map, k interface{}) bool { 39 _, ok := m.Index(k) 40 return ok 41 }