Skip to content

Commit

Permalink
Update dict.merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason2605 committed Oct 24, 2021
1 parent cdd6417 commit b0df3b9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
6 changes: 4 additions & 2 deletions docs/docs/collections/dictionaries.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,15 @@ myDict.forEach(def (key, value) => {

### dict.merge(anotherDict)

To merge with another dictionary. This operation will produce a new object. If another dictionary contains a key that exists in invoking one, the invoking dictionary's key will be taken.
To merge two dicts together we use `.merge`. This operation will take a shallow copy of the dict the `.merge` method
was called on and add any items from the dictionary passed into the method. If there are keys that exist in both dictionaries
the value from the passed in dictionary is the one that will be used.

```cs
const dictOne = {"key": 1, "key1": 2, "key2": 3};
const dictTwo = {"key3": 4,"key1":0};

const mergedDict = dictOne.merge(dictTwo);

mergedDict; //{"key2": 3, "key": 1, "key3": 4, "key1": 2}
mergedDict; //{"key2": 3, "key": 1, "key3": 4, "key1": 0}
```
10 changes: 5 additions & 5 deletions src/vm/datatypes/dicts/dict-source.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
"}\n" \
"\n" \
"def merge(dict, anotherDict) {\n" \
" const dictKeys = dict.keys();\n" \
" const newDict= anotherDict.copy();\n" \
" const newDict = dict.copy();\n" \
"\n" \
" forEach(anotherDict, def (key, value) => {\n" \
" newDict[key] = value;\n" \
" });\n" \
"\n" \
" for (var i = 0; i < dictKeys.len(); i += 1) {\n" \
" newDict[dictKeys[i]]=dict[dictKeys[i]];\n" \
" }\n" \
" return newDict;\n" \
"}\n" \

12 changes: 6 additions & 6 deletions src/vm/datatypes/dicts/dict.du
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ def forEach(dict, func) {
}

def merge(dict, anotherDict) {
const dictKeys = dict.keys();
const newDict= anotherDict.copy();
const newDict = dict.copy();

forEach(anotherDict, def (key, value) => {
newDict[key] = value;
});

for (var i = 0; i < dictKeys.len(); i += 1) {
newDict[dictKeys[i]]=dict[dictKeys[i]];
}
return newDict;
}
}
14 changes: 7 additions & 7 deletions tests/dicts/merge.du
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
*
* Testing the dict.merge() method
*
* .merge() To merge with another dictionary. This operation will produce a new object. If another dictionary contains a key that exists in invoking one, the invoking dictionary's key will be taken..
* .merge() merges two dictionaries together.
*/

const dictOne = {"key": 1, "key1": 2, "key2": 3};
const dictTwo = {"key3": 4,"key1":0};
const dictTwo = {"key3": 4,"key1": 0};

const mergedDict=dictOne.merge(dictTwo);
const mergedDict = dictOne.merge(dictTwo);

assert(mergedDict.keys().len() == 4);

Expand All @@ -18,7 +18,7 @@ assert(mergedDict.exists("key1"));
assert(mergedDict.exists("key2"));
assert(mergedDict.exists("key3"));

assert(mergedDict["key"] ==1);
assert(mergedDict["key1"]==2);
assert(mergedDict["key2"]==3);
assert(mergedDict["key3"]==4);
assert(mergedDict["key"] == 1);
assert(mergedDict["key1"] == 0);
assert(mergedDict["key2"] == 3);
assert(mergedDict["key3"] == 4);

0 comments on commit b0df3b9

Please sign in to comment.