Re: A syntax question: positional keyword
On Apr 7, 7:56 am, David Nolen dnolen.li...@gmail.com wrote: The runtime cost of destructuring is not worth getting worked up about. It's easy to check this yourself with (time ...) Results below: user= (defn fk [ {:keys [a b c]}] (+ a b c)) user= (defn fp [a b c] (+ a b c)) user= (time (dotimes [_ 100] (fk :a 2 :b 2 :c 2))) Elapsed time: 1582.178 msecs user= (time (dotimes [_ 100] (fp 2 2 2))) Elapsed time: 319.243 msecs Not sure how significant that would be in user code, but I'd rather not have to choose less a less readable version because of the hit. Positional keywords would give the readable function calls without the run-time hit (and we could still use optional keys if they were somehow distinguished in the function calls). -- 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: A syntax question: positional keyword
On Apr 7, 12:37 pm, Armando Blancas armando_blan...@yahoo.com wrote: in other languages they'd be annotations and maybe perceived as redundant, e.g. a call like: (circle x y radius) is readable Ah, but what about: (circle year population income) vs. (circle :x year :y population :r income) In Smtalltalk a single-arg keyword message is readable because the syntax gets the received out of the way to the left: 5.0 raisedTo: 3 where #raisedTo: is both the selector and keyword. (raise base: 5 to: 3) ;; all keyword (raise 5 to: 3) ;; 1st positional + 2nd keyword Smalltalk may actually be the asymmetrical one here: truck moveX: 5 y: 6 vs. truck move x: 5 y: 6 ;; my prefixed version Smalltalk munges the root command name (move) with the keyword for the first argument (X), which is why I showed a prefixed version instead. In Clojure this might be: (move :obj truck :x 5 :y 6) ;; all keyword (move truck :x 5 :y 6) ;; combine position + keyword -- 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: A syntax question: positional keyword
You can easily code positional keyword parameters yourself. It takes only a few minutes for a basic version. Here's an admittedly not very pretty example: http://gist.github.com/360145 -Per On Thu, Apr 8, 2010 at 9:13 PM, Sophie itsme...@hotmail.com wrote: On Apr 7, 7:56 am, David Nolen dnolen.li...@gmail.com wrote: The runtime cost of destructuring is not worth getting worked up about. It's easy to check this yourself with (time ...) Results below: user= (defn fk [ {:keys [a b c]}] (+ a b c)) user= (defn fp [a b c] (+ a b c)) user= (time (dotimes [_ 100] (fk :a 2 :b 2 :c 2))) Elapsed time: 1582.178 msecs user= (time (dotimes [_ 100] (fp 2 2 2))) Elapsed time: 319.243 msecs Not sure how significant that would be in user code, but I'd rather not have to choose less a less readable version because of the hit. Positional keywords would give the readable function calls without the run-time hit (and we could still use optional keys if they were somehow distinguished in the function calls). -- 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 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 To unsubscribe, reply using remove me as the subject.
Re: A syntax question: positional keyword
In my own code I only avoid the convenience of destructuring in the rare tight loops such as calculations intended to drive animations. I've found it to have little effect elsewhere on program performance. As an aside I personally prefer non positional keyword arguments. I find positional ones quite tedious. David On Thursday, April 8, 2010, Sophie itsme...@hotmail.com wrote: On Apr 7, 7:56 am, David Nolen dnolen.li...@gmail.com wrote: The runtime cost of destructuring is not worth getting worked up about. It's easy to check this yourself with (time ...) Results below: user= (defn fk [ {:keys [a b c]}] (+ a b c)) user= (defn fp [a b c] (+ a b c)) user= (time (dotimes [_ 100] (fk :a 2 :b 2 :c 2))) Elapsed time: 1582.178 msecs user= (time (dotimes [_ 100] (fp 2 2 2))) Elapsed time: 319.243 msecs Not sure how significant that would be in user code, but I'd rather not have to choose less a less readable version because of the hit. Positional keywords would give the readable function calls without the run-time hit (and we could still use optional keys if they were somehow distinguished in the function calls). -- 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 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 To unsubscribe, reply using remove me as the subject.
Re: A syntax question: positional keyword
Yes, I meant that :-) But I agree that some more sugar in general in map destructuring could help (and be more DRY). e.g. (defn g [a b {:keys [[c 1] [d 2]]}] [a b c d]) or (defn g [a b {c [:c 1] d [:d 2]]}] [a b c d]) in the general case 2010/4/8 Chris Perkins chrisperkin...@gmail.com: On Apr 7, 5:41 am, Laurent PETIT laurent.pe...@gmail.com wrote: I think defnk is deprecated by the new feature mentioned by Stuart. Do you mean to say that this: user= (defnk f [a b :c 1 :d 2] [a b c d]) #'user/f user= (f 3 4 :d 7) [3 4 1 7] is deprecated in favor of this: user= (defn g [a b {:keys [c d] :or {c 1 d 2}}] [a b c d]) #'user/g user= (g 3 4 :d 7) [3 4 1 7] Eeek, I certainly hope not. - Chris Perkins -- 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 To unsubscribe, reply using remove me as the subject. -- 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: A syntax question: positional keyword
On Apr 8, 11:08 am, David Nolen dnolen.li...@gmail.com wrote: In my own code I only avoid the convenience of destructuring in the rare tight loops such as calculations intended to drive animations. But when you write a function you would have to decide positional vs. keyword. Would you then take a guess about usage in tight loops vs. not? I've found it to have little effect elsewhere on program performance. I suspect you are right. Anyway, I'm very glad to see the style supported, and hope the cleaner defnk version wins, with some ability to further destructure the keyworded arguments. (defnk f [a b :c 1 :d 2] [a b c d]) -- 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: A syntax question: positional keyword
I think defnk is deprecated by the new feature mentioned by Stuart. As far as I remember, positional arguments in defnk do not allow to be used either prefixed by their name, either without, so I guess defnk didn't solve your problem definition. 2010/4/7 Sophie itsme...@hotmail.com: On Apr 6, 7:03 pm, ataggart alex.tagg...@gmail.com wrote: See: http://richhickey.github.com/clojure-contrib/def-api.html#clojure.con... Ah, thank you (all). Will this be in 1.2? Is run-time cost expected to be minor, and will passing unrecognized keys be an error? -- 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 To unsubscribe, reply using remove me as the subject. -- 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: A syntax question: positional keyword
The runtime cost of destructuring is not worth getting worked up about. It's easy to check this yourself with (time ...) David On Wednesday, April 7, 2010, Sophie itsme...@hotmail.com wrote: On Apr 6, 7:03 pm, ataggart alex.tagg...@gmail.com wrote: See: http://richhickey.github.com/clojure-contrib/def-api.html#clojure.con... Ah, thank you (all). Will this be in 1.2? Is run-time cost expected to be minor, and will passing unrecognized keys be an error? -- 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 To unsubscribe, reply using remove me as the subject. -- 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: A syntax question: positional keyword
Just curious - what folks think of fixed-positional-keyword params - whether it was considered for Clojure It's difficult to image that keyword params will be considered in languages where they aren't folklore, as they are in Smalltalk, Objective-C, and Self. Unlike Smtalltalk and Self, where keywords do real work --they name the selector and are separators at the call place-- in other languages they'd be annotations and maybe perceived as redundant, e.g. a call like: (circle x y radius) is readable without keywords. In Smtalltalk a single-arg keyword message is readable because the syntax gets the received out of the way to the left: 5.0 raisedTo: 3 where #raisedTo: is both the selector and keyword. In Clojure it wouldn't look as simple and it'd introduce the inconsistency that the first argument isn't keyed. Another aspect is of course high-order functions, where keywords make things difficult at the call place and imposible inside the called function. Having to make a feature optional when things get tough using it usualy gets it scrapped. Smalltalk's high-order methods don't have a problem because a method to call is passed using its symbol (ie, its keywords) not as value. BTW, which Smalltalk do you use? I've never seen the prefix you mentioned. AFAIK, a keyword selector is just the concat of its keys. -- 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 To unsubscribe, reply using remove me as the subject.
Re: A syntax question: positional keyword
On Apr 7, 12:25 am, Sophie itsme...@hotmail.com wrote: Just curious - what folks think of fixed-positional-keyword params - whether it was considered for Clojure I don't know for certain whether Rich ever considered smalltalk-style parameters, but I doubt it. I do like them, though; in Objective-C and Smalltalk. They're extremely readable and natural in an object-oriented language (or a message-oriented one, anyway). However, I don't think they are a very good fit for a Lisp. Expressing positionally fixed keyword parameters in terns of lists, symbols and keywords feels clunky and unnatural to me, but the greater problem is that they also make some very common functional patterns cumbersome: most notably function application (ie. apply), composition, and higher-order functions. -- 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 To unsubscribe, reply using remove me as the subject.
Re: A syntax question: positional keyword
Have you seen destructuring of rest args in the current master branch? (defn foo [ {:keys [a b c]}] [a b c]) (foo :a 1 :c 3) = [1 nil 3] With this last bit of sugar in place I am extremely happy with Clojure's arg handling. Please don't misunderstand this post - it is not asking for a change of syntax, just trying to understand something. Clojure has chosen positional parameters (just like for Lisp, C, C++, Java, Ruby, Python, Prolog, ...) Smalltalk composes a full method name from a prefix-name + named parameters. [obj] prefix key1: x key2: y key3: z The [obj] part above is an artifact of the single-dispatch model, and is irrelevant to this discussion, so I'll leave it out from here on. Importantly, the method name here is a composite: #prefix:key1:key2:key3 This leads to remarkably readable function calls: 1. (schedule project: p1 after: p2 before: p3 priority: 7) ;; calls schedule:project:after:before:priority vs. 2. (schedule p1 p2 p3 7) Note that 1. in no way requires arbitrary ordering of keywords, maps, de-structuring, etc. The positions are still fixed, just keyword- prefixed. It's like taking the Clojure parameter list (defn schedule [project before after priority] ) and requiring those parameter names (or a formal version thereof which is meant to be part of the interface) at the calling sites. An only-slightly-strained analogy would be only allowing %1 %2 for formal parameters in all function bodies (defn schedule [%1 %2 %3] ( stuff with %1 %2 ...)) or (deftype Person [name age]) and then requiring (1 joe) instead of (:name joe) Just curious - what folks think of fixed-positional-keyword params - whether it was considered for Clojure 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 To unsubscribe, reply using remove me as the subject. -- 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: A syntax question: positional keyword
On Apr 6, 4:46 pm, Jarkko Oranen chous...@gmail.com wrote: problem is that they also make some very common functional patterns cumbersome: most notably function application (ie. apply), composition, and higher-order functions. I don't think it should be either-or (and positional would be needed anyway to call out to Java friends). So your functional patterns could still be done positionally, since the first-class function arguments f g are not named anyway: (let [f ..., g ..., x ...] (apply f g x) (comp f g)) The arg-lists in the defn would distinguish ;; positional-keyword calls needed ;; function name is a composite ;; one defn could create more than one function name (defn schedule [project: p before: b after: a priority: i] ...) from ;; existing defn calling code could even be unchanged ;; no keywords (defn apply (f args* argseq) ...) -- 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 To unsubscribe, reply using remove me as the subject.
Re: A syntax question: positional keyword
On Apr 6, 5:23 pm, Stuart Halloway stuart.hallo...@gmail.com wrote: Have you seen destructuring of rest args in the current master branch? (defn foo [ {:keys [a b c]}] [a b c]) (foo :a 1 :c 3) = [1 nil 3] With this last bit of sugar in place I am extremely happy with Clojure's arg handling. Hmmm. Looks nice, but if these are optional, any-order keys - is there a run-time cost to construct / deconstruct / lookup? - how do I use foo in apply, comp, and friends? 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 To unsubscribe, reply using remove me as the subject.
Re: A syntax question: positional keyword
Don't you think - fixed-order named parameters could (should?) be a separate issue from - optional, any-order, named parameters ? ;; :x :y are fixed order, named, while :a :b are optional, named (defn foo [:x :y {:keys [a b]] [x, y, a, b]) (foo :x 1 :y 2) = [1 2 nil nil] (foo :x 1 :a 2) = error (foo :x 1 :a 3 :y 2) = error (foo :x 1 :y 2 :b 3) = [1 2 nil 3] Note that (defn foo ([:x {:keys [y]] [x nil]) ([:x :y][x y])) would create 2 function symbols e.g. foo:x and foo:x:y -- 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 To unsubscribe, reply using remove me as the subject.
Re: A syntax question: positional keyword
On Apr 6, 2010, at 6:08 PM, Sophie wrote: Don't you think - fixed-order named parameters could (should?) be a separate issue from - optional, any-order, named parameters ? I don't see the advantage of fixed-order named parameters over keyword parameters. Note that you can require certain keyword parameters simply by throwing an exception when they're omitted. -- 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 To unsubscribe, reply using remove me as the subject.
Re: A syntax question: positional keyword
See: http://richhickey.github.com/clojure-contrib/def-api.html#clojure.contrib.def/defnk On Apr 6, 2:25 pm, Sophie itsme...@hotmail.com wrote: Please don't misunderstand this post - it is not asking for a change of syntax, just trying to understand something. Clojure has chosen positional parameters (just like for Lisp, C, C++, Java, Ruby, Python, Prolog, ...) Smalltalk composes a full method name from a prefix-name + named parameters. [obj] prefix key1: x key2: y key3: z The [obj] part above is an artifact of the single-dispatch model, and is irrelevant to this discussion, so I'll leave it out from here on. Importantly, the method name here is a composite: #prefix:key1:key2:key3 This leads to remarkably readable function calls: 1. (schedule project: p1 after: p2 before: p3 priority: 7) ;; calls schedule:project:after:before:priority vs. 2. (schedule p1 p2 p3 7) Note that 1. in no way requires arbitrary ordering of keywords, maps, de-structuring, etc. The positions are still fixed, just keyword- prefixed. It's like taking the Clojure parameter list (defn schedule [project before after priority] ) and requiring those parameter names (or a formal version thereof which is meant to be part of the interface) at the calling sites. An only-slightly-strained analogy would be only allowing %1 %2 for formal parameters in all function bodies (defn schedule [%1 %2 %3] ( stuff with %1 %2 ...)) or (deftype Person [name age]) and then requiring (1 joe) instead of (:name joe) Just curious - what folks think of fixed-positional-keyword params - whether it was considered for Clojure 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 To unsubscribe, reply using remove me as the subject.
Re: A syntax question: positional keyword
On Apr 6, 7:03 pm, ataggart alex.tagg...@gmail.com wrote: See: http://richhickey.github.com/clojure-contrib/def-api.html#clojure.con... Ah, thank you (all). Will this be in 1.2? Is run-time cost expected to be minor, and will passing unrecognized keys be an error? -- 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 To unsubscribe, reply using remove me as the subject.