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 <[email protected]> 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
> <[email protected]>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---