Thanks all. So combining a few suggested ideas:
(defn slice
"Return the items in coll at index positions keys.
(slice [0 4 6] \"abcdefg\") => (\\a \\e \\g)"
[keys coll]
(let [max-idx (apply max keys)
keyset (set keys)]
(map second
(filter (fn [[idx _]] (keyset idx))
(take-while (fn [[idx _]] (<= idx max-idx))
(indexed coll))))))
This version has the advantage of working on infinite sequences and not
hanging onto the head. This works as expected:
(slice [200 201] (repeatedly #(int-array 102400)))
without blowing up the stack or throwing OutOfMemory (on my JVM).
Thanks again,
Mark
Mark Triggs <[email protected]> writes:
> 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)
--
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
-~----------~----~----~----~------~----~------~--~---