diff options
author | Oskar Haarklou Veileborg <ohv1020@hotmail.com> | 2023-01-09 20:28:08 +0100 |
---|---|---|
committer | Oskar Haarklou Veileborg <ohv1020@hotmail.com> | 2023-01-09 20:28:08 +0100 |
commit | 8e1383ea6a9561610e00b8c7f401bca1257f9001 (patch) | |
tree | 3ce620c77162717648feb4ae411b6a63acb0f2e3 /immutable.go | |
parent | Merge pull request #35 from laher/sets-maps-append-multi (diff) | |
download | pds-8e1383ea6a9561610e00b8c7f401bca1257f9001.tar.gz pds-8e1383ea6a9561610e00b8c7f401bca1257f9001.tar.xz |
Ensure immutability of sets (and maps with SetMany)
Diffstat (limited to 'immutable.go')
-rw-r--r-- | immutable.go | 10 |
1 files changed, 4 insertions, 6 deletions
diff --git a/immutable.go b/immutable.go index b3d7dcb..4a42e33 100644 --- a/immutable.go +++ b/immutable.go @@ -757,11 +757,10 @@ func (m *Map[K, V]) Set(key K, value V) *Map[K, V] { // This function will return a new map even if the updated value is the same as // the existing value because Map does not track value equality. func (m *Map[K, V]) SetMany(entries map[K]V) *Map[K, V] { - n := m.clone() for k, v := range entries { - n.set(k, v, true) + m = m.Set(k, v) } - return n + return m } func (m *Map[K, V]) set(key K, value V, mutable bool) *Map[K, V] { @@ -1644,11 +1643,10 @@ func (m *SortedMap[K, V]) Set(key K, value V) *SortedMap[K, V] { // SetMany returns a map with the keys set to the new values. func (m *SortedMap[K, V]) SetMany(entries map[K]V) *SortedMap[K, V] { - n := m.clone() for k, v := range entries { - n.set(k, v, true) + m = m.Set(k, v) } - return n + return m } func (m *SortedMap[K, V]) set(key K, value V, mutable bool) *SortedMap[K, V] { |