For a genetic algorithm I'm writing, I need to sub-sequence a list, but I need it to be lazy so I can compose it with other lazy functions. If I introduce strictness into the chain, I risk potentially massive slow-downs since each step in the chain requires a full traversal of the population.
Amazingly, such a function doesn't appear to built into the
core, so I needed to write one:
(defn lsubseq "Lazily sub-sequences any iterable collection. The left-index is inclusive, while the right is exclusive." [coll left-index right-index] (map second (filter #(<= left-index (first %) (dec right-index)) (map vector (range) coll))))
I find this to be simultaneously atrocious and beautiful. It works exactly as I expected, so I'm happy with it in that regard.
What I'm not really crazy about is the need to enumerate the collection, only to strip the enumerations before returning. I know it's lazy, so the overhead of this should be minimal, but it still seems like a roundabout of achieving this.
What I want reviews on:
Is there really no built-in for this? This seems like something that I would expect in a standard library.
Are there any improvements that could be made?