Public Vars

Back

lazy-seq (clj)

(source)

macro

(lazy-seq & body)
Takes a body of expressions that returns an ISeq or nil, and yields a Seqable object that will invoke the body only the first time seq is called, and will cache the result and return it on all subsequent seq calls. See also - realized?

Examples

clojure
(ns clojure.test-clojure.reducers
  (:require [clojure.core.reducers :as r]
            [clojure.test.generative :refer (defspec)]
            [clojure.data.generators :as gen])
  (:use clojure.test))

(deftest test-mapcat-obeys-reduced
  (is (= [1 "0" 2 "1" 3]
        (->> (concat (range 100) (lazy-seq (throw (Exception. "Too eager"))))
          (r/mapcat (juxt inc str))
          (r/take 5)
          (into [])))))
Netflix/PigPen
(ns pigpen.extensions.core-async-test
  (:use clojure.test
        pigpen.extensions.core-async)
  (:require [clojure.core.async :as a]))

(deftest test-chan->lazy-seq
  
  (testing "normal"
    (let [c (a/chan 10)]
      (a/go (safe->! c 1)
            (safe->! c 2)
            (safe->! c 3)
            (a/close! c))
      (is (= (chan->lazy-seq c)
             [1 2 3]))))
  
  (testing "with nil"
    (let [c (a/chan 10)]
      (a/go (safe->! c 1)
            (safe->! c nil)
            (safe->! c 3)
            (a/close! c))
      (is (= (chan->lazy-seq c)
             [1 nil 3])))))
uncomplicate/fluokitten
(ns uncomplicate.fluokitten.articles.fluokitten-extensions-clojure-core-test
  "These expressions are used as examples in the
    Fluokitten Extensions of Clojure Core
    article at the Fluokitten web site."
  (:use [uncomplicate.fluokitten jvm core test utils])
  (:require [clojure.core.reducers :as r])
  (:use [midje.sweet :exclude [just]]))

 (fmap inc (lazy-seq [])) => (lazy-seq [])

 (fmap inc (lazy-seq [1 2 3])) => (lazy-seq [2 3 4])

 (fmap + (lazy-seq [1 2])
       (lazy-seq [3 4 5])
       (lazy-seq [6 7 8]))
 => (lazy-seq [10 13])

 (pure (lazy-seq [4]) 1) => (lazy-seq [1])

 (fapply (lazy-seq []) (lazy-seq []))
 => (lazy-seq [])

 (fapply (lazy-seq []) (lazy-seq [1 2 3]))
 => (lazy-seq [])

 (fapply (lazy-seq [inc dec]) (lazy-seq []))
 => (lazy-seq [])

 (fapply (lazy-seq [inc dec]) (lazy-seq [1 2 3]))
 => (lazy-seq [2 3 4 0 1 2])

 (fapply (lazy-seq [+ *])
         (lazy-seq [1 2 3])
         (lazy-seq [4 5 6]))
 => (lazy-seq [5 7 9 4 10 18])

 (bind (lazy-seq []) increment)
 => (lazy-seq [])

 (bind (lazy-seq [1 2 3]) increment)
 => (lazy-seq [2 3 4])

 (bind (lazy-seq [1 2 3]) (lazy-seq [4 5 6]) add)
 => (lazy-seq [5 7 9])

 (join (lazy-seq (list (list 1 2) (list 3 (list 4 5) 6))))
 => (lazy-seq (list 1 2 3 (list 4 5) 6))

 (op (lazy-seq [1 2 3]) (lazy-seq [4 5 6]))
 => (lazy-seq [1 2 3 4 5 6])

 (op (lazy-seq [1 2 3]) (lazy-seq [4 5 6])
     (lazy-seq [7 8 9]) (lazy-seq [10 11 12]))
 => (lazy-seq [1 2 3 4 5 6 7 8 9 10 11 12])

 (id (lazy-seq [1 23])) => (lazy-seq [])

 (fold (lazy-seq [[1] (list 2) (seq [3])])) => [1 2 3]
uncomplicate/fluokitten
(ns uncomplicate.fluokitten.core-test
  (:use [uncomplicate.fluokitten algo jvm core test utils])
  (:use [midje.sweet :exclude [just]])
  (:require [clojure.string :as s]
            [clojure.core.reducers :as r]))

(functor-law2 inc (partial * 100) (lazy-seq (list 78 -3 5)))

(functor-law2 inc (partial * 100) (lazy-seq (list 78 -3 5)) (lazy-seq (list 88 0 -4)))

(fmap-keeps-type inc (lazy-seq (list 8 9 -2)))

(fmap-keeps-type + (lazy-seq (list 8 9 -2)) (lazy-seq (list 18 5 -92)))

(applicative-law1 inc (lazy-seq (list 3 9 0)))

(applicative-law1 + (lazy-seq (list 3 9 0)) (lazy-seq (list -4 -5)))

(applicative-law2-identity (lazy-seq (list 3 -79 29)))

(applicative-law3-composition (lazy-seq (list inc)) (lazy-seq (list (partial * 10)))
                              (lazy-seq (list 3 -1 0)))

(applicative-law3-composition (lazy-seq (list inc)) (lazy-seq (list (partial * 10)))
                              (lazy-seq (list 3 -1 0)) (lazy-seq (list 9)))

(applicative-law4-homomorphism (lazy-seq (list 3)) inc 3)

(applicative-law4-homomorphism (lazy-seq (list 3)) + 3 -2)

(applicative-law5-interchange (lazy-seq (list 3)) inc 3)

(applicative-law5-interchange (lazy-seq (list 3)) + 3 -2 -3)

(fapply-keeps-type inc (lazy-seq (list 3 -4 9)))

(fapply-keeps-type + (lazy-seq (list 3 -4 9)) (lazy-seq (list -5 -5 -5)))

(fapply-keeps-type + (lazy-seq (list 3 -4 9)) (lazy-seq (list -5 -5 -5)))

(monad-law1-left-identity (lazy-seq (list)) (comp list inc) 3)

(monad-law1-left-identity (lazy-seq (list)) (comp list +) 3 49 9)

(monad-law2-right-identity (lazy-seq (list 3 2 -33)))

(monad-law3-associativity (comp list inc) (comp list (partial * 10)) (lazy-seq (list 3 -3 -88)))

(extract-is-dual-of-pure [0] 1)
(extract-is-dual-of-pure (double-array [0]) 1.0)
(extract-is-dual-of-pure (list 0) 1)
(extract-is-dual-of-pure (reducible []) 1)
(extract-is-dual-of-pure (lazy-seq []) 1)
(extract-is-dual-of-pure (eduction []) 1)
(extract-is-dual-of-pure (seq []) 1)
(extract-is-dual-of-pure #{0} 1)
(extract-is-dual-of-pure {:a :b} 1)
(extract-is-dual-of-pure (first {:a :b}) 1)
(extract-is-dual-of-pure (atom 0) 1)
(extract-is-dual-of-pure (ref 0) 1)
(extract-is-dual-of-pure (volatile! 0) 1)
(extract-is-dual-of-pure + 1)
(extract-is-dual-of-pure (just 0) 1)

(magma-op-keeps-type (lazy-seq (list 1 2)) (lazy-seq (list 3 4)))

(magma-op-keeps-type (lazy-seq (list 1 2)) (lazy-seq (list 3 4))
                     (lazy-seq (list 5 6)) (lazy-seq (list 7 8)))

(semigroup-op-associativity (lazy-seq (list 1 2)) (lazy-seq (list 3 4)))

(semigroup-op-associativity (lazy-seq (list 1 2)) (lazy-seq (list 3 4))
                            (lazy-seq (list 5 6)) (lazy-seq (list 7 8)))

(monoid-identity-law (lazy-seq (list 1 2)))

(facts "Monoids are used by collection fold."
       (fold [[1] [2]]) => [1 2]
       (fold [(list 1) (list 2)]) => (list 1 2)
       (fold [(seq (list 1)) (seq (list 2))]) => (seq (list 1 2))
       (fold [(lazy-seq (list 1)) (lazy-seq (list 2))]) => (lazy-seq (list 1 2))
       (fold [#{1} #{2}]) => #{1 2}
       (fold [{:a 1} {:b 2}]) => {:a 1 :b 2}
       (fold [:a :b :c]) => :abc
       (fold ["a" "b" "c"]) => "abc"
       (fold [1 2 3]) => 6
       (fold [(just 1) (just 2) (just 3)]) => (just 6)
       (fold [(atom 1) (atom 2) (atom 3)]) => (check-eq (atom 6))
       (fold [(ref 1) (ref 2) (ref 3)]) => (check-eq (ref 6))
       (fold [(first {:a 1}) (first {:a 2}) (first {:b 3})]) => (check-eq (first {:aab 6})))

(facts "How Foldable collections work."
       (fold [1 2 3 4 5]) => 15
       (fold (vec (range 999))) => 498501
       (fold []) => nil
       (fold (list 1 2 3 4 5))= > 15
       (fold (r/map inc [1 2 3 4 5])) => 20
       (fold (list 1 2 3 4 5)) => 15
       (fold (lazy-seq (list 1 2 3 4 5))) => 15
       (fold (eduction (list 1 2 3 4 5))) => 15
       (fold (seq (list 1 2 3 4 5))) => 15
       (fold {:a 1 :b 2 :c 3}) => 6
       (fold #{1 2 3 4 5}) => 15

       (foldmap inc [1 2 3 4 5]) => 20
       (foldmap inc []) => nil
       (foldmap inc (list 1 2 3 4 5))= > 20
       (foldmap inc (r/map inc [1 2 3 4 5])) => 25
       (foldmap inc (list 1 2 3 4 5)) => 20
       (foldmap inc (lazy-seq (list 1 2 3 4 5))) => 20
       (foldmap inc (eduction (list 1 2 3 4 5))) => 20
       (foldmap inc (seq (list 1 2 3 4 5))) => 20
       (foldmap inc {:a 1 :b 2 :c 3}) => 9
       (foldmap inc #{1 2 3 4 5}) => 20

(data-structures-should-preserve-metadata inc + #(lazy-seq (list %))
                                          (lazy-seq (list 1 2 3)) (lazy-seq (list 4 5 6)))
clojurecup2014/parade-route
(ns clojure.test-clojure.reducers
  (:require [clojure.core.reducers :as r])
  (:use clojure.test))

(deftest test-mapcat-obeys-reduced
  (is (= [1 "0" 2 "1" 3]
        (->> (concat (range 100) (lazy-seq (throw (Exception. "Too eager"))))
          (r/mapcat (juxt inc str))
          (r/take 5)
          (into [])))))
logaan/promise-stream
;; The promise-stream.pstream namespace contains the public API.
(ns promise-stream.walkthrough
  (:use [promise-stream.pstream :only
         [open-pstream closed-pstream append! promise close! fmap mapd* map*]]
        [jayq.core :only [done]]
        [jayq.util :only [log]])
  (:require [clojure.core.reducers :as r])
  (:use-macros [promise-stream.macros :only [for-pstream]]))

  ;; Unfortunately the promise-streams can't let a funciton like `map` know when
  ;; it's hit the end of a the stream using just the `ISeq` protocol. If you try
  ;; to use `reduce` on a lazy-seq returned by `map` then you'll get an
  ;; infinite loop: