Re: Best practice - (:keyword map) versus (map :keyword)
Generally, if the map is behaving like small struct, and you are accessing fields of it, (:keyword map) is idiomatic, and mirrors (.fields object). If the map is used like a data-structure, or a mapping function, then (map :keyword) is more idiomatic. Note that if you are using defrecords, (:keyword record) performs better, because the clojure compiler generates some shim implementations that let the underlying field get accessed directly. On Tue, Jan 29, 2013 at 2:00 AM, Jonathon McKitrick jmckitr...@gmail.comwrote: Either one works, is there any kind of guideline on which to prefer, when? Or is it entirely personal preference? It seems each way could be more readable in different circumstances -- -- 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/groups/opt_out. -- -- 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/groups/opt_out.
Re: Best practice - (:keyword map) versus (map :keyword)
a) If your map represents an record with fields (for instance if it would make sense to also represent it as record via defrecord), then use (:field map) b) If your map is a collection of key/vals, then use (map key) Rationale for a): it will allow better migration path if you use defrecords at some point, and conveys the semantic that you know there should be the given key in the map Rationale for b): you have an existing map, but the key may or may not be present. Note that you can also use the version which returns default value if there's no key in the map: (map key default-value) Rich already mentioned all this, better, in an earlier email which I'm too lazy to search for right now. -- Laurent 2013/1/29 Jonathon McKitrick jmckitr...@gmail.com: Either one works, is there any kind of guideline on which to prefer, when? Or is it entirely personal preference? It seems each way could be more readable in different circumstances -- -- 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/groups/opt_out. -- -- 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/groups/opt_out.
Re: Best practice - (:keyword map) versus (map :keyword)
For completeness sake... Am Dienstag, 29. Januar 2013 10:31:18 UTC+1 schrieb lpetit: Rationale for b): you have an existing map, but the key may or may not be present. Note that you can also use the version which returns default value if there's no key in the map: (map key default-value) Also, key may be of arbitrary type. Not necessarily a keyword. Meikel -- -- 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/groups/opt_out.
Re: Best practice - (:keyword map) versus (map :keyword)
You have 3 choices, all supporting a -n optional default value: 1. (:kw m), 2. (m :kw), 3. (get m :kw) If your keyword is literal, always pick #1. So if your keyword is not literal then you are left with either (m x) or (get m x) -- remember x is not litteral. The later version will work with maps, records, vectors, sets and nil -- or even anything. The former version will work with anything which is a function (maps, sets, vectors and... fns). So it boils down to: do you consider your map as a handy shorthand for a fn (pick #2) or as a collection (pick #3) Christophe On Tue, Jan 29, 2013 at 11:24 AM, Meikel Brandmeyer (kotarak) m...@kotka.dewrote: For completeness sake... Am Dienstag, 29. Januar 2013 10:31:18 UTC+1 schrieb lpetit: Rationale for b): you have an existing map, but the key may or may not be present. Note that you can also use the version which returns default value if there's no key in the map: (map key default-value) Also, key may be of arbitrary type. Not necessarily a keyword. Meikel -- -- 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/groups/opt_out. -- On Clojure http://clj-me.cgrand.net/ Clojure Programming http://clojurebook.com Training, Consulting Contracting http://lambdanext.eu/ -- -- 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/groups/opt_out.
Best practice - (:keyword map) versus (map :keyword)
Either one works, is there any kind of guideline on which to prefer, when? Or is it entirely personal preference? It seems each way could be more readable in different circumstances -- -- 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/groups/opt_out.
Re: Best practice - (:keyword map) versus (map :keyword)
(:Keyword map) is preferred, because keyword itself can never be nil, while it's possible for map, you might be interested to have a look at: https://github.com/bbatsov/clojure-style-guide From: Jonathon McKitrick jmckitr...@gmail.com Reply-To: clojure@googlegroups.com Date: Mon, 28 Jan 2013 18:00:01 -0800 (PST) To: clojure@googlegroups.com Subject: Best practice - (:keyword map) versus (map :keyword) Either one works, is there any kind of guideline on which to prefer, when? Or is it entirely personal preference? It seems each way could be more readable in different circumstances -- -- 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/groups/opt_out. -- -- 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/groups/opt_out.
Re: Best practice - (:keyword map) versus (map :keyword)
I would use (map :keyword) myself, for that exact reason(because I'm into fail-fast), but only when I know the map is expected to never be nil at this point(but likely I'll do the program in such a way that this point won't be reached with a nil map in the first place), so that if it happens that the map is nil it will throw exception at this point and it will stop the bug propagating further, but if I were to use (:keyword map) it may err later or never. Sure I could use other ways to test for nil, but just in case I forget to do so, (map :keyword) would save me where (:keyword map) would just continue. I note that it's idiomatic clojure to use the latter variant (ie. non-fail-fast). - says the guy(me) that hasn't done any program(in clojure anyway) On Tue, Jan 29, 2013 at 3:09 AM, James Xu xumingming64398...@gmail.comwrote: (:Keyword map) is preferred, because keyword itself can never be nil, while it's possible for map, you might be interested to have a look at: https://github.com/bbatsov/clojure-style-guide From: Jonathon McKitrick jmckitr...@gmail.com Reply-To: clojure@googlegroups.com Date: Mon, 28 Jan 2013 18:00:01 -0800 (PST) To: clojure@googlegroups.com Subject: Best practice - (:keyword map) versus (map :keyword) Either one works, is there any kind of guideline on which to prefer, when? Or is it entirely personal preference? It seems each way could be more readable in different circumstances -- -- 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/groups/opt_out. -- -- 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/groups/opt_out. -- Please correct me if I'm wrong or incomplete, even if you think I'll subconsciously hate it. -- -- 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/groups/opt_out.
Re: Best practice - (:keyword map) versus (map :keyword)
Also, :keyword is a function, whereas the object you're passing to it may not be a array-map or hash-map (both functions), but a record or other type implementing the map interface. --- Joseph Smith j...@uwcreations.com @solussd On Jan 28, 2013, at 8:09 PM, James Xu xumingming64398...@gmail.com wrote: (:Keyword map) is preferred, because keyword itself can never be nil, while it's possible for map, you might be interested to have a look at: https://github.com/bbatsov/clojure-style-guide From: Jonathon McKitrick jmckitr...@gmail.com Reply-To: clojure@googlegroups.com Date: Mon, 28 Jan 2013 18:00:01 -0800 (PST) To: clojure@googlegroups.com Subject: Best practice - (:keyword map) versus (map :keyword) Either one works, is there any kind of guideline on which to prefer, when? Or is it entirely personal preference? It seems each way could be more readable in different circumstances -- -- 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/groups/opt_out. -- -- 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/groups/opt_out. -- -- 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/groups/opt_out.
Re: Best practice - (:keyword map) versus (map :keyword)
The problem with this approach is if your map is a record (e.g. made with defrecord) it will not work. --- Joseph Smith j...@uwcreations.com @solussd On Jan 28, 2013, at 8:31 PM, AtKaaZ atk...@gmail.com wrote: I would use (map :keyword) myself, for that exact reason(because I'm into fail-fast), but only when I know the map is expected to never be nil at this point(but likely I'll do the program in such a way that this point won't be reached with a nil map in the first place), so that if it happens that the map is nil it will throw exception at this point and it will stop the bug propagating further, but if I were to use (:keyword map) it may err later or never. Sure I could use other ways to test for nil, but just in case I forget to do so, (map :keyword) would save me where (:keyword map) would just continue. I note that it's idiomatic clojure to use the latter variant (ie. non-fail-fast). - says the guy(me) that hasn't done any program(in clojure anyway) On Tue, Jan 29, 2013 at 3:09 AM, James Xu xumingming64398...@gmail.com wrote: (:Keyword map) is preferred, because keyword itself can never be nil, while it's possible for map, you might be interested to have a look at: https://github.com/bbatsov/clojure-style-guide From: Jonathon McKitrick jmckitr...@gmail.com Reply-To: clojure@googlegroups.com Date: Mon, 28 Jan 2013 18:00:01 -0800 (PST) To: clojure@googlegroups.com Subject: Best practice - (:keyword map) versus (map :keyword) Either one works, is there any kind of guideline on which to prefer, when? Or is it entirely personal preference? It seems each way could be more readable in different circumstances -- -- 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/groups/opt_out. -- -- 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/groups/opt_out. -- Please correct me if I'm wrong or incomplete, even if you think I'll subconsciously hate it. -- -- 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/groups/opt_out. -- -- 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/groups/opt_out.
Re: Best practice - (:keyword map) versus (map :keyword)
Thank you. In this case(although I have zero xp with records) I would probably opt for making a wrapper function and have it always be used in any instance when records are to be gotten like that, even though I understand it would be a performance hit (maybe I could make it a macro which I could change in release to return the (:keyword record) thing instead of whatever I decide to use to make sure record isn't nil first) On Tue, Jan 29, 2013 at 4:35 AM, Joseph Smith j...@uwcreations.com wrote: The problem with this approach is if your map is a record (e.g. made with defrecord) it will not work. --- Joseph Smith j...@uwcreations.com @solussd On Jan 28, 2013, at 8:31 PM, AtKaaZ atk...@gmail.com wrote: I would use (map :keyword) myself, for that exact reason(because I'm into fail-fast), but only when I know the map is expected to never be nil at this point(but likely I'll do the program in such a way that this point won't be reached with a nil map in the first place), so that if it happens that the map is nil it will throw exception at this point and it will stop the bug propagating further, but if I were to use (:keyword map) it may err later or never. Sure I could use other ways to test for nil, but just in case I forget to do so, (map :keyword) would save me where (:keyword map) would just continue. I note that it's idiomatic clojure to use the latter variant (ie. non-fail-fast). - says the guy(me) that hasn't done any program(in clojure anyway) On Tue, Jan 29, 2013 at 3:09 AM, James Xu xumingming64398...@gmail.comwrote: (:Keyword map) is preferred, because keyword itself can never be nil, while it's possible for map, you might be interested to have a look at: https://github.com/bbatsov/clojure-style-guide From: Jonathon McKitrick jmckitr...@gmail.com Reply-To: clojure@googlegroups.com Date: Mon, 28 Jan 2013 18:00:01 -0800 (PST) To: clojure@googlegroups.com Subject: Best practice - (:keyword map) versus (map :keyword) Either one works, is there any kind of guideline on which to prefer, when? Or is it entirely personal preference? It seems each way could be more readable in different circumstances -- -- 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/groups/opt_out. -- -- 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/groups/opt_out. -- Please correct me if I'm wrong or incomplete, even if you think I'll subconsciously hate it. -- -- 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/groups/opt_out. -- -- 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