Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add update #21

Merged
merged 1 commit into from

2 participants

@gfredericks

Used several arities to make it GC-friendly.

@gfredericks gfredericks Add update
Used several arities to make it GC-friendly.
68d3371
@gfredericks gfredericks referenced this pull request
Closed

update #20

@w01fe
Owner

Awesome, thanks for the PR!

@w01fe w01fe merged commit 1cc3c89 into Prismatic:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 5, 2014
  1. @gfredericks

    Add update

    gfredericks authored
    Used several arities to make it GC-friendly.
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 0 deletions.
  1. +10 −0 src/plumbing/core.clj
  2. +14 −0 test/plumbing/core_test.clj
View
10 src/plumbing/core.clj
@@ -30,6 +30,16 @@
(reset! m-atom# (assoc! ~m-sym ~key-expr ~val-expr))))
(persistent! @m-atom#))))
+(defn update
+ "Updates the value in map m at k with the function f.
+
+ Like update-in, but for updating a single top-level key.
+ Any additional args will be passed to f after the value."
+ ([m k f] (assoc m k (f (get m k))))
+ ([m k f x1] (assoc m k (f (get m k) x1)))
+ ([m k f x1 x2] (assoc m k (f (get m k) x1 x2)))
+ ([m k f x1 x2 & xs] (assoc m k (apply f (get m k) x1 x2 xs))))
+
(defn map-vals
"Build map k -> (f v) for [k v] in map, preserving the initial type"
[f m]
View
14 test/plumbing/core_test.clj
@@ -27,6 +27,20 @@
(is (= (count m) 1000))
(is (= (m 999) 499500))))
+(deftest update-test
+ (testing "0 extra args"
+ (is (= {:a 5, :b 0}
+ (update {:a 4, :b 0} :a inc)))
+ (is (= {:a 1}
+ (update {} :a (fnil inc 0)))))
+ (testing "1 extra arg"
+ (is (= {:a 42, :b 0}
+ (update {:a 6, :b 0} :a * 7)))
+ (is (= {:a #{42}}
+ (update {} :a (fnil conj #{}) 42))))
+ (testing "100 extra args"
+ (is (= {:a 4951} (apply update {:a 1} :a + (range 100))))))
+
(deftest map-vals-test
(is (= (map-vals inc {:a 0 :b 0})
{:a 1 :b 1}))
Something went wrong with that request. Please try again.