Skip to main content
deleted 31 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Clojure - Lazy sub sequence

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. This is what I came up with:

(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:

  1. Is there really no built-in for this? This seems like something that I would expect in a standard library.

  2. Are there any improvements that could be made?

Clojure - Lazy sub sequence

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. This is what I came up with:

(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:

  1. Is there really no built-in for this? This seems like something that I would expect in a standard library.

  2. Are there any improvements that could be made?

Lazy sub sequence

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:

  1. Is there really no built-in for this? This seems like something that I would expect in a standard library.

  2. Are there any improvements that could be made?

Source Link
Carcigenicate
  • 16.3k
  • 3
  • 35
  • 80

Clojure - Lazy sub sequence

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. This is what I came up with:

(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:

  1. Is there really no built-in for this? This seems like something that I would expect in a standard library.

  2. Are there any improvements that could be made?