Hi,there!
I need a function that replaces a first found element of list.
like that,
>(replace-first :a :b [:c :c :a :c :a]
[:c :c :b :c :a]
~~
;replace first :a to :b
and my code is as follows.
---code begin------------------------------------------
(use '[clojure.contrib.seq-utils])
(defn position [x coll]
(first (positions #(= x %) coll)))
(defn replace-first [s r coll]
(let [idx (position s coll)
splitted (split-at idx coll)]
(concat (first splitted) (list r) (rest (fnext splitted)))))
---code end------------------------------------------
I'd like to see how the replase-first could be improved,
especially concatination of elements.
Thanks for your insights.
Kenichi Kobayashi
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en