From 6e57f15d9004e994ac81ab7a1ad8c1ffd9c69c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Str=C3=B6mberg?= Date: Sun, 9 Oct 2022 22:02:08 +0200 Subject: [PATCH 1/2] Add check if source is a set and preserve if so Fixes #8 --- src/vvvvalvalval/supdate/api.cljc | 3 ++- src/vvvvalvalval/supdate/impl.cljc | 7 ++++--- test/vvvvalvalval/supdate/test/api.clj | 18 ++++++++++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/vvvvalvalval/supdate/api.cljc b/src/vvvvalvalval/supdate/api.cljc index de64b21..cf635f8 100644 --- a/src/vvvvalvalval/supdate/api.cljc +++ b/src/vvvvalvalval/supdate/api.cljc @@ -58,7 +58,8 @@ To avoid the cost of a runtime type check, the caller may add the ^{:vvvvalvalva The transform will only be performed for the keys that are contained in v. If the transform value for a key is `false`, then the key is dissoc'ed from v. * if `transform` is a vector with one element (a nested transform), will treat v as a collection an apply the nested transform to each element. -It the source collection is a vector, the output collection will be a vector as well. +If the source collection is a vector, the output collection will be a vector as well. +If the source collection is a set, the output collection will be a set as well. * if transform is a sequence, will apply each transform in the sequence in order. In order to achieve efficiency, this macro will attempt to leverage static information on the `transform` form, diff --git a/src/vvvvalvalval/supdate/impl.cljc b/src/vvvvalvalval/supdate/impl.cljc index 8022efe..d08617a 100644 --- a/src/vvvvalvalval/supdate/impl.cljc +++ b/src/vvvvalvalval/supdate/impl.cljc @@ -30,9 +30,10 @@ (defn supd-map* [f coll] - (if (vector? coll) - (mapv f coll) - (map f coll))) + (cond + (vector? coll) (mapv f coll) + (set? coll) (into #{} (map f coll)) + :else (map f coll))) (defn comp1 "ad-hoc composition of 1-arity fns, faster than clojure.core/comp." diff --git a/test/vvvvalvalval/supdate/test/api.clj b/test/vvvvalvalval/supdate/test/api.clj index 0014933..ece74d2 100644 --- a/test/vvvvalvalval/supdate/test/api.clj +++ b/test/vvvvalvalval/supdate/test/api.clj @@ -68,9 +68,16 @@ (supdate (list 1 2 3 4) [inc]) => '(2 3 4 5) + (supdate (set [1 2 3 4]) [inc]) + => #{2 3 4 5} + + (supdate {:a #{1 2 3 4}} {:a [inc]}) + => {:a #{2 3 4 5}} + (supdate [] [inc]) => [] (supdate () [inc]) => () - ) + (supdate #{} [inc]) => #{} + ) ;; you can nest transforms arbitrarily. (fact "Transforms can be nested" @@ -94,16 +101,19 @@ :b [1 2 3] :c {"d" [{:e 1 :f 1} {:e 2 :f 2}]} :g 0 - :h 0}) + :h 0 + :i #{1 2 3}}) (supdate my-data {:a inc :b [inc] :c {"d" [{:e inc}]} - :g [inc inc inc]}) + :g [inc inc inc] + :i [str]}) => {:a 2, :b [2 3 4], :c {"d" [{:e 2, :f 1} {:e 3, :f 2}]} :g 3, - :h 0} + :h 0 + :i #{"1" "2" "3"}} From 8635e820a7a7a59f7f1caa26ed0cb2faeca0cc8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Str=C3=B6mberg?= Date: Wed, 12 Oct 2022 16:53:12 +0200 Subject: [PATCH 2/2] Simplify `supd-map*` Co-authored-by: Valentin Waeselynck --- src/vvvvalvalval/supdate/impl.cljc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/vvvvalvalval/supdate/impl.cljc b/src/vvvvalvalval/supdate/impl.cljc index d08617a..d62e4e7 100644 --- a/src/vvvvalvalval/supdate/impl.cljc +++ b/src/vvvvalvalval/supdate/impl.cljc @@ -30,10 +30,9 @@ (defn supd-map* [f coll] - (cond - (vector? coll) (mapv f coll) - (set? coll) (into #{} (map f coll)) - :else (map f coll))) + (if (or (vector? coll) (set? coll)) + (into (empty coll) (map f) coll) + (map f coll))) (defn comp1 "ad-hoc composition of 1-arity fns, faster than clojure.core/comp."