struct sharing same key?

2011-06-26 Thread Antonio Recio
I would like to create an struct with the names of differents nationalities. 
When people are the same nationality I would don't need to repeat again the 
nationality. Is there any way t0 get that. For example, I would like to 
write something like that:

(defstruct person :nationality :first :last)

(def people (struct person  "english" "Jim"
 "Silvester"
 "Stephen" "Howards"
   "chinese" "Chiu" "Chiu"))

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: struct sharing same key?

2011-06-26 Thread Ken Wesson
On Mon, Jun 27, 2011 at 1:10 AM, Antonio Recio  wrote:
> I would like to create an struct with the names of differents nationalities.
> When people are the same nationality I would don't need to repeat again the
> nationality. Is there any way t0 get that. For example, I would like to
> write something like that:
> (defstruct person :nationality :first :last)
> (def people (struct person  "english" "Jim"
>                                                      "Silvester"
>                                                      "Stephen" "Howards"
>                                        "chinese" "Chiu" "Chiu"))

You probably want something more like a map like this:

{:english [{:first "Jim" :last "Silvester"}
   {:first "Stephen" :last "Howards"}]
 :chinese [{:first "Chiu" :last "Chiu"}]}

-- 
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: struct sharing same key?

2011-06-27 Thread Antonio Recio
When I repeat : the number 3 [{:3 "father Andrew D" :3 "mother Lisa D"}] I 
get an error, although I can repeat number 1 with no problem. Why?

(struct person
{:1 "english person"
 [{:2 "Andrew D"
   [{:3 "father Andrew D"
 :3 "mother Lisa D"}]
   :2 "Justin M"
   [{:3 "Elisa M"}]}]
 :1 "chinese person"
 [{:2 "Chi chi"}]
 })

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: struct sharing same key?

2011-06-27 Thread Benny Tsai
:1 and :3 are keywords, not numbers.

map literals are specified in terms of key-value pairs; for example, in {a b 
c d}, a and c are keys, b and d are values.  In your person struct, :1 is 
only used as a key once, which is why that works.  This might help make 
things clearer:

(struct person
{:1
 "english person"
 
 [{:2 "Andrew D"
   [{:3 "father Andrew D"
 :3 "mother Lisa D"}]
   :2 "Justin M"
   [{:3 "Elisa M"}]}]
 :1
 
 "chinese person"
 [{:2 "Chi chi"}]
 })

In the first pair, :1 is the key, "english person" is the value.  In the 
second pair, the nested vector/map thing is the key, :1 is the value.  In 
the last pair, "chinese person" is the key, the vector containing a map is 
the value.

With all due respect, the questions you've posted to the list contain some 
misunderstandings about certain Clojure fundamentals.  May I suggest taking 
the time to work through a tutorial or book?  It will make life easier going 
forward.  There are many good choices to pick from, but this is one of my 
personal favorites:

http://java.ociweb.com/mark/clojure/article.html

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: struct sharing same key?

2011-06-28 Thread Stefan Kamphausen
Hi,

maybe Ken's solution needs some clarification

1. Use a map, not a struct.  defstruct is kinda deprecated.
2. Use a keyword (preceded by a colon) to code the nationality.  A keyword 
is created only once and won't consume any more memory if you use it 
repeatedly.  I'm not sure whether you want to save the memory or the 
repeated typing.
3. Ken bundled all persons in the map by language.  This may be the right 
solution for you or not.  But if you do so, note that the inner maps reside 
in a vector (square brackets)

Maybe something like this:

;; a function returning a map representing a person
(defn make-person [nationality first last]
  {:first first :last last :nationality nationality})

;; the data on which you base the creation of persons
;; This repeats the keyword for each person
(def persons-data
  [[:english "Jim" "Silvester"]
   [:english "Stephen" "Howards"]
   [:chinese "Chiu""Chiu"]])

;; combine the fn and the data
(def persons
  (into [] (map #(apply make-person %) persons-data)))


;; A second way of storing your data uses (almost) the representation 
suggested by Ken:
(def persons-data-2
  {:english [["Jim" "Silvester"]
 ["Stephen" "Howards"]]
   :chinese ["Chiu""Chiu"]})


;; This can be turned into a seq of persons like this
(def persons-2
  (map
   (fn [natio names]; can't use #() because it doesn't nest
 (map (fn [[first last]]; note the destructuring
(make-person natio first last))
  names))
   persons-data-2))


Hope this helps,
Stefan

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en