(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 <[email protected]> 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 {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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en