practicalli / practicalli/clojure
clojure.spec - reducing repetition with &
Nobody has claimed this yet.
- Dominant language
- Makefile
- Stars
- 117
- Forks
- 36
- PR merge metrics
- No merged PRs in 30d
Description
Remove the repetition here without nesting or additional keywords in the conformed data?
(s/def ::fact
(s/cat :var (s/? ::?symbol)
:type (s/or :simple symbol? :acc ::acc-vec)
:destruct (s/? vector?)
:exprs (s/* list?)))(s/def ::fact-without-var
(s/cat :type (s/or :simple symbol? :acc ::acc-vec)
:destruct (s/? vector?)
:exprs (s/* list?)))
doesn't the first one include the second?
But I want a spec for data where a var is forbidden. And one where it's optional.
ah
so, you can create a new spec that is an s/cat that just contains the common portions, then reference it in both ::fact specs
(s/def ::fact-without-var
(s/cat :type (s/or :simple symbol? :acc ::acc-vec)
:destruct (s/? vector?)
:exprs (s/* list?)))(s/def ::fact
(s/cat :var (s/? ::?symbol)
:tail ::fact-without-var))
that does introduce nesting in the conformed data so is not everything you want
another way would be to restrict the bigger one
maybe something like
(s/def ::fact
(s/cat :var (s/? ::?symbol)
:type (s/or :simple symbol? :acc ::acc-vec)
:destruct (s/? vector?)
:exprs (s/* list?)))(s/def ::fact-without-var
(s/& ::fact #(nil? (:var %))))
can't say I've done this before, might need to tweak that predicate more
& just lets you and arbitrary predicates into the regex spec
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Review the ::fact and ::fact-without-var specs in the issue, focusing on the behavior of s/cat, s/def, and s/&. Determine whether the repeated portions can be shared while preserving flat conformed data and separate handling of optional versus forbidden vars; done means the intended spec behavior is established.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- clojure
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100