Can this function be simpler?

2011-03-10 Thread Damien Lepage
Hi I wrote a function to transform a variable number of arguments into embedded maps. Here is what it does: (enmap 1 2) {1 2} (enmap 1 2 3) {1 {2 3}} (enmap 1 2 3 4) {1 {2 {3 4}}} (enmap 1 2 3 4 {5 6 7 8}) {1 {2 {3 {4 {5 6, 7 8} Here is my implementation: (defn enmap [arg args]

Re: Can this function be simpler?

2011-03-10 Thread Alan
(let [[tail more] ((juxt last (comp reverse butlast)) [1 2 3 4 {5 6 7 8}])] (reduce #(hash-map %2 %1) tail more)) {1 {2 {3 {4 {5 6, 7 8} On Mar 10, 9:46 am, Damien Lepage damienlep...@gmail.com wrote: Hi I wrote a function to transform a variable number of arguments into embedded

Re: Can this function be simpler?

2011-03-10 Thread Aaron Cohen
On Thu, Mar 10, 2011 at 12:46 PM, Damien Lepage damienlep...@gmail.com wrote: Hi I wrote a function to transform a variable number of arguments into embedded maps. Here is what it does: (enmap 1 2) {1 2} (enmap 1 2 3) {1 {2 3}} (enmap 1 2 3 4) {1 {2 {3 4}}} (enmap 1 2 3 4 {5 6 7 8})

Re: Can this function be simpler?

2011-03-10 Thread Chris Perkins
On Mar 10, 12:46 pm, Damien Lepage damienlep...@gmail.com wrote: Hi I wrote a function to transform a variable number of arguments into embedded maps. Here is what it does: (enmap 1 2) {1 2} (enmap 1 2 3) {1 {2 3}} (enmap 1 2 3 4) {1 {2 {3 4}}} (enmap 1 2 3 4 {5 6 7 8}) {1 {2 {3

Re: Can this function be simpler?

2011-03-10 Thread Alan
On Mar 10, 10:05 am, Aaron Cohen aa...@assonance.org wrote: On Thu, Mar 10, 2011 at 12:46 PM, Damien Lepage damienlep...@gmail.com wrote: Hi I wrote a function to transform a variable number of arguments into embedded maps. Here is what it does: (enmap 1 2) {1 2} (enmap 1 2 3)

Re: Can this function be simpler?

2011-03-10 Thread Aaron Cohen
On Thu, Mar 10, 2011 at 12:46 PM, Damien Lepage damienlep...@gmail.com wrote: Hi           (apply enmap arg (concat even-more (list (hash-map k v Is there something simpler than (concat even-more (list (hash-map k v)) to append an element at the end of a sequence? To answer your second

Re: Can this function be simpler?

2011-03-10 Thread Jason Wolfe
   - Is there a way to make this function less complicated? without    recursion maybe? Looks like you're covered on this one.    - Is there something simpler than (concat even-more (list (hash-map k    v)) to append an element at the end of a sequence? Clojure is opinionated in this sense.

Re: Can this function be simpler?

2011-03-10 Thread Takahiro
Interesting. Here is my attempt. (defn enmap [args] (let [[fs res] (reverse args)] (reduce (fn [v k] (hash-map k v)) fs res))) (enmap [1 2 3 4 {5 6 7 8}]) = {1 {2 {3 {4 {5 6, 7 8} (let [[tail more] ((juxt last (comp reverse butlast)) [1 2 3 4 {5 6 7 8}])] (reduce #(hash-map %2 %1)

Re: Can this function be simpler?

2011-03-10 Thread Takahiro
more concise: (defn enmap [args] (reduce #(hash-map %2 %1) (reverse args))) 2011/3/11 Takahiro fat...@googlemail.com: Interesting. Here is my attempt. (defn enmap [args]  (let [[fs res] (reverse args)]    (reduce (fn [v k] (hash-map k v)) fs res))) (enmap [1 2 3 4 {5 6 7 8}]) = {1 {2

Re: Can this function be simpler?

2011-03-10 Thread Damien Lepage
Thanks a lot for your help, there's an awesome community here. Sorry for the dumb questions, I'll try no to be too noisy on this list. BTW, shouldn't it be better to create a separate mailing list for beginners? Well, we would still need some experienced clojurers to answer the questions though

Re: Can this function be simpler?

2011-03-10 Thread Alan
Ah, thanks. I think the pulling-apart of last and butlast was a remnant of my trying to do this with foldl instead of foldr :P. On Mar 10, 10:38 am, Takahiro fat...@googlemail.com wrote: more concise: (defn enmap [args]   (reduce #(hash-map %2 %1) (reverse args))) 2011/3/11 Takahiro

Re: Can this function be simpler?

2011-03-10 Thread Alan
Thanks a lot for your help, there's an awesome community here. Sorry for the dumb questions, I'll try no to be too noisy on this list. BTW, shouldn't it be better to create a separate mailing list for beginners? Well, we would still need some experienced clojurers to answer the questions

Re: Can this function be simpler?

2011-03-10 Thread Saul Hazledine
On Mar 10, 7:48 pm, Damien Lepage damienlep...@gmail.com wrote: Sorry for the dumb questions, I'll try no to be too noisy on this list. I found this thread useful. Please keep asking questions. Saul -- You received this message because you are subscribed to the Google Groups Clojure group. To

Re: Can this function be simpler?

2011-03-10 Thread Armando Blancas
Real nice and short. Doing [ args] avoids the vector: user= (enmap 1 2 3 4 {5 6 7 8}) {1 {2 {3 {4 {5 6, 7 8} On Mar 10, 10:38 am, Takahiro fat...@googlemail.com wrote: more concise: (defn enmap [args]   (reduce #(hash-map %2 %1) (reverse args))) 2011/3/11 Takahiro

Re: Can this function be simpler?

2011-03-10 Thread Meikel Brandmeyer
Hi, here something different. But also using the ugly butlast and last. user= (defn enmap [ args] (assoc-in {} (butlast args) (last args))) #'user/enmap user= (enmap 1 2 3 4 {5 6 7 8}) {1 {2 {3 {4 {5 6, 7 8} Sincerely Meikel -- You received this message because you are subscribed to the