Re: Questions regarding Map vs Record and usage

2014-04-09 Thread Greg D
FWIW, I use:

   - (->Book "Lord of the Rings", "Tolkien")
  - when I define the record instance with the default fields, and the 
  number of default fields is a small number
   - (map->Book {:title "The Fellowship of the Ring",
  :author "Tolkien",
  :series "The Lord of the Rings",
  :publisher "Mariner Books",
  :year 2012,
  :ISBN-10 "0547928211",
  :my-rating 10}) 
  - The explicit key names are good documentation when 
  - there are many fields in the defrecord, or
 - when adding entries not in the defrecord (eg. my-rating)
  
Greg

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Questions regarding Map vs Record and usage

2014-04-09 Thread Ben Mabey

On 4/9/14, 12:51 PM, Anthony Ortiz wrote:

I see that there are several ways of instantiating a record :


(->Book "Lord of the Rings", "Tolkien")

(Book. "Lord of the Rings", "Tolkien")

#user.Book{:title "Lord of the Rings", :author "Tolkien"}


You missed one:

(map->Book {:title "Lord of the Rings", :author "Tolkien"})



Questions :
1) The second version is referred to as the "original" version so I'm 
wondering which is the preferred method.
The helper constructor fns are preferred and not the older dot form.  
IIRCC you have to import the record to use that constructor, but with 
the constructor fns you can :require them like any other clojure fn.  
Not a big deal, but it is nicer and doesn't let the interop abstraction 
bleed into your ns declarations.


2) Why is the third version not within a list? I thought that for the 
language to consider something "callable" it had to be the first 
argument in a list (eg : (+ 1 2))
Your third version is using tagged literals and so the semantics of the 
reader is different in that case.  See 
http://clojure.org/reader#The%20Reader--extensible%20data%20notation%20(edn)
3) The record that is returned via (def b [version of choice goes 
here]) is treated as a map when using the form (:title b) but doesn't 
treat it as a map when using the form (b :title) which seems pretty 
inconsistent to me; is there are valid reason for this inconsistency? 
I'm sure I'm missing something.


I agree that it is inconsistent.  I don't know why that inconsistency 
exists but I would be curious to know the reason if someone on the list 
knows...  As A. Webb just pointed out you can add that behavior yourself 
if you want to.


Thanks!
--
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
---
You received this message because you are subscribed to the Google 
Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to clojure+unsubscr...@googlegroups.com 
.

For more options, visit https://groups.google.com/d/optout.


--
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
--- 
You received this message because you are subscribed to the Google Groups "Clojure" group.

To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Questions regarding Map vs Record and usage

2014-04-09 Thread A. Webb
You can refer-in the first (factory function) to another namespace; for the 
second you'd have to import the class. I prefer the first. The third is 
special syntax for the reader.

As to the last question, I don't know, but it allows you to define your 
own. For example, 

(defrecord Book [title author] 
  clojure.lang.IFn 
  (invoke [this x] (get this x)) 
  (invoke [this x not-found] (get this x not-found)))

(def b (->Book "Lord of the Rings", "Tolkien"))

(b :title) ;=> "Lord of the Rings"

But you could alter the behavior as desired.


On Wednesday, April 9, 2014 1:51:01 PM UTC-5, Anthony Ortiz wrote:
>
> I see that there are several ways of instantiating a record :
>
>
> (->Book "Lord of the Rings", "Tolkien")
>
> (Book. "Lord of the Rings", "Tolkien")
>
> #user.Book{:title "Lord of the Rings", :author "Tolkien"}
>
>
> Questions :
> 1) The second version is referred to as the "original" version so I'm 
> wondering which is the preferred method. 
> 2) Why is the third version not within a list? I thought that for the 
> language to consider something "callable" it had to be the first argument 
> in a list (eg : (+ 1 2))
> 3) The record that is returned via (def b [version of choice goes here]) 
> is treated as a map when using the form (:title b) but doesn't treat it as 
> a map when using the form (b :title) which seems pretty inconsistent to me; 
> is there are valid reason for this inconsistency? I'm sure I'm missing 
> something.
>
> Thanks! 
>

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Questions regarding Map vs Record and usage

2014-04-09 Thread Anthony Ortiz
I see that there are several ways of instantiating a record :


(->Book "Lord of the Rings", "Tolkien")

(Book. "Lord of the Rings", "Tolkien")

#user.Book{:title "Lord of the Rings", :author "Tolkien"}


Questions :
1) The second version is referred to as the "original" version so I'm 
wondering which is the preferred method. 
2) Why is the third version not within a list? I thought that for the 
language to consider something "callable" it had to be the first argument 
in a list (eg : (+ 1 2))
3) The record that is returned via (def b [version of choice goes here]) is 
treated as a map when using the form (:title b) but doesn't treat it as a 
map when using the form (b :title) which seems pretty inconsistent to me; 
is there are valid reason for this inconsistency? I'm sure I'm missing 
something.

Thanks! 

-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.