Details

    • Type: Improvement
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects versions: Release 1.5
    • Fix versions: None
    • Labels:
    • Approval:
      Triaged
    • Patch:
      Code and Test

      Description

      The following expression prints 1234 and returns 1:

      (first (mapcat #(do (print %) [%]) '(1 2 3 4 5 6 7)))
      

      The reason is that (apply concat args) is not maximally lazy in its arguments, and indeed will realize the first four before returning the first item. This in turn is essentially unavoidable for a variadic concat.

      This could either be fixed just in mapcat, or by adding a new function (to clojure.core?) that is a non-variadic equivalent to concat, and reimplementing mapcat with it:

      (defn join
        "Lazily concatenates a sequence-of-sequences into a flat sequence."
        [s]
        (lazy-seq (when-let [[x & xs] (seq s)] (concat x (join xs)))))
      

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              gfredericks gfredericks
            • Votes:
              8 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

              • Created:
                Updated: