Back
cast (clj)
(source)function
(cast c x)
Throws a ClassCastException if x is not a c, else returns x.
Examples
originrose/cortex
(ns cortex.util-test
(:refer-clojure :exclude [defonce])
(:require
#?(:cljs [cljs.test :refer-macros [deftest is testing]]
:clj [clojure.test :refer [deftest is testing]])
[clojure.core.matrix :as m]
[clojure.string :as str]
[cortex.util :refer :all]))
(deftest sformat-test
(testing "basic formatting"
(is (= (sformat "Hello, %s!" "world")
"Hello, world!")))
(testing "numeric formatting and %% specifier"
(is (= (sformat "Sales tax of %d%% on $%.2f item" 37 1.25)
"Sales tax of 37% on $1.25 item")))
(testing "autocasting"
(is (= (sformat "Long as double %.2f and double as long %d" 3141 2.718)
"Long as double 3141.00 and double as long 2")))
(testing "positional arguments"
(is (= (sformat "%s %% %1$s %s %3$s %3$s %% %s %2$s %s" 1 2 3 4)
"1 % 1 2 3 3 % 3 2 4")))
(testing "map format specifier over collections"
(is (= (sformat "%1$.2f %1$.4f" [Math/PI Math/E 5])
"[3.14 2.72 5.00] [3.1416 2.7183 5.0000]")))
(testing "formatting core.matrix arrays as nested collections"
(is (= (sformat "%.1f" (m/identity-matrix :vectorz 2))
"[[1.0 0.0] [0.0 1.0]]"))))
mikera/core.matrix
WARNING: because they lack efficient indexed access, sequences will perform badly for most
array operations. In general they should be converted to other implementations before use."
(:require [clojure.core.matrix.protocols :as mp]
[clojure.core.matrix.implementations :as imp]
#?(:clj [clojure.core.matrix.macros :refer [scalar-coerce error]]))
#?(:clj (:import [clojure.lang ISeq])
:cljs (:require-macros [clojure.core.matrix.macros :refer [scalar-coerce error]])))
(extend-protocol mp/PBroadcast
ISeq
(broadcast [m new-shape]
(mp/broadcast (mp/convert-to-nested-vectors m) new-shape)))
(extend-protocol mp/PBroadcastLike
ISeq
(broadcast-like [m a]
(mp/broadcast (mp/convert-to-nested-vectors a) (mp/get-shape m))))
(extend-protocol mp/PFunctionalOperations
ISeq
(element-seq [m]
(if (== 0 (long (mp/dimensionality (first m))))
m ;; handle 1D case, just return this sequence unchanged
(mapcat mp/element-seq m)))
(element-map
([m f]
(mapv #(mp/element-map % f) m))
([m f a]
(let [[m a] (mp/broadcast-compatible m a)]
(mapv #(mp/element-map % f %2) m (mp/get-major-slice-seq a))))
([m f a more]
(let [[m a & more] (apply mp/broadcast-compatible m a more)] ; FIXME
(mapv #(mp/element-map % f %2 %3) m (mp/get-major-slice-seq a) (map mp/get-major-slice-seq more)))))
(element-map!
([m f]
(error "Sequence arrays are not mutable!"))
([m f a]
(error "Sequence arrays are not mutable!"))
([m f a more]
(error "Sequence arrays are not mutable!")))
(element-reduce
([m f]
(reduce f (mapcat mp/element-seq m)))
([m f init]
(reduce f init (mapcat mp/element-seq m)))))
(extend-protocol mp/PMapIndexed
ISeq
(element-map-indexed
([ms f]
(mapv (fn [i m] (mp/element-map-indexed m #(apply f (cons i %1) %&)))
(range (count ms)) ms))
([ms f as]
(let [[ms as] (mp/broadcast-compatible ms as)]
(mapv (fn [i m a]
(mp/element-map-indexed m #(apply f (cons i %1) %&) a))
(range (count ms)) ms (mp/get-major-slice-seq as))))
([ms f as more]
(let [[ms as & more] (apply mp/broadcast-compatible ms as more)] ; FIXME
(mapv (fn [i m a & mr]
(mp/element-map-indexed m #(apply f (cons i %1) %&) a mr))
(range (count ms)) ms
(mp/get-major-slice-seq as)
(map mp/get-major-slice-seq more)))))
(element-map-indexed!
([m f]
(error "Sequence arrays are not mutable!"))
([m f a]
(error "Sequence arrays are not mutable!"))
([m f a more]
(error "Sequence arrays are not mutable!"))))
typedclojure/typedclojure
In general these operations:
- are macros instead of functions
- take Typed Clojure types instead of malli schemas
- helps the type checker infer their results
See also:
- typed.malli.generator
- typed.malli.swagger
- typed.malli.json-schema"
(:require [typed.clojure :as t]
[clojure.core.typed.unsafe :as unsafe]
[malli.core :as m]))
(defmacro validate
"Validate v as type t. Type checker infers as a predicate
on t.
eg.,
(validate t/AnyInteger 1) => true
(validate t/AnyInteger nil) => false"
[t v]
`((unsafe/ignore-with-unchecked-cast
m/validate
[t/Any t/Any :-> t/Bool :filters {:then (~'is ~t 1)
:else (~'! ~t 1)}])
~((requiring-resolve 'typed.malli.parse-type/type-syntax->malli-syntax) t)
~v))
(defmacro validator
"Create validator for type t. Type checker infers as a predicate
on t.
eg.,
((validator t/AnyInteger) 1) => true
((validator t/AnyInteger) nil) => false"
[t]
`((unsafe/ignore-with-unchecked-cast
m/validator
[t/Any :-> [t/Any :-> t/Bool :filters {:then (~'is ~t 0)
:else (~'! ~t 0)}]])
~((requiring-resolve 'typed.malli.parse-type/type-syntax->malli-syntax) t)))
(defmacro explain
"Explain validation failure of v conforming to type t. Type checker infers as a predicate
on \"not t\".
(explain t/AnyInteger 1) => nil
(explain t/AnyInteger nil)
=>
{:schema :int,
:value nil,
:errors
({:path [],
:in [],
:schema :int,
:value nil,
:type nil,
:message nil})}"
[t v]
`((unsafe/ignore-with-unchecked-cast
m/explain
[t/Any t/Any :-> (t/Nilable '{:schema t/Any
:value t/Any
:errors (t/Seqable t/Any)})
:filters {:then (~'! ~t 1)
:else (~'is ~t 1)}])
~((requiring-resolve 'typed.malli.parse-type/type-syntax->malli-syntax) t)
~v))
(defmacro parse
"Parse value v as type t. Type checker infers the shape of
return value.
eg.,
(parse t/AnyInteger 1) ;=> 1
(parse t/AnyInteger nil) ;=> :malli.core/invalid
(parse (t/U ^{::name :int} t/AnyInteger
^{::name :bool} t/Bool)
1)
;=> [:int 1]
(parse (t/U ^{::name :int} t/AnyInteger
^{::name :bool} t/Bool)
true)
;=> [:bool true]"
[t v]
`((unsafe/ignore-with-unchecked-cast
m/parse
[t/Any t/Any :-> (t/U '::m/invalid ~(-> t
((requiring-resolve 'typed.malli.parse-type/type-syntax->malli-syntax))
((requiring-resolve 'typed.malli.schema-to-type/malli-syntax->parser-type))))])
~((requiring-resolve 'typed.malli.parse-type/type-syntax->malli-syntax) t)
~v))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
"Type annotations for the base Clojure distribution."
#?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
(:require [clojure.core :as cc]
[typed.clojure :as t]
#?(:clj [typed.ann-macros.clojure :as macros])
#?(:clj typed.ann.clojure.jvm) ;; jvm annotations
#?(:clj clojure.core.typed))
#?(:clj
(:import (clojure.lang PersistentHashSet PersistentList
APersistentMap #_IPersistentCollection
#_ITransientSet
IRef)
(java.util Comparator Collection))))
#?@(:cljs [] :default [
cc/cast (t/All [x] [Class x :-> x])
])
;cast to java array
;; TODO rethink input and output types. eg.,
;; cc/booleans [(ReadyOnlyArray boolean) :-> (t/U nil (Array boolean))]
;; TODO objects??
;; cc/objects [(ReadyOnlyArray Object) :-> (t/U nil (ReadyOnlyArray Object))]
;;
;; TODO propagate to Numbers/booleans etc
;cc/booleans [t/Any :-> (t/U nil (Array boolean))]
;cc/bytes [t/Any :-> (t/U nil (Array byte))]
;cc/chars [t/Any :-> (t/U nil (Array char))]
;cc/shorts [t/Any :-> (t/U nil (Array short))]
;cc/ints [t/Any :-> (t/U nil (Array int))]
;cc/longs [t/Any :-> (t/U nil (Array long))]
;cc/floats [t/Any :-> (t/U nil (Array float))]
;cc/doubles [t/Any :-> (t/U nil (Array double))]
zakwilson/ceilingbounce
(ns com.flashlightdb.ceilingbounce.main
(:require [neko.activity :refer [defactivity set-content-view!]]
[neko.notify :as notify]
[neko.resource :as res]
[neko.find-view :refer [find-view]]
[neko.threading :refer [on-ui]]
[neko.action-bar :refer [setup-action-bar tab-listener]]
[neko.log :as log]
[neko.ui :as ui]
neko.tools.repl
[clojure.java.io :as io]
[clojure.core.async
:as a
:refer [>! <! >!! <!! go chan buffer close! thread
alts! alts!! timeout]]
[com.flashlightdb.ceilingbounce.runtime :as runtime]
[com.flashlightdb.ceilingbounce.lumens :as lumens]
[com.flashlightdb.ceilingbounce.throw :as throw]
[com.flashlightdb.ceilingbounce.common
:as common
:refer [identity* config main-activity do-nothing]])
(:import android.widget.EditText
[android.hardware
SensorManager
SensorEventListener
Sensor
SensorEvent
SensorListener]
[android.app
Activity]
neko.App))
(defactivity com.flashlightdb.ceilingbounce.MainActivity
:key :main
(onCreate [this bundle]
(swap! main-activity
identity* this)
(common/read-config)
(def sm (cast SensorManager (.getSystemService ^Activity this "sensor")))
(def light-sensor (.getDefaultSensor ^SensorManager sm
(Sensor/TYPE_LIGHT)))
(def sensor-listener
(let [activity this]
(reify SensorEventListener
(onSensorChanged [this evt]
(let [lux (first (.values evt))]
(swap! common/lux identity* lux)))
(onAccuracyChanged [this s a]
(do-nothing)))))
(.superOnCreate this bundle)
(neko.debug/keep-screen-on this)
(runtime/setup-chart)
(on-ui
(try ; FIXME why does this error?
(setup-action-bar this
{:navigation-mode :tabs
:id ::action-bar
:tabs [[:tab {:text "Lumens"
:tab-listener (tab-listener
:on-tab-selected @#'lumens/activate-tab
:on-tab-unselected @#'lumens/deactivate-tab)}]
[:tab {:text "Throw"
:tab-listener (tab-listener
:on-tab-selected @#'throw/activate-tab
:on-tab-unselected @#'throw/deactivate-tab)}]
[:tab {:text "Runtime"
:tab-listener (tab-listener
:on-tab-selected @#'runtime/activate-tab
:on-tab-unselected @#'runtime/deactivate-tab)}]]})
(catch Exception e nil))))