The source for both functions is pretty simple (use the source!), although 
admittedly `reduced?` does some work to optimize perf that obscures it a 
little.

`reduced` is used by a reducing function to indicate to the outer reducing 
process that a return value should end the reduce. It does this by simply 
wrapping the return value in a Reduced object.

`reduced?` is used by the outer process to detect that wrapper (it just 
checks if it is a Reduced)


On Tuesday, April 25, 2017 at 2:34:05 PM UTC-5, John Gabriele wrote:
>
> Just recently stumbled upon `reduced` and `reduced?`. It seems rather 
> magical... how does the outer `reduce` (or `reductions`) know to stop? That 
> is, how does the function which is being called (`reduced`) affect the 
> function that's calling it (below, `reductions`)? : 
>

> ~~~clojure
> (defn main
>   []
>   (let [res (reductions (fn [accum x]
>                           (if (< accum 100)
>                             (+ accum x)
>                             (reduced [x accum])))
>                         (range))]
>     (prn res)
>     (prn (reduced? res)) ;=> false
>     (prn (reduced? (last res))))) ;=> false --- why isn't this `true`?
> ~~~
>
> Also, what's the use of `reduced?`, and on what in the above snippet would 
> I call it to return true?
>
 

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