{"id":22155798,"url":"https://github.com/inqwell/typeops","last_synced_at":"2025-08-26T09:15:05.357Z","repository":{"id":57714083,"uuid":"89473065","full_name":"inqwell/typeops","owner":"inqwell","description":"Alternative value type combination in Clojure arithmetic","archived":false,"fork":false,"pushed_at":"2019-03-30T20:39:12.000Z","size":27,"stargazers_count":12,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-02T23:41:32.615Z","etag":null,"topics":["arithmetic","clojure","type-safe"],"latest_commit_sha":null,"homepage":null,"language":"Clojure","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"epl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/inqwell.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-26T11:25:58.000Z","updated_at":"2024-05-21T09:14:09.000Z","dependencies_parsed_at":"2022-09-26T21:31:12.056Z","dependency_job_id":null,"html_url":"https://github.com/inqwell/typeops","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/inqwell/typeops","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inqwell%2Ftypeops","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inqwell%2Ftypeops/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inqwell%2Ftypeops/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inqwell%2Ftypeops/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/inqwell","download_url":"https://codeload.github.com/inqwell/typeops/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inqwell%2Ftypeops/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267130204,"owners_count":24040412,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-26T02:00:08.937Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arithmetic","clojure","type-safe"],"created_at":"2024-12-02T02:20:04.726Z","updated_at":"2025-07-26T06:33:22.280Z","avatar_url":"https://github.com/inqwell.png","language":"Clojure","readme":"# typeops\n\nAlternative type outcomes for arithmetic in Clojure.\n\n[API](https://inqwell.github.io/typeops/index.html)\n\n[![Clojars Project](http://clojars.org/typeops/latest-version.svg)](http://clojars.org/typeops)\n\n`[typeops \"0.1.2\"]`\n\n* In Clojure, functions are agnostic about argument types, yet the host platform is not and likely\nneither is your database.\n\n* If you are writing a calculation engine then in many cases floating point\ntypes are unsuitable - inaccuracies will accumulate making results unpredictable.\n\n* Value type systems and how the types combine are a policy decision. While\nClojure supports integer and floating point types, and mostly makes precise\ndecimal usage transparent, the way these combine as operands in the\narithmetic functions may not be to your liking.\n\nOK:\n```clojure\n(/ 123.456M 3)\n=\u003e 41.152M\n```\n\nNot OK:\n```clojure\n(/ 123.457M 3)\nArithmeticException Non-terminating decimal expansion;\nno exact representable decimal result.\njava.math.BigDecimal.divide (BigDecimal.java:1690)\n```\n\nWe can get round this using `with-precision` :\n```clojure\n(with-precision 5\n  (/ 123.457M 3))\n=\u003e 41.152M\n```\nbut this taints the code, forcing us to be aware of the underlying types,\nand what precision do we choose if our interest is accuracy?\n\nIf you are using `decimal` it doesn't make sense to allow these to combine\nwith floating point:\n```clojure\n(* 123.457M 3.142)\n=\u003e 387.90189399999997\n```\nIf you want to avoid `ratio` preferring integer arithmetic, again, you have to be\nexplicit:\n```clojure\n(/ 4 3)\n=\u003e 4/3\n\n(quot 4 3)\n=\u003e 1\n```\nTypeops does the following for `+` `-` `*` and `/` :\n\n* Integer arithmetic gives a (truncated) integer result\n\n* Intermediate results do not lose accuracy\n\n* decimals cannot combine with floating point\n\n## \"assign\"\nIf you are modelling domain types it is useful to \"assign\" fields according to\ntheir underlying type and accuracy, rather than say relying on your database\nto do this for you:\n```clojure\n(def m\n{:Integer  0,\n :Decimal2 0.00M,\n :Short    0,\n :Decimal  0E-15M,\n :Float    0.0,\n :Long     1,\n :Byte     0,\n :Double   0.0,\n :String   \"\"})\n\n(assoc m :Decimal2 2.7182818M)\n=\u003e {:Integer 0,\n    :Decimal2 2.72M,\n    :Short 0,\n    :Decimal 0E-15M,\n    :Float 0.0,\n    :Long 1,\n    :Byte 0,\n    :Double 0.0,\n    :String \"\"}\n```\n### nil\nIf your domain model permits `NULL` values you can represent these as `nil` in\nClojure. This destroys the type information however if a map has meta data:\n```clojure\n(meta m)\n=\u003e {:proto {:Integer 0, :Decimal2 0.00M, ...}}\n```\nthen \"assigning\" away from `nil` will use the corresponding field in\nthe `:proto` map to align the type.\n\n### Non-numerics\nFor non-numeric fields typeops will use any :proto to keep map values as their intended\ntype. Attempting to \"assign\" something that is false for `instance?` results in an\nexception\n\n### Error Handling\nTypeops uses dynamic vars to help with error handling and debugging. Bind `*debug*`\nto `true` to carry information about type-incompatible `assoc` operations out via\nthe exception.\n```clojure\n(binding [typeops.core/*debug* true]\n     (assoc m :Integer \"foo\"))\n\n=\u003e ExceptionInfo Incompatible type for operation: class java.lang.String  clojure.core/ex-info (core.clj:4617)\n*e\n\n=\u003e #error{:cause \"Incompatible type for operation: class java.lang.String\",\n          :data {:map {:Integer 0,\n                       :Decimal2 0.00M,\n                       :Short 0,\n                       :Decimal 0E-15M,\n                       :Float 0.0,\n                       :Date #inst\"2019-03-28T17:14:27.816-00:00\",\n                       :Long 1,\n                       :Byte 0,\n                       :Double 0.0,\n                       :String \"\"},\n                 :key :Integer,\n                 :val \"foo\",\n                 :cur 0},\n          :via [{:type clojure.lang.ExceptionInfo,\n                 :message \"Incompatible type for operation: class java.lang.String\"\n                   .\n                   .\n```\n\nBind `*warn-on-absent-key*` to a function of two arguments `(fn [m k] ...)` which will\nbe called when `assoc` puts a key into a map that wasn't there before.\n```\n(binding [typeops.assign/*warn-on-absent-key*\n          (fn [m k]\n            (println k \"Boo!\"))]\n  (assoc m :Absent \"foo\"))\n:Absent Boo!\n=\u003e {:Absent \"foo\",\n    :Integer 0,\n    :Decimal2 0.00M,\n    :Short 0,\n    :Decimal 0E-15M,\n    :Float 0.0,\n    :Date #inst\"2019-03-28T17:14:27.816-00:00\",\n    :Long 1,\n    :Byte 0,\n    :Double 0.0,\n    :String \"\"}\n```\n\n## Usage\n\n### Per Namespace\n```clojure\n(ns myns\n  (:refer-clojure :exclude [+ - * / assoc merge])\n  (:require [typeops.core :refer :all])\n  (:require [typeops.assign :refer :all]))\n\n(+ 3.142M 2.7182818M)\n=\u003e 5.8602818M\n\n(- 3.142M 2.7182818M 3.142M)\n=\u003e -2.7182818M\n\n(* 3.142M 2.7182818M 3.142M)\n=\u003e 26.8353237278152M\n\n(/ 3.142M 2.7182818M 0.1234M)\n=\u003e 9.368M\n\n(assoc my-map k v ... ks vs)  ; assoc preserves type and precision\n(assign my-map k v ... ks vs) ; same as above\n\n```\n\n### Globally\nCall the function `init-global!` somewhere in your system start up to\nalter the vars `+` `-` `*` and `/` in `clojure.core`.\n\n## License\n\nCopyright © 2018-2019 Inqwell Ltd\n\nDistributed under the Eclipse Public License either version 1.0 or (at\nyour option) any later version.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finqwell%2Ftypeops","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finqwell%2Ftypeops","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finqwell%2Ftypeops/lists"}