Actually, now that I think about this, I do have another solution. I've posted a proposal for a proj fn in the dev list. Read up here
http://groups.google.com/group/clojure-dev/browse_thread/thread/155c8b9893d673bc (defn slice-c [& indices] (apply proj (map (fn[index] (fn[coll] (nth coll index))) indices))) user=>((slice-c 0 1 4) (range 1 10)) (1 2 5) Hope this helps. Sean On Aug 17, 9:46 pm, Sean Devlin <francoisdev...@gmail.com> wrote: > My $.02 > > (defn slice > [indices coll] > (let [idx (filter #(< % (count coll) (set indices))] > (map (partial nth coll) idx))) > > I changed the order of your arguments to accommodate map operations > (which you're gonna need to parse a log file). Alternatively, you > could have slice generate a closure > > (defn slice-c > [& indices] > (fn[coll] > (let [idx (filter #(< % (count coll) (set indices))] > (map (partial nth coll) idx)))) > > Sean > > On Aug 17, 9:17 pm, Mark Triggs <mark.h.tri...@gmail.com> wrote: > > > > > Hi all, > > > Is there an idiom for what I'm doing below, or does a function already > > exist? > > > (let [vowels (slice "abcdefghijklmnopqrstuvwxyz" > > 0 4 8 14 20)] > > vowels) > > => (\a \e \i \o \u) > > > A possible implementation: > > > (defn slice > > "Return the items in coll at index positions keys. > > > (slice \"abcdefg\" 0 4 6) => (\\a \\e \\g)" > > [coll & keys] > > (let [keyset (set keys)] > > (for [[idx elt] (indexed coll) > > :when (keyset idx)] > > elt))) > > > I often want something like this when picking apart lines from log files > > (calling .split on a string, then grabbing certain segments) > > > Thanks, > > > Mark > > > -- > > Mark Triggs > > <mark.h.tri...@gmail.com> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---