Public Vars

Back

num (clj)

(source)

function

(num x)
Coerce to Number

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

(defn gen-num []
  (gen/uniform 0 2000))

(defspec reduced-always-returns
  (fn [probe to-end]
    (let [len (+ probe to-end 1)
          nums (range len)
          m (zipmap nums nums)]
      (reduced-at-probe m probe)))
  [^{:tag `gen-num} probe ^{:tag `gen-num} to-end]
  (assert (= :foo %)))

(deftest test-closed-over-clearing
  ;; this will throw OutOfMemory without proper reference clearing
  (is (number? (reduce + 0 (r/map identity (range 1e8))))))
clojure
(deftest division
  (is (= clojure.core// /))
  (binding [*ns* *ns*]
    (eval '(do (ns foo
                 (:require [clojure.core :as bar])
                 (:use [clojure.test]))
               (is (= clojure.core// bar//))))))

(deftest reader-conditionals
  (testing "basic read-cond"
    (is (= '[foo-form]
           (read-string {:read-cond :allow :features #{:foo}} "[#?(:foo foo-form :bar bar-form)]")))
    (is (= '[bar-form]
           (read-string {:read-cond :allow :features #{:bar}} "[#?(:foo foo-form :bar bar-form)]")))
    (is (= '[foo-form]
           (read-string {:read-cond :allow :features #{:foo :bar}} "[#?(:foo foo-form :bar bar-form)]")))
    (is (= '[]
           (read-string {:read-cond :allow :features #{:baz}} "[#?( :foo foo-form :bar bar-form)]"))))
  (testing "environmental features"
    (is (= "clojure" #?(:clj "clojure" :cljs "clojurescript" :default "default"))))
  (testing "default features"
    (is (= "default" #?(:clj-clr "clr" :cljs "cljs" :default "default"))))
  (testing "splicing"
    (is (= [] [#?@(:clj [])]))
    (is (= [:a] [#?@(:clj [:a])]))
    (is (= [:a :b] [#?@(:clj [:a :b])]))
    (is (= [:a :b :c] [#?@(:clj [:a :b :c])]))
    (is (= [:a :b :c] [#?@(:clj [:a :b :c])])))
  (testing "nested splicing"
    (is (= [:a :b :c :d :e]
           [#?@(:clj [:a #?@(:clj [:b #?@(:clj [:c]) :d]):e])]))
    (is (= '(+ 1 (+ 2 3))
           '(+ #?@(:clj [1 (+ #?@(:clj [2 3]))]))))
    (is (= '(+ (+ 2 3) 1)
           '(+ #?@(:clj [(+ #?@(:clj [2 3])) 1]))))
    (is (= [:a [:b [:c] :d] :e]
           [#?@(:clj [:a [#?@(:clj [:b #?@(:clj [[:c]]) :d])] :e])])))
  (testing "bypass unknown tagged literals"
    (is (= [1 2 3] #?(:cljs #js [1 2 3] :clj [1 2 3])))
    (is (= :clojure #?(:foo #some.nonexistent.Record {:x 1} :clj :clojure))))
  (testing "error cases"
    (is (thrown-with-msg? RuntimeException #"Feature should be a keyword" (read-string {:read-cond :allow} "#?((+ 1 2) :a)")))
    (is (thrown-with-msg? RuntimeException #"even number of forms" (read-string {:read-cond :allow} "#?(:cljs :a :clj)")))
    (is (thrown-with-msg? RuntimeException #"read-cond-splicing must implement" (read-string {:read-cond :allow} "#?@(:clj :a)")))
    (is (thrown-with-msg? RuntimeException #"is reserved" (read-string {:read-cond :allow} "#?@(:foo :a :else :b)")))
    (is (thrown-with-msg? RuntimeException #"must be a list" (read-string {:read-cond :allow} "#?[:foo :a :else :b]")))
    (is (thrown-with-msg? RuntimeException #"Conditional read not allowed" (read-string {:read-cond :BOGUS} "#?[:clj :a :default nil]")))
    (is (thrown-with-msg? RuntimeException #"Conditional read not allowed" (read-string "#?[:clj :a :default nil]")))
    (is (thrown-with-msg? RuntimeException #"Reader conditional splicing not allowed at the top level" (read-string {:read-cond :allow} "#?@(:clj [1 2])")))
    (is (thrown-with-msg? RuntimeException #"Reader conditional splicing not allowed at the top level" (read-string {:read-cond :allow} "#?@(:clj [1])")))
    (is (thrown-with-msg? RuntimeException #"Reader conditional splicing not allowed at the top level" (read-string {:read-cond :allow} "#?@(:clj []) 1"))))
  (testing "clj-1698-regression"
    (let [opts {:features #{:clj} :read-cond :allow}]
      (is (= 1 (read-string opts "#?(:cljs {'a 1 'b 2} :clj 1)")))
      (is (= 1 (read-string opts "#?(:cljs (let [{{b :b} :a {d :d} :c} {}]) :clj 1)")))
      (is (= '(def m {}) (read-string opts "(def m #?(:cljs ^{:a :b} {} :clj  ^{:a :b} {}))")))
      (is (= '(def m {}) (read-string opts "(def m #?(:cljs ^{:a :b} {} :clj ^{:a :b} {}))")))
      (is (= 1 (read-string opts "#?(:cljs {:a #_:b :c} :clj 1)")))))
  (testing "nil expressions"
    (is (nil? #?(:default nil)))
    (is (nil? #?(:foo :bar :clj nil)))
    (is (nil? #?(:clj nil :foo :bar)))
    (is (nil? #?(:foo :bar :default nil)))))

(deftest namespaced-map-errors
  (are [err msg form] (thrown-with-msg? err msg (read-string form))
                      Exception #"Invalid token" "#:::"
                      Exception #"Namespaced map literal must contain an even number of forms" "#:s{1}"
                      Exception #"Namespaced map must specify a valid namespace" "#:s/t{1 2}"
                      Exception #"Unknown auto-resolved namespace alias" "#::BOGUS{1 2}"
                      Exception #"Namespaced map must specify a namespace" "#: s{:a 1}"
                      Exception #"Duplicate key: :user/a" "#::{:a 1 :a 2}"
                      Exception #"Duplicate key: user/a" "#::{a 1 a 2}"))

(deftest t-Explicit-line-column-numbers
  (is (= {:line 42 :column 99}
         (-> "^{:line 42 :column 99} (1 2)" read-string meta (select-keys [:line :column]))))

  (eval (-> "^{:line 42 :column 99} (defn explicit-line-numbering [])" str->lnpr read))
  (is (= {:line 42 :column 99}
         (-> 'explicit-line-numbering resolve meta (select-keys [:line :column])))))
logseq/logseq
(ns frontend.pubsub
  "All mults and pubs are collected to this ns.
  vars with suffix '-mult' is a/Mult, use a/tap and a/untap on them. used by event subscribers
  vars with suffix '-pub' is a/Pub, use a/sub and a/unsub on them. used by event subscribers
  vars with suffix '-ch' is chan used by event publishers."
  {:clj-kondo/config {:linters {:unresolved-symbol {:level :off}}}}
  #?(:cljs (:require-macros [frontend.pubsub :refer [def-mult-or-pub chan-of]]))
  (:require [clojure.core.async :as a :refer [chan mult pub]]
            [clojure.core.async.impl.protocols :as ap]
            [malli.core :as m]
            [malli.dev.pretty :as mdp]
            [clojure.pprint :as pp]))

(def-mult-or-pub sync-events
  "file-sync events"
  [:map
   [:event [:enum
            :created-local-version-file
            :finished-local->remote
            :finished-remote->local
            :start
            :pause
            :resume
            :exception-decrypt-failed
            :remote->local-full-sync-failed
            :local->remote-full-sync-failed
            :get-remote-graph-failed
            :get-deletion-logs-failed
            :get-remote-all-files-failed]]
   [:data :map]]
  :topic-fn :event
  :ch-buffer (a/sliding-buffer 10))
clojure/core.async
;; The clojure.core.async namespace contains the public API.
(require '[clojure.core.async :as async :refer :all])

;; Pass a number to create a channel with a fixed buffer:
(chan 10)
nextjournal/clerk
(ns viewers.controls
  "Demo of Clerk's two-way bindings."
  {:nextjournal.clerk/visibility {:code :show :result :show}}
  (:require [clojure.core :as core]
            [nextjournal.clerk :as clerk]
            [nextjournal.clerk.viewer :as viewer]))

;; We `defonce` an atom and tag it with `^::clerk/sync`. This will create a corresponding (reagent) atom in the browser.
^{::clerk/sync true}
(defonce number-atom
  (atom 0))


;; This is showing the state that the JVM has.
@number-atom

#'number-atom


^{::clerk/viewer {:render-fn (list 'comp render-slider 'deref) :transform-fn transform-var}}
#'number-atom

@number-atom

^{::clerk/viewer {:transform-fn transform-var
                  :render-fn '#(vector nextjournal.clerk.render/inspect @%)}}
#'number-atom


@number-atom


^{::clerk/viewer convenient-slider ::clerk/sync true}
(defonce number-atom-2 (atom 99))

^{::clerk/viewer convenient-slider}
#'number-atom-2

@number-atom-2
originrose/cortex
(ns cortex.nn.impl
  "Implementation helpers to aid implementing neural network cortex protocols
or specific neural network layers"
  (:require [cortex.nn.layers :as layers]
            [clojure.core.matrix.macros :refer [c-for]]))


(defmacro convolution-outer-kernel
  [conv-desc & body]
  `(let [~'conv-desc ~conv-desc
         ~'output-width (long (:output-width ~'conv-desc))
         ~'output-height (long (:output-height ~'conv-desc))
         ~'num-in-channels (long (:input-channels ~'conv-desc))
         ~'num-out-channels (long (:output-channels ~'conv-desc))
         ~'input-width (long (:input-width ~'conv-desc))
         ~'input-height (long (:input-height ~'conv-desc))
         ~'input-planar-stride (* ~'input-width ~'input-height)
         ~'output-planar-stride (* ~'output-width ~'output-height)
         ~'kernel-width (long (:kernel-width ~'conv-desc))
         ~'kernel-height (long (:kernel-height ~'conv-desc))
         ~'output-channel-stride (* ~'kernel-width ~'kernel-height)
         ~'output-column-stride (* ~'output-channel-stride ~'num-in-channels)
         ~'stride-y (long (:stride-y ~'conv-desc))
         ~'stride-x (long (:stride-x ~'conv-desc))
         ~'pad-x (long (:pad-x ~'conv-desc))
         ~'pad-y (long (:pad-y ~'conv-desc))
         ~'min-x (- 0 ~'pad-x)
         ~'min-y (- 0 ~'pad-y)
         ~'max-x (+ ~'input-width ~'pad-x)
         ~'max-y (+ ~'input-height ~'pad-y)
         ~'kernel-num-elems (* ~'kernel-width ~'kernel-height)]
     (c-for
      [~'chan 0 (< ~'chan ~'num-in-channels) (inc ~'chan)]
      (let [~'chan-input-offset (* ~'chan ~'input-planar-stride)
            ~'chan-output-offset (* ~'chan ~'output-planar-stride)]
        (c-for
         [~'out-y 0 (< ~'out-y ~'output-height) (inc ~'out-y)]
         (let [~'input-rel-y (- (* ~'out-y ~'stride-y) ~'pad-y)]
           (c-for
            [~'out-x 0 (< ~'out-x ~'output-width) (inc ~'out-x)]
            (let [~'input-rel-x (- (* ~'out-x ~'stride-x) ~'pad-x)]
              ~@body))))))))


(defmacro convolution-roll-unroll-inner-kernel
  [& body]
  `(let [~'chan-conv-offset (* ~'chan ~'output-channel-stride)
         ~'output-offset (+ (* ~'out-y ~'output-width)
                            ~'out-x)
         ;;positive values for how far out we are into the padding
         input-over-x# (max 0 (- (+ ~'input-rel-x ~'kernel-width)
                                 ~'input-width))
         input-over-y# (max 0 (- (+ ~'input-rel-y ~'kernel-height)
                                 ~'input-height))
         ;;Negative values for how far before the 0 idx we are.
         input-under-x# (min ~'input-rel-x 0)
         input-under-y# (min ~'input-rel-y 0)
         ;;Width of the kernel excluding padding
         ~'exc-pad-width (max 0 (+ (- ~'kernel-width input-over-x#)
                                   input-under-x#))
         ~'exc-pad-height (max 0 (+ (- ~'kernel-height input-over-y#)
                                    input-under-y#))
         ~'exc-pad-kernel-num-elems (* ~'exc-pad-width ~'exc-pad-height)]
     (c-for
      [~'k-y 0 (< ~'k-y ~'kernel-height) (inc ~'k-y)]
      (c-for
       [~'k-x 0 (< ~'k-x ~'kernel-width) (inc ~'k-x)]
       (let [~'input-x (+ ~'input-rel-x ~'k-x)
             ~'input-y (+ ~'input-rel-y ~'k-y)
             ~'output-conv-addr (+ (* ~'output-offset
                                      ~'output-column-stride)
                                   ~'chan-conv-offset
                                   (* ~'k-y ~'kernel-width)
                                   ~'k-x)
             ~'input-addr  (+ (* ~'input-y ~'input-width)
                              ~'input-x
                              ~'chan-input-offset)
             ~'input-valid? (and (in-bounds? ~'input-x 0 ~'input-width)
                                 (in-bounds? ~'input-y 0 ~'input-height))
             loop-valid?# (and (in-bounds? ~'input-x ~'min-x ~'max-x)
                               (in-bounds? ~'input-y ~'min-y ~'max-y))]
         (when loop-valid?#
           ~@body))))))
hraberg/deuce
(ns deuce.emacs.font
  (:use [deuce.emacs-lisp :only (defun defvar)])
  (:require [clojure.core :as c])
  (:refer-clojure :exclude []))

(defvar font-width-table nil
  "Alist of font width symbols vs the corresponding numeric values.
  See `font-weight-table' for the format of the vector.")

(defvar font-slant-table nil
  "Vector of font slant symbols vs the corresponding numeric values.
  See `font-weight-table' for the format of the vector.")

(defun list-fonts (font-spec &optional frame num prefer)
  "List available fonts matching FONT-SPEC on the current frame.
  Optional 2nd argument FRAME specifies the target frame.
  Optional 3rd argument NUM, if non-nil, limits the number of returned fonts.
  Optional 4th argument PREFER, if non-nil, is a font-spec to
  control the order of the returned list.  Fonts are sorted by
  how close they are to PREFER."
  )

  Each element is a vector containing information of a glyph in this format:
    [FROM-IDX TO-IDX C CODE WIDTH LBEARING RBEARING ASCENT DESCENT ADJUSTMENT]
  where
    FROM is an index numbers of a character the glyph corresponds to.
    TO is the same as FROM.
    C is the character of the glyph.
    CODE is the glyph-code of C in FONT-OBJECT.
    WIDTH thru DESCENT are the metrics (in pixels) of the glyph.
    ADJUSTMENT is always nil.
  If FONT-OBJECT doesn't have a glyph for a character,
  the corresponding element is nil."
  )

  VALUE must be a non-negative integer or a floating point number
  specifying the font size.  It specifies the font size in pixels (if
  VALUE is an integer), or in points (if VALUE is a float).