Skip to content

Commit

Permalink
Merge pull request #17 from jellelicht/master
Browse files Browse the repository at this point in the history
Atomic swap and IAtom2 implementation
  • Loading branch information
niwinz committed Oct 14, 2021
2 parents 2cf7a6a + 11b8a79 commit c44256e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/lentes/core.cljc
Expand Up @@ -166,20 +166,27 @@

clojure.lang.IAtom
(reset [self newval]
(swap! src #(put lens newval %))
(deref self))
(focus lens (swap! src #(put lens newval %))))
(swap [self f]
(swap! src (fn [s] (over lens f s)))
(deref self))
(focus lens (swap! src (fn [s] (over lens f s)))))
(swap [self f x]
(swap! src (fn [s] (over lens #(f % x) s)))
(deref self))
(focus lens (swap! src (fn [s] (over lens #(f % x) s)))))
(swap [self f x y]
(swap! src (fn [s] (over lens #(f % x y) s)))
(deref self))
(focus lens (swap! src (fn [s] (over lens #(f % x y) s)))))
(swap [self f x y more]
(swap! src (fn [s] (over lens #(apply f % x y more) s)))
(deref self))
(focus lens (swap! src (fn [s] (over lens #(apply f % x y more) s)))))

clojure.lang.IAtom2
(resetVals [self newval]
(mapv (partial focus lens) (swap-vals! src #(put lens newval %))))
(swapVals [self f]
(mapv (partial focus lens) (swap-vals! src (fn [s] (over lens f s)))))
(swapVals [self f x]
(mapv (partial focus lens) (swap-vals! src (fn [s] (over lens #(f % x) s)))))
(swapVals [self f x y]
(mapv (partial focus lens) (swap-vals! src (fn [s] (over lens #(f % x y) s)))))
(swapVals [self f x y more]
(mapv (partial focus lens) (swap-vals! src (fn [s] (over lens #(apply f % x y more) s)))))

clojure.lang.IRef
(addWatch [self key cb]
Expand Down
2 changes: 2 additions & 0 deletions test/lentes/tests.cljc
Expand Up @@ -224,6 +224,7 @@
(t/is (= @fsource 0))

#?@(:clj [(t/is (instance? clojure.lang.IAtom fsource))
(t/is (instance? clojure.lang.IAtom2 fsource))
(t/is (instance? clojure.lang.IDeref fsource))
(t/is (instance? clojure.lang.IRef fsource))]
:cljs [(t/is (satisfies? IDeref fsource))
Expand All @@ -245,6 +246,7 @@
(t/is (= @fsource 0))

#?@(:clj [(t/is (not (instance? clojure.lang.IAtom fsource)))
(t/is (not (instance? clojure.lang.IAtom2 fsource)))
(t/is (instance? clojure.lang.IDeref fsource))
(t/is (instance? clojure.lang.IRef fsource))]
:cljs [(t/is (satisfies? IDeref fsource))
Expand Down

0 comments on commit c44256e

Please sign in to comment.