Public Vars

Back

with-bindings (clj)

(source)

macro

(with-bindings binding-map & body)
Takes a map of Var/value pairs. Installs for the given Vars the associated values as thread-local bindings. Then executes body. Pops the installed bindings after body was evaluated. Returns the value of body.

Examples

frenchy64/fully-satisfies
(ns io.github.frenchy64.fully-satisfies.non-leaky-macros.clojure.core
  "Implementations of clojure.core macros that don't leak implementation details."
  (:refer-clojure :exclude [locking binding with-bindings sync with-local-vars
                            with-in-str dosync with-precision with-loading-context
                            with-redefs delay vswap! lazy-seq lazy-cat future
                            pvalues])
  (:require [clojure.core :as cc]))

(defmacro non-leaky-with-bindings
  "Like clojure.core/with-bindings, except body cannot leak :pre/post syntax or use (recur)."
  [binding-map & body]
  `(cc/with-bindings ~binding-map
     (let [res# (do ~@body)]
       res#)))

(defmacro with-bindings
  [& args]
  `(non-leaky-with-bindings ~@args))
originrose/think.image
(ns think.image.patch-test
  (:require [clojure.test :refer :all]
            [mikera.image.core :as i]
            [mikera.image.colours :as colour]
            [think.image.patch :as p]
            [think.image.util :refer [hue-wheel-image square-image circular-image rand-image]]
            [think.image.core]
            [think.image.image :as image]
            [think.image.pixel :as pixel]
            [think.image.image-util :as iu]
            [clojure.core.matrix.macros :refer [c-for]]
            [clojure.core.matrix :as m]
            [clojure.core.matrix.stats :as m-stats])
  (:import [java.awt.image BufferedImage]
           [java.awt Rectangle]))

(deftest test-patches-within-content
  (testing "Exercise the patch generation code to ensure that patches exist within the content rect."
    (with-bindings {#'p/*content-threshold-cutoff* 1.0}
     (let [test-img (circular-image)
           patch-count 5
           patch-dim 32
           ;;Create an image mask that is exactly the red area of the circular image
           patches (p/masked-image->patches test-img (image/image->mask-image test-img)
                                            patch-count patch-dim (iu/image->rect test-img)
                                            :double)
           images (mapv #(p/patch->image % patch-dim) patches)]
       ;; TODO -- ensure that generated patches match the expected patch
       (doseq [img images]
         ;;Default serialization type of an image is
         (let [^ints int-data (image/->array img)
               num-pixels (alength int-data)
               r-sum (reduce (fn [^double r-sum int-pix]
                               (pixel/with-unpacked-pixel int-pix
                                 (+ r-sum r)))
                             0.0
                             (seq int-data))]
           (is (= 255 (long (Math/round (double (/ r-sum num-pixels))))))))))))