practicalli / practicalli/clojure

clojure.spec - reducing repetition with &

Open
#124 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

spec
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.