Re: Idiomatic use of records vs. maps
A couple of aspects of records that I have found useful: * they provide a type for dispatching. Rather than rooting around in the map to find out what it is, a multi-method can dispatch directly on the type of the object. * having a central definition of the main keys contained in the structure is sometimes nice for defining an apps data structures -David -- 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: Idiomatic use of records vs. maps
On Tue, May 31, 2011 at 11:30 AM, David McNeil mcneil.da...@gmail.com wrote: A couple of aspects of records that I have found useful: * they provide a type for dispatching. Rather than rooting around in the map to find out what it is, a multi-method can dispatch directly on the type of the object. And you can use a protocol, which dispatches very efficiently on the type of the object if the record is declared at creation time as implementing the protocol. However the OP indicated he wasn't using protocols (or, presumably, multimethods) with his records in this *particular* instance. * having a central definition of the main keys contained in the structure is sometimes nice for defining an apps data structures This is a point; a defrecord can be useful as documentation within the code, all other things being equal. -- 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: Idiomatic use of records vs. maps
On 30 May 2011 02:02, Ken Wesson kwess...@gmail.com wrote: I don't think either is non-idiomatic, but I'd probably just use the map. It's shorter and simpler code, more widely interoperable with other Clojure facilities, and the member access speedup using a record is unlikely to matter much in code that is blocked on I/O nearly all of the time anyway. Some very good points. I've decided to remove the record and go with a map for now. - James -- 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
Idiomatic use of records vs. maps
The documentation on records seems to indicate that they be used in place of structs, but is there any advantage to using a record if no protocols are assigned to it? For example, I've defined a TcpServer record as part of a library I'm developing: (defrecord TcpServer [port host backlog handler socket connections]) (defn tcp-server [ {:as options}] {:pre [(:port options) (:handler options)]} (TcpServer. (:port options) (:host options 127.0.0.1) (:backlog options 50) (:handler options) (atom nil) (atom #{}))) Is this idiomatic Clojure, or should I just use a map instead: (defn tcp-server [ {:as options}] (merge {:host 127.0.0.1 :backlog 50 :socket (atom nil) :connections (atom #{}) options)) - James -- 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: Idiomatic use of records vs. maps
On Sun, May 29, 2011 at 8:56 PM, James Reeves jree...@weavejester.com wrote: The documentation on records seems to indicate that they be used in place of structs, but is there any advantage to using a record if no protocols are assigned to it? Access to a record's in-definition members should be faster than access to arbitrary keys in a map, but that's it if no protocols are used. For example, I've defined a TcpServer record as part of a library I'm developing: (defrecord TcpServer [port host backlog handler socket connections]) (defn tcp-server [ {:as options}] {:pre [(:port options) (:handler options)]} (TcpServer. (:port options) (:host options 127.0.0.1) (:backlog options 50) (:handler options) (atom nil) (atom #{}))) Is this idiomatic Clojure, or should I just use a map instead: (defn tcp-server [ {:as options}] (merge {:host 127.0.0.1 :backlog 50 :socket (atom nil) :connections (atom #{}) options)) I don't think either is non-idiomatic, but I'd probably just use the map. It's shorter and simpler code, more widely interoperable with other Clojure facilities, and the member access speedup using a record is unlikely to matter much in code that is blocked on I/O nearly all of the time anyway. If you were using it in CPU-intensive work, e.g. a complex number datatype in heavy number crunching, then the speedup would matter (although in that specific instance a 1.3 primitive-double vector of length 2 would probably beat a record for speed unless records or deftypes can have unboxed primitive fields and you can access these without boxing in 1.3). -- 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