This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-05-30
Channels
- # announcements (2)
- # babashka (3)
- # beginners (31)
- # biff (3)
- # calva (40)
- # chlorine-clover (2)
- # clerk (5)
- # clj-kondo (10)
- # clojure (13)
- # clojure-art (1)
- # clojure-denver (16)
- # clojure-europe (44)
- # clojure-nl (1)
- # clojure-norway (27)
- # clojure-sweden (3)
- # clojure-uk (1)
- # clojurescript (49)
- # clr (6)
- # community-development (2)
- # datalevin (10)
- # datomic (63)
- # events (2)
- # fulcro (9)
- # holy-lambda (15)
- # honeysql (8)
- # hoplon (6)
- # hyperfiddle (9)
- # introduce-yourself (1)
- # kaocha (1)
- # london-clojurians (2)
- # matrix (1)
- # nbb (7)
- # off-topic (38)
- # polylith (6)
- # re-frame (4)
- # reagent (2)
- # releases (3)
- # sci (13)
- # scittle (5)
- # shadow-cljs (6)
- # sql (5)
- # tools-deps (4)
- # vim (33)
- # web-security (8)
- # xtdb (2)
Attempting to implement something akin to spec in fennel. Starting small. Was curious how the ops like s/and
and s/or
work under the hood. Was anticipating they would return a function that does some kind of check and returns data that functions like s/confirm
and s/verify
can use. However, it actually returns some kind of object I don't have much info about. Any kind of hints about how that all works?
clojure
(pprint (s/and number? int?))
#object[clojure.alpha.spec.impl$and_spec_impl$reify__1802 0x382dc417 "clojure.alpha.spec.impl$and_spec_impl$reify__1802@382dc417"]
It creates a new object that implements some required protocols. The implementation is relatively straightforward, but there's a lot under the hood to explain in plain English without copying the whole code.
That's fair. Trying to study it on my phone while waiting for the subway was probably net the best first attempt. Will just have to take my time to grasp it 🙂
I don't know fennel, but maybe https://github.com/borkdude/spartan.spec comes in handy. It's an implementation of spec with just maps and functions, no protocols
Great, spartan was exactly what I needed. That makes more sense now and unblocks me. Thanks immensely!
Alright here's my book report:
• A spec is essentially a specialized hash-map (enforced in clj via protocols) with...
• :type ::spec
,
• :cform
conform function that returns a value or ::invalid
• :explain
function that communicates which predicate failed, and the path within the tree to get there
• :describe
function that shows the symbols that it would run
• Specs are registered with unique keys against a shared registry, usually an atom
• Macros like s/and transform the predicates it receives into specs themselves
• Forms like s/and use macros instead of functions so it can quote the predicate forms passed into it