Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check if source is a set and preserve if so #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/vvvvalvalval/supdate/api.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions src/vvvvalvalval/supdate/impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
PEZ marked this conversation as resolved.
Show resolved Hide resolved

(defn comp1
"ad-hoc composition of 1-arity fns, faster than clojure.core/comp."
Expand Down
18 changes: 14 additions & 4 deletions test/vvvvalvalval/supdate/test/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"}}