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 {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) tail more))
> I still cannot understand what is going on in this code. juxt always
> make my head crashed.
>
> Thanks.
>
> 2011/3/11 Damien Lepage <damienlep...@gmail.com>:
>> 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]
>>   (if-let [more (butlast args)]
>>       (let [k (last more), v (last args)]
>>         (if-let [even-more (butlast more)]
>>           (apply enmap arg (concat even-more (list (hash-map k v))))
>>           (enmap arg (hash-map k v))))
>>       (apply hash-map arg args)))
>> Two things bother me:
>>
>> Is there a way to make this function less complicated? without recursion
>> maybe?
>> Is there something simpler than (concat even-more (list (hash-map k v)) to
>> append an element at the end of a sequence?
>>
>> Thanks
>> --
>> Damien
>>
>> --
>> 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

Reply via email to