Fork me on GitHub
#clojure-spec
<
2019-08-27
>
joefromct15:08:14

Hi, I’ve read the spec guide and couldn’t really come up with the correct way to do the following; Apologies if i had missed something. I am looking to define a spec for a ::paragraph that consists of “::words” , “::space(s)” , and “less often” ::punctuation. It (of course) does not need to be grammatically correct. It seems like i need something like s/cat however i want the results to be not a vec but a larger string (that consists of smaller “spec” strings...“). I’m interested in having a spec for ::paragraph as well as a generator to play with a natural language processing app i’m working on. Any tips appreciated,

ataggart20:08:23

I managed to do something like that by using conformer to tokenize the string first. E.g.:

(s/and string?
       (s/conformer #(str/split % #",") #(str/join "," %))
       (s/cat ...))
But it won’t be as fast as a real parser.

ataggart20:08:18

Typed that on my phone from memory, so apologies if I got things incorrect.

Alex Miller (Clojure team)15:08:26

I think you’re probably asking for more out of spec than it is designed to provide

joefromct16:08:08

ok, that makes sense. thanks, i’ll come up with something.

vemv20:08:32

Is there a version (in some library, etc) of s/keys that allows me do manipulate the spec definition before proceeding? My current pattern for that is:

(eval `(spec/keys :req ~(conj dependencies ::foo-options)))
...Generally it works fine, but now my needs also include clojurescript, so I can't use eval

Alex Miller (Clojure team)21:08:42

I don't think there is an easy pattern for it in spec 1. CLJ-2112 has a sketch of specs for specs which lets you conform, modify then unform, but it's pretty cumbersome due to the structure of s/keys.

Alex Miller (Clojure team)21:08:28

spec 2 has several ways to do this and a new symbolic map form in work in particular

vemv23:08:48

Appreciate the confirmation, thanks!

johanatan21:08:16

what would the regex to match "one or more forward slashes" be? #"\/+" doesn't seem to compile or work

johanatan21:08:42

also tried several other permutations

johanatan21:08:35

this seems to have worked: (js/RegExp. "\\/+")

johanatan21:08:54

but pretty sure #"\\/+" did not

jahson21:08:01

user=> (re-seq #"\/+" "///////asd////")
("///////" "////")
It definetely compiles.