replace a list element

1,371 views
Skip to first unread message

James

unread,
Feb 13, 2012, 5:16:51 PM2/13/12
to Clojure
Hi,

I am new to clojure and I am very excited about learning the
language. I have a question about replacing an element in a list. I
have elements that look like this :

{:id G__781, :value 1}

The list looks like this:

({:id G__821, :value 1} {:id G__820, :value 1} {:id G__819, :value 1}
{:id G__818, :value 1} {:id G__817, :value 1})

I want to update an element in the list by conjoining a new list with
an updated element. For example, I'll update the head of the list:
({:id G__821, :value 10} {:id G__820, :value 1} {:id G__819, :value 1}
{:id G__818, :value 1} {:id G__817, :value 1})

My update code (it really builds a new list with and replaces the
updated element) looks like this:

(defn update-id-list [id-list new-id new-id-list]
(doseq[old-id id-list
new-id-list (conj new-id-list (choose-id old-id new-id) )]
(println "New List " new-id-list)) new-id-list)

(defn choose-id[old-id new-id]
(if (= (:id old-id) (:id new-id)) new-id old-id))

This doesn't work. It never conjoin's elements and ultimately returns
an empty list. I've tried using various other methods but I keep
going in circles.

Any ideas why this wouldn't work?

Thanks,
James

Cedric Greevey

unread,
Feb 14, 2012, 9:19:58 AM2/14/12
to clo...@googlegroups.com

Functions like conj create and return a new list, rather than modify
the original in-place. You'd need to change your doseq to something
like:

(for [old-id id-list] (choose-id old-id new-id))

I'd also use better names, perhaps

(for [old-entry entry-list] (choose-entry old-entry new-entry))

But for this sort of job you might be better off using a larger map:

{G__821 {:id G__821 :value 1} G__820 {:id G__820 :value 1} ... }

(assoc entry-map old-id new-entry)

e.g. (assoc entry-map G__821 {:id G__821 :value 10})

or even (update-in entry-map [id :value] (constantly new-value))

e.g. (update-in entry-map [G__821 :value] (constantly 10))

If you actually transform the old value with a function to get the new
one, so much the better:

(update-in entry-map [G__821 :value] #(* 10 %))

etc.

Incidentally, are all of these symbols G__xxx defined somewhere? You
might want keywords or strings instead, to avoid having to quote them
to avoid errors.

Moritz Ulrich

unread,
Feb 14, 2012, 10:31:02 AM2/14/12
to clo...@googlegroups.com
I'd use clojure.core/replace

It takes a replacement-map in the form of {before after, ...} and a
collection. It replaces all befores with the corresponding afters:

=> (replace {:answer 42} [:the :answer :to :life])
[:the 42 :to :life]

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

--
Moritz Ulrich

Reply all
Reply to author
Forward
0 new messages