Public verbs for combining and converting quantities. All are plain vars (autocomplete-friendly, no clojure.core shadowing) and interoperate with plain numbers as dimensionless scalars.
(require '[commensura.units :as u] '[commensura.core :refer [by per plus minus pow to ratio]])
(to (by (u/feet 10) (u/feet 12) (u/feet 8)) u/gallons) ;=> 552960/77 gallon ≈ 7181.30 [volume] ; str/println form; prn wraps it in a ; #commensura/quantity tagged literal
Comparison verbs — the everyday lt?/le?/gt?/ge?/eq?/ne? (a physical,
dimension-checked order that throws on overlapping intervals) plus the interval-aware
certainly-…?/possibly-…? operators — order quantities by base magnitude, so
(eq? (u/inch 12) (u/foot 1)) is true.
The verbs also accept Intervals (see commensura.interval); an interval accessor treats a bare quantity/number as a degenerate interval, so no scalar promotion is needed.
Public verbs for combining and converting quantities. All are plain vars
(autocomplete-friendly, no clojure.core shadowing) and interoperate with plain
numbers as dimensionless scalars.
(require '[commensura.units :as u]
'[commensura.core :refer [by per plus minus pow to ratio]])
(to (by (u/feet 10) (u/feet 12) (u/feet 8)) u/gallons)
;=> 552960/77 gallon ≈ 7181.30 [volume] ; str/println form; prn wraps it in a
; #commensura/quantity tagged literal
Comparison verbs — the everyday `lt?`/`le?`/`gt?`/`ge?`/`eq?`/`ne?` (a physical,
dimension-checked order that throws on overlapping intervals) plus the interval-aware
`certainly-…?`/`possibly-…?` operators — order quantities by base magnitude, so
`(eq? (u/inch 12) (u/foot 1))` is true.
The verbs also accept Intervals (see commensura.interval); an interval accessor treats a
bare quantity/number as a degenerate interval, so no scalar promotion is needed.(by x)(by x y)(by x y & more)Product of quantities/numbers/intervals (dimensions add). Variadic.
Product of quantities/numbers/intervals (dimensions add). Variadic.
(certainly-eq? x y)x and y are the same single point (both collapse to one shared value).
x and y are the same single point (both collapse to one shared value).
(certainly-ge? x y)x's low bound >= y's high bound: every value of x >= every value of y.
x's low bound >= y's high bound: every value of x >= every value of y.
(certainly-gt? x y)x's low bound is above y's high bound: every value of x > every value of y.
x's low bound is above y's high bound: every value of x > every value of y.
(certainly-le? x y)x's high bound <= y's low bound: every value of x <= every value of y.
x's high bound <= y's low bound: every value of x <= every value of y.
(certainly-lt? x y)x's high bound is below y's low bound: every value of x < every value of y.
x's high bound is below y's low bound: every value of x < every value of y.
(certainly-ne? x y)x and y are disjoint: their ranges share no value.
x and y are disjoint: their ranges share no value.
(defunit sym expr)(defunit sym a b)(defunit sym doc mag dims)Define a callable Unit var. The bound value is the new unit name: it prints
under its own name, scales when called, and serves as a to/ratio target.
Four caller-facing forms — the 3-argument shape dispatches on whether its middle form is a number (⇒ literal) or not (⇒ docstring):
(defunit sym expr) — the everyday form: expr is any quantity/number
expression; its magnitude and dimensions become the unit's.(defunit sym doc expr) — as above, with a leading docstring on the var.(defunit sym magnitude dims) — a literal base-SI magnitude and dimension map,
with no evaluation; the form the generated builtins emit.(defunit sym doc magnitude dims) — the literal form with a leading docstring
(a generated builtin that carries a comment).Examples:
(defunit beer (by (u/floz 12) (u/percent 3.2) (per u/water u/alcohol))) (beer 5) ;=> 5 beer (to (by u/magnum (u/percent 13.5)) beer) ;=> …how many beers in a magnum (ratio some-volume beer) ;=> …bare count of beers
(defunit smoot "Oliver Smoot's height (an MIT prank unit)" (u/inch 67))
(defunit foot 381/1250 {:length 1}) ; literal form (defunit gee "standard gravity" 196133/20000 {:length 1 :time -2})
Define a callable `Unit` var. The bound value is the new unit name: it prints
under its own name, scales when called, and serves as a `to`/`ratio` target.
Four caller-facing forms — the 3-argument shape dispatches on whether its middle
form is a number (⇒ literal) or not (⇒ docstring):
- `(defunit sym expr)` — the everyday form: `expr` is any quantity/number
expression; its magnitude and dimensions become the unit's.
- `(defunit sym doc expr)` — as above, with a leading docstring on the var.
- `(defunit sym magnitude dims)` — a literal base-SI magnitude and dimension map,
with no evaluation; the form the generated builtins emit.
- `(defunit sym doc magnitude dims)` — the literal form with a leading docstring
(a generated builtin that carries a comment).
Examples:
(defunit beer (by (u/floz 12) (u/percent 3.2) (per u/water u/alcohol)))
(beer 5) ;=> 5 beer
(to (by u/magnum (u/percent 13.5)) beer) ;=> …how many beers in a magnum
(ratio some-volume beer) ;=> …bare count of beers
(defunit smoot "Oliver Smoot's height (an MIT prank unit)" (u/inch 67))
(defunit foot 381/1250 {:length 1}) ; literal form
(defunit gee "standard gravity" 196133/20000 {:length 1 :time -2})(eq? x y)Physical equality; throws on overlap.
Physical equality; throws on overlap.
(ge? x y)Greater-or-equal; throws on overlap.
Greater-or-equal; throws on overlap.
(gt? x y)Greater than; throws on overlap.
Greater than; throws on overlap.
(le? x y)Less-or-equal; throws on overlap.
Less-or-equal; throws on overlap.
(lt? x y)Less than; throws on overlapping intervals.
Less than; throws on overlapping intervals.
(minus x)(minus x y)(minus x y & more)Difference of same-dimension quantities/intervals; unary form negates.
Difference of same-dimension quantities/intervals; unary form negates.
(money->quantity money)Convert an org.joda.money.Money into an exact commensura currency quantity. The Money's ISO code
resolves to a live currency unit (through the currency resolver — require commensura.currency first)
scaled by the Money's decimal amount, which re-enters the exact tower as a rational. Inverse of
quantity->money (Money already sits at the currency's scale, so nothing is lost coming back).
(money->quantity (quantity->money (cur/USD 19.99))) ;=> 1999/100 USD ; == (cur/USD 19.99)
Convert an `org.joda.money.Money` into an exact commensura currency quantity. The Money's ISO code resolves to a live currency unit (through the currency resolver — `require commensura.currency` first) scaled by the Money's decimal amount, which re-enters the exact tower as a rational. Inverse of `quantity->money` (Money already sits at the currency's scale, so nothing is lost coming back). (money->quantity (quantity->money (cur/USD 19.99))) ;=> 1999/100 USD ; == (cur/USD 19.99)
(ne? x y)Physical inequality; throws on overlap.
Physical inequality; throws on overlap.
(per x)(per x y)(per x y & more)Quotient, left-associative: (per a b c) = a/b/c (dimensions subtract).
Quotient, left-associative: (per a b c) = a/b/c (dimensions subtract).
(plus x)(plus x y)(plus x y & more)Sum of same-dimension quantities/intervals. Variadic.
Sum of same-dimension quantities/intervals. Variadic.
(possibly-eq? x y)x and y overlap: their ranges share a value.
x and y overlap: their ranges share a value.
(possibly-ge? x y)Some value of x >= some value of y.
Some value of x >= some value of y.
(possibly-gt? x y)Some value of x > some value of y.
Some value of x > some value of y.
(possibly-le? x y)Some value of x <= some value of y.
Some value of x <= some value of y.
(possibly-lt? x y)Some value of x < some value of y.
Some value of x < some value of y.
(possibly-ne? x y)x and y are not a single shared point.
x and y are not a single shared point.
(pow x n)Raise a quantity/interval to an integer power.
Raise a quantity/interval to an integer power.
(quantity->money qty)(quantity->money qty mode)Convert a currency quantity to an org.joda.money.Money (via clojurewerkz/money). qty must carry a
currency dimension of exponent 1; its ISO code is the currency term's name (dollar → USD). The
displayed amount is rounded to the currency's decimal places — HALF_EVEN by default, or pass a
java.math.RoundingMode.
This deliberately leaves commensura's exact tower: Money is a fixed-scale decimal, so an amount
finer than the currency's minor unit is rounded. Throws on non-currency dimensions or a non-ISO code.
Inverse of money->quantity (up to that scale-rounding).
(quantity->money (cur/USD 19.99)) ;=> USD 19.99 (quantity->money (cur/USD 19.995)) ;=> USD 20.00 (HALF_EVEN) (quantity->money (cur/USD 19.995) RoundingMode/FLOOR) ;=> USD 19.99
Convert a currency quantity to an `org.joda.money.Money` (via clojurewerkz/money). `qty` must carry a currency dimension of exponent 1; its ISO code is the currency term's name (`dollar` → `USD`). The displayed amount is rounded to the currency's decimal places — `HALF_EVEN` by default, or pass a `java.math.RoundingMode`. This deliberately **leaves commensura's exact tower**: Money is a fixed-scale decimal, so an amount finer than the currency's minor unit is rounded. Throws on non-currency dimensions or a non-ISO code. Inverse of `money->quantity` (up to that scale-rounding). (quantity->money (cur/USD 19.99)) ;=> USD 19.99 (quantity->money (cur/USD 19.995)) ;=> USD 20.00 (HALF_EVEN) (quantity->money (cur/USD 19.995) RoundingMode/FLOOR) ;=> USD 19.99
(ratio x target)Dimensionless count: how many of target fit in x (quantity or interval).
Dimensionless count: how many of target fit in x (quantity or interval).
(register-dimension! dims nm)Give a human name to a dimension map, so quantities of that dimension print it
in the trailing [..] slot (overriding any builtin). Returns the name.
(register-dimension! {:length 4} "quaternary space") (pow u/meter 4) ;=> 1 meter^4 ≈ 1.0 [quaternary space]
Give a human name to a dimension map, so quantities of that dimension print it
in the trailing `[..]` slot (overriding any builtin). Returns the name.
(register-dimension! {:length 4} "quaternary space")
(pow u/meter 4) ;=> 1 meter^4 ≈ 1.0 [quaternary space](register-unit-resolver! pred dispatch)Install a resolver for a family of units derivable from their name rather than registered one by
one (the extension seam behind commensura's own historical currencies). pred is name -> boolean;
dispatch is name -> unit, invoked when a name isn't in the unit table and pred matches. So a
#commensura/unit "…" literal (and to/ratio) reify the whole family on demand. Resolvers are
tried in registration order, first match wins. Complements defunit (a single fixed unit).
Install a resolver for a *family* of units derivable from their name rather than registered one by one (the extension seam behind commensura's own historical currencies). `pred` is `name -> boolean`; `dispatch` is `name -> unit`, invoked when a name isn't in the unit table and `pred` matches. So a `#commensura/unit "…"` literal (and `to`/`ratio`) reify the whole family on demand. Resolvers are tried in registration order, first match wins. Complements `defunit` (a single fixed unit).
(to x target)Re-express a quantity/interval in a target unit (dimension-preserving). Uses only the target's unit
basis: a scaled target's coefficient is ignored (and warns) — (to (u/mile 5) (u/foot 3)) gives
feet, not 3-foot units. For "how many of a given quantity fit", use ratio. See q/to.
Re-express a quantity/interval in a target unit (dimension-preserving). Uses only the target's unit basis: a *scaled* target's coefficient is ignored (and warns) — `(to (u/mile 5) (u/foot 3))` gives feet, not 3-foot units. For "how many of a given quantity fit", use `ratio`. See `q/to`.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |