FAQ

On Mon, Feb 13, 2012 at 5:16 PM, James wrote:
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?
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.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 3 | next ›
Discussion Overview
groupclojure @
categoriesclojure
postedFeb 14, '12 at 1:21p
activeFeb 14, '12 at 3:31p
posts3
users3
websiteclojure.org
irc#clojure

People

Translate

site design / logo © 2022 Grokbase