Public Vars

Back

reduced? (clj)

(source)

function

(reduced? x)
Returns true if x is the result of a call to reduced

Examples

typedclojure/typedclojure
(ns typed-test.spec.clojure.core
  (:require [typed.spec.clojure.core :as api]
            [clojure.alpha.spec :as s]
            [clojure.alpha.spec.gen :as gen]
            [clojure.alpha.spec.test :as stest]
            [typed.clj.spec :as t]
            [typed.clj.spec.test-utils :as tu]
            [clojure.test :refer :all]))

(deftest reduced-spec-test
  (doseq [v (gen/sample
              (s/gen (api/reduced-spec integer?)))]
    (assert (reduced? v) v)
    (is (integer? @v) v))
  (tu/is-valid (api/reduced-spec integer?) (reduced 1))
  (tu/is-invalid (api/reduced-spec symbol?) (reduced 1))
  (tu/is-valid (s/or :reduced (api/reduced-spec integer?)
                     :integer integer?)
               (reduced 1))
  (tu/is-invalid (s/or :reduced (api/reduced-spec integer?)
                       :integer integer?)
                 (gensym))
  (tu/is-invalid (api/reduced-spec integer?)
                 (gensym))
  (is (apply
        (every-pred
          reduced?
          (comp integer? deref))
        (gen/sample
          (s/gen (api/reduced-spec integer?)))))
  )
athos/Drains
(ns drains.impl.unsafe
  (:require [clojure.core :as cc]
            [drains.protocols :as p]
            #?(:clj [drains.impl.macros :refer [def-unsafe-drains-n def-attachable-drain]]
               :cljs [drains.impl.macros :refer-macros [def-unsafe-drains-n def-attachable-drain]])
            [drains.impl.reduced :as reduced]
            [drains.impl.utils :as utils]))

(deftype UnsafeDrain [rf ^:unsynchronized-mutable val]
  p/IDrain
  (-reduced? [this] false)
  (-flush [this input]
    (let [val' (rf val input)]
      (if (cc/reduced? val')
        (reduced/->ReducedDrain (rf @val'))
        (do (set! val val')
            this))))
  (-residual [this] (rf val)))

;; defines
;;  - UnsafeDrains
;;  - UnsafeDrainsAttachable
(def-attachable-drain UnsafeDrains [^:unsynchronized-mutable ds
                                    ^:unsynchronized-mutable reduced?
                                    ^:unsynchronized-mutable active-keys]
  {:-reduced? ([this] reduced?)
   :-residual ([this]
               (reduce-kv (fn [ds k drain] (assoc ds k (p/-residual drain))) ds ds))
   :-flush ([this input]
            (reduce-kv (fn [_ k drain]
                         (let [drain' (p/-flush drain input)]
                           (when-not (identical? drain drain')
                             (update-drain! this k drain')
                             (when (p/-reduced? drain')
                               (disj-active-key! this k)
                               (when-not (seq (.-active-keys this))
                                 (update-reduced! this))))))
                       nil
                       ds))}
  DrainUpdater
  (update-drain! [this k drain]
    (set! ds (assoc ds k drain)))
  ActiveKeyUpdater
  (disj-active-key! [this k]
    (set! active-keys (disj active-keys k)))
  ReducedUpdater
  (update-reduced! [this]
    (set! reduced? true)))

(deftype UnsafeFmap [f
                     ^:unsynchronized-mutable d
                     ^:unsynchronized-mutable reduced?]
  p/IDrain
  (-reduced? [this] reduced?)
  (-flush [this input]
    (let [d' (p/-flush d input)]
      (when-not (identical? d d')
        (set! d d')
        (when (p/-reduced? d')
          (set! reduced? true))))
    this)
  (-residual [this]
    (f (p/-residual d))))

;; defines
;;  - UnsafeGroupBy
;;  - UnsafeGroupByAttachable
(def-attachable-drain UnsafeGroupBy [key-fn d ^:unsynchronized-mutable ds]
  {:-reduced? ([this] false)
   :-residual ([this] (reduce-kv #(assoc %1 %2 (p/-residual %3)) ds ds))
   :-flush ([this input]
            (let [key (key-fn input)
                  d (or (get ds key)
                        (let [d (utils/->unsafe (utils/unwrap d))]
                          (set! ds (assoc ds key d))
                          d))
                  d' (p/-flush d input)]
              (when-not (identical? d d')
                (set! ds (assoc ds key d')))))})