Skip to content

Commit

Permalink
Add q69
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkun committed Sep 15, 2024
1 parent 769d06f commit 646d386
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/ex/q069.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
;; https://4clojure.oxal.org/#/problem/069

(ns ex.q069)

(defn ans
"Problem 69, Merge with a Function. (restriction: merge-with)"
[f m1 & maps]
(letfn [(combine
[m1 m2]
(reduce (fn [m [k v2]]
(if-let [v1 (get m k)]
(assoc m k (f v1 v2))
(assoc m k v2)))
m1 m2))]
(reduce combine m1 maps)))
15 changes: 15 additions & 0 deletions test/ex/q069_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
;; https://4clojure.oxal.org/#/problem/069

(ns ex.q069-test
(:require [clojure.test :as t]
[ex.q069 :as sut]))

(t/deftest test-q69
(t/testing "Problem 69, Merge with a Function"
(t/is (= {:a 4, :b 6, :c 20}
(sut/ans * {:a 2, :b 3, :c 4} {:a 2} {:b 2} {:c 5})))
;; (t/is (= {1 7, 2 10, 3 15}
;; (sut/ans - {1 10, 2 20} {1 3, 2 10, 3 15})))
;; (t/is (= {:a [3 4 5], :b [6 7], :c [8 9]}
;; (sut/ans concat {:a [3], :b [6]} {:a [4 5], :c [8 9]} {:b [7]})))
))

0 comments on commit 646d386

Please sign in to comment.