Skip to content

Commit

Permalink
Added replace
Browse files Browse the repository at this point in the history
  • Loading branch information
fogus committed Jul 28, 2011
1 parent e1de3b5 commit 424fb2c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
10 changes: 5 additions & 5 deletions devnotes/corelib.org
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ does what?
* TODO methods
* DONE min
* DONE min-key
* TODO mod
* DONE mod
* munge
* DONE name
* DONE namespace
Expand Down Expand Up @@ -410,7 +410,7 @@ dunno about printing
* proxy-super
* push-thread-bindings
* pvalues
* TODO quot
* DONE quot
* DONE rand
* DONE rand-int
* DONE rand-nth
Expand Down Expand Up @@ -441,15 +441,15 @@ dunno about regex
* TODO reify
also specify - make a particular object extend a protocol
* release-pending-sends
* TODO rem
* DONE rem
* DONE remove
* TODO remove-all-methods
* TODO remove-method
* remove-ns
* remove-watch
* DONE repeat
* TODO repeatedly
* TODO replace
* DONE repeatedly
* DONE replace
* DONE replicate
* TODO require
ticket #8
Expand Down
18 changes: 18 additions & 0 deletions src/cljs/cljs/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2225,6 +2225,20 @@ reduces them without incurring seq initialization"
(recur (rest in) (conj out (first in)))
out)))

(defn replace
"Given a map of replacement pairs and a vector/collection, returns a
vector/seq with any elements = a key in smap replaced with the
corresponding val in smap"
[smap coll]
(if (vector? coll)
(let [n (count coll)]
(reduce (fn [v i]
(if-let [e (find smap (nth v i))]
(assoc v i (second e))
v))
coll (take n (iterate inc 0))))
(map #(if-let [e (find smap %)] (second e) %) coll)))

(defn distinct
"Returns a lazy sequence of the elements of coll with duplicates removed"
[coll]
Expand Down Expand Up @@ -3416,6 +3430,10 @@ reduces them without incurring seq initialization"
{:a 1 :b 100}
{:a 1 :b 2 :c 3}
{:a 1 :c 10}])))

(assert (= '[a c e] (replace '[a b c d e] [0 2 4])))
(assert (= [:one :zero :two :zero]
(replace {0 :zero 1 :one 2 :two} '(0 1 2 0))))
:ok
)

Expand Down

0 comments on commit 424fb2c

Please sign in to comment.