Skip to content

Commit 4178c21

Browse files
authored
Added merge-with core fn (#874)
Hi, can you please consider patch to add support for the `merge-with` core fn. It resolves #860. I have added a couple of test cases for the same. Thanks --------- Co-authored-by: ikappaki <[email protected]>
1 parent 174d5ff commit 4178c21

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
* Added support for binding destructuring in `for` bindings (#774)
1515
* Added `==` as an alias to `=` (#859)
1616
* Added custom exception formatting for `basilisp.lang.compiler.exception.CompilerException` and `basilisp.lang.reader.SyntaxError` to show more useful details to users on errors (#870)
17+
* Added `merge-with` core function (#860)
1718

1819
### Changed
1920
* Cause exceptions arising from compilation issues during macroexpansion will no longer be nested for each level of macroexpansion (#852)

src/basilisp/core.lpy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3258,6 +3258,21 @@
32583258
(transient {})
32593259
maps))))
32603260

3261+
(defn merge-with
3262+
"Merge maps together from left to right as by :lpy:fn:`conj`. If a
3263+
duplicate key appears in a map, the resulting value of that key in
3264+
the merged result will be the result of calling ``(f current-val
3265+
next-val)``."
3266+
[f & maps]
3267+
(when (some identity maps)
3268+
(reduce (fn [acc kv]
3269+
(let [k (first kv)
3270+
v (second kv)]
3271+
(if (contains? acc k)
3272+
(update acc k f v)
3273+
(assoc acc k v))))
3274+
(first maps) (apply concat (rest maps)))))
3275+
32613276
(defn trampoline
32623277
"Trampoline ``f`` with starting arguments. If ``f`` returns a function (as determined
32633278
by :lpy:fn:`fn?`), call its return value with no arguments, repeating that process

tests/basilisp/prompt_test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def completions(self) -> Iterable[str]:
4040
"max",
4141
"max-key",
4242
"merge",
43+
"merge-with",
4344
"meta",
4445
"methods",
4546
"min",
@@ -77,6 +78,7 @@ def patch_completions(self, completions: Iterable[str]):
7778
"memfn",
7879
"memoize",
7980
"merge",
81+
"merge-with",
8082
"meta",
8183
"methods",
8284
"min",

tests/basilisp/test_core_fns.lpy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,12 @@
10961096
{:a 1 :b 2} [:a :b] [1 2]
10971097
{:a 0 :b 1} [:a :b] (range)))
10981098

1099+
(deftest merge-with-test
1100+
(is (= nil (merge-with conj)))
1101+
(is (= nil (merge-with str nil)))
1102+
(is (= {:a 0 :b "235" :c "46" :d 7 :z 8} (merge-with str {:a 0 :b 2} {:b 3 :c 4 :z 8} {:b 5 :c 6 :d 7})))
1103+
(is (= {:a 0 :b [2 5] :c 7} (merge-with conj {:a 0 :b [2]} nil {:b 5 :c 7}))))
1104+
10991105
(deftest trampoline-test
11001106
(let [a (atom [])]
11011107
(trampoline (fn [v]

0 commit comments

Comments
 (0)