This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-02-18
Channels
- # beginners (56)
- # boot (1)
- # cider (96)
- # cljs-dev (148)
- # clojure (60)
- # clojure-austin (11)
- # clojure-france (2)
- # clojure-italy (5)
- # clojure-russia (11)
- # clojure-spec (31)
- # clojure-uk (5)
- # clojurescript (52)
- # community-development (37)
- # cursive (3)
- # data-science (8)
- # datomic (14)
- # devcards (2)
- # emacs (1)
- # fulcro (13)
- # hoplon (1)
- # immutant (2)
- # luminus (3)
- # off-topic (2)
- # onyx (16)
- # parinfer (38)
- # re-frame (8)
- # reagent (5)
- # shadow-cljs (332)
- # spacemacs (5)
- # specter (5)
- # sql (6)
- # vim (52)
A :sherman.grammar/expanding-term
should contain ONE or more :sherman.grammar/expanding-symbol
s, and ZERO or more :sherman.grammar/terminating-symbol
s
I'm trying to express, "A string that, if you split it at the spaces, one or more of the elements of that collection would be an ::expanding-symbol
"
s/alt could spec the result of splitting the string at spaces
I've changed my terminology a little, but I'm still failing to reject invalid input:
I hypothesize that the right thing to do is to make functions do more work, and specs do less. E.g., much as noisesmith suggested when they suggested tokenizing first, I could have a predicate which detects well-formed tokens, and base a spec off of that
I'm still working out what responsibilities belong in spec and what belong in plain ol' clojure code 🙂
@mathpunk another option using s/&
:
(s/def ::valid-term
(s/& (s/* (s/alt :terminating ::terminating-symbol
:expanding ::expanding-symbol))
#(some (comp #{:expanding} first) %)))
(s/conform ::valid-term (tokenize "This #expands# also"))
=> [[:terminating "This"] [:expanding "#expands#"] [:terminating "also"]]
I'll channel Alex Miller and say clojure.spec
is not intended for string parsing -- there are much better tools out that for that (e.g., instaparse
).
The "regular expression" part of spec is intended for sequence processing.
It is not
Come join us
We have cookies
And somewhat lackluster but existent respond rate to issues
I would expect clojure.spec.alpha/and
to be commutative. Is anyone else seeing this behavior?
> *clojurescript-version*
"\"1.9.946\""
> (defn f [m] (println m) (-> m (get 1) :db/id (= 1)))
"#'invest-calc.state/f"
> (s/def :account/works (s/and f (s/map-of any? (s/keys))))
":account/works"
> (s/def :account/fails (s/and (s/map-of any? (s/keys)) f))
":account/fails"
> (s/conform :account/works {1 {:db/id 1}})
{1 #:db{:id 1}}
"{1 #:db{:id [:db/id 1]}}"
> (s/conform :account/fails {1 {:db/id 1}})
{1 #:db{:id [:db/id 1]}}
":cljs.spec.alpha/invalid"
s/and flows conformed results through predicates
Heh, I guess I should read the docs closer. Is it expected that it would operate differently in normal Clojure?
invest-calc.state> *clojure-version*
{:major 1, :minor 9, :incremental 0, :qualifier nil}
invest-calc.state> (defn f [m] (println m) (-> m (get 1) :db/id (= 1)))
#'invest-calc.state/f
invest-calc.state> (s/def :account/works (s/and f (s/map-of any? (s/keys))))
:account/works
invest-calc.state> (s/def :account/fails (s/and (s/map-of any? (s/keys)) f))
:account/fails
invest-calc.state> (s/conform :account/works {1 {:db/id 1}})
{1 #:db{:id 1}}
{1 #:db{:id 1}}
invest-calc.state> (s/conform :account/fails {1 {:db/id 1}})
{1 #:db{:id 1}}
{1 #:db{:id 1}}