Re: Incomplete With Destructuring Lists
There is also this sort of solution: (def xs (range 9)) (let [ [[a b c] mid3 [d e f]] (partition 3 xs) ] [a b c mid3 d e f]) => (0 1 2 (3 4 5) 6 7 8) -- 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: Incomplete With Destructuring Lists
Output: (foo 1 2 3 4 5 6 7 8 9) => [a b c] => [1 2 3] mid3 => (4 5 6) [g h i] => [7 8 9] (foo 1 2 3 4 5) => [a b c] => [1 2 3] mid3 => (4 5) [g h i] => [nil nil nil] (foo 1 2 3 4 5 6 7 8 9 10) => java.lang.IllegalArgumentException: Too many args:(1 2 3 4 5 6 7 8 9 10) On Wed, May 10, 2017 at 9:04 PM, Alan Thompson wrote: > Don't try to force the destructuring DSL to do more than it was intended > to do. Just do it manually (or write a macro if you really insist): > > > > (defn foo > [& args] > (let [ [a b c] (take 3 args) > mid3 (take 3 (drop 3 args)) > [g h i] (drop 6 args) ] > (when (< 9 (count args)) > (throw (IllegalArgumentException. (str "Too many args:" args)) > > (foo 1 2 3 4 5 6 7 8 9) => nil > (foo 1 2 3 4 5) => nil > (foo 1 2 3 4 5 6 7 8 9 10) => java.lang.IllegalArgumentException: Too > many args:(1 2 3 4 5 6 7 8 9 10), > > > > On Wed, May 10, 2017 at 6:17 PM, Kevin Kleinfelter < > kleinfelter.gro...@gmail.com> wrote: > >> Can I destructure a list, assigning the first and last few items >> individually, while putting several items from the middle into a list? >> >> I see that I can assign a list: >> (let [a '( 1 2 3 4)] ...) >> >> I can destructure the list as a whole: >> (let [[a b c d] '(1 2 3 4)] ...) >> >> I can destructure the list and bundle the TRAILING elements into a list: >> (let [[a & stuff] '(1 2 3 4)] ...)) >> >> But suppose I had a list of 9 items and I wanted the first 3 >> individually, 3 in the MIDDLE as a list, and the last 3 individually. >> Something like: >> (let [[a b c & middle d e f] '( 1 2 3 4 5 6 7 8 9)] ...) >> >> How do I do that? >> >> Tnx. >> >> -- >> 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: Incomplete With Destructuring Lists
Don't try to force the destructuring DSL to do more than it was intended to do. Just do it manually (or write a macro if you really insist): (defn foo [& args] (let [ [a b c] (take 3 args) mid3 (take 3 (drop 3 args)) [g h i] (drop 6 args) ] (when (< 9 (count args)) (throw (IllegalArgumentException. (str "Too many args:" args)) (foo 1 2 3 4 5 6 7 8 9) => nil (foo 1 2 3 4 5) => nil (foo 1 2 3 4 5 6 7 8 9 10) => java.lang.IllegalArgumentException: Too many args:(1 2 3 4 5 6 7 8 9 10), On Wed, May 10, 2017 at 6:17 PM, Kevin Kleinfelter < kleinfelter.gro...@gmail.com> wrote: > Can I destructure a list, assigning the first and last few items > individually, while putting several items from the middle into a list? > > I see that I can assign a list: > (let [a '( 1 2 3 4)] ...) > > I can destructure the list as a whole: > (let [[a b c d] '(1 2 3 4)] ...) > > I can destructure the list and bundle the TRAILING elements into a list: > (let [[a & stuff] '(1 2 3 4)] ...)) > > But suppose I had a list of 9 items and I wanted the first 3 individually, > 3 in the MIDDLE as a list, and the last 3 individually. Something like: > (let [[a b c & middle d e f] '( 1 2 3 4 5 6 7 8 9)] ...) > > How do I do that? > > Tnx. > > -- > 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.
Incomplete With Destructuring Lists
Can I destructure a list, assigning the first and last few items individually, while putting several items from the middle into a list? I see that I can assign a list: (let [a '( 1 2 3 4)] ...) I can destructure the list as a whole: (let [[a b c d] '(1 2 3 4)] ...) I can destructure the list and bundle the TRAILING elements into a list: (let [[a & stuff] '(1 2 3 4)] ...)) But suppose I had a list of 9 items and I wanted the first 3 individually, 3 in the MIDDLE as a list, and the last 3 individually. Something like: (let [[a b c & middle d e f] '( 1 2 3 4 5 6 7 8 9)] ...) How do I do that? Tnx. -- 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.