You can also use destructuring for this:
(def nested-ds ["foo", {:hi "there" :hello ["buddy"]}, "hi"])
(let [[foo {there :hi [buddy] :hello} hi] nested-ds]
(print foo there buddy hi))
James
On Jul 21, 6:31 am, Moses <[email protected]> wrote:
> I come primarily from a perl programming background, but am trying
> to learn Clojure.
>
> I'm looking for a clojure equivalent to the following.
>
> Perl:
>
> my $nestedDS = [ "foo", { hi => there, hello => ["buddy"] }, "hi"]
>
> my $foo = $nestedDS->[0];
> my $there = $nestedDS->[1]->{hi};
> my $hello = $nestedDS->[1]->{hello};
> my $hi = $nestedDS->[2];
>
> print "$foo $there $buddy $hi";
>
> I'm not sure if it exists or not, but hopefully if it doesn't it
> can be created with a macro.
>
> To clarify, I'm looking for a function/macro that makes use of a
> small DSL for accessing fields within the basic Clojure data
> structures.
>
> The best I can come up with in Clojure for a DS equivalent to the
> above is:
>
> (let [nestedDS ["foo", {"hi" "there", "hello", ["buddy"]}, "hi"]
> foo (nth nestedDS 0)
> there (get (nth nestedDS 1) "hi")
> buddy (first (get (nth nestedDS 1) "hello"))
> hi (nth nestedDS 2)]
> (print foo there buddy hi)
> )
>
> Its not that much longer, but if the fields were more deeply nested,
> it would become a bit pretty annoying.
>
> Does any kind of DS access macro exist? If not, how hard do you
> think it would be to write?
>
> Something like
>
> buddy (get-it nestedDS "[1]-{hello}-[0]")
>
> Or perhaps an even shorter syntax is possible?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---