The `reduced?` check is there in case somebody returns a `reduced` as
acc value from the reducing function, as a way to terminate the
reduction early:

user=> (reductions (fn [_ x] (if (= 10 x) (reduced x) x)) (range))
(0 1 2 3 4 5 6 7 8 9 10)

deref is the way to retrieve the value of a reduced object:
user=> (deref (reduced 1))
1


Alex Eberts writes:

> Hi All,
>
> Could someone please explain why reduced? is being used in the reductions
> function (below)? From what I understand reduced? checks if there has been
> a call to reduced and I don't see where that might be happening.
>
> Also, why is the deref macro being used with init?
>
> thanks,
> Alex
>
>
> (defn reductions
>   "Returns a lazy seq of the intermediate values of the reduction (as
>   per reduce) of coll by f, starting with init."
>   {:added "1.2"}
>   ([f coll]
>      (lazy-seq
>       (if-let [s (seq coll)]
>         (reductions f (first s) (rest s))
>         (list (f)))))
>   ([f init coll]
>      (if (reduced? init)
>        (list @init)
>        (cons init
>              (lazy-seq
>               (when-let [s (seq coll)]
>                 (reductions f (f init (first s)) (rest s))))))))
>
> from:
> https://github.com/clojure/clojure/blob/clojure-1.7.0/src/clj/clojure/core.clj#L6921

--

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to