What is the difference between `&` and `and` in Clojure Spec? - clojure

I seem to have a hard time keeping apart the meaning of the & and and operators of Clojure Spec. They both seem to do sort of the same thing, only one is noted as a regex operator, a difference I'm not sure I understand the importance of.

We can see the difference between the two if we sample some data from them:
(ns playground
(:require [clojure.spec :as spec]
[clojure.spec.gen :as gen]))
(gen/generate (spec/gen (spec/and #{:a :c} #{:b :a :c})))
=> :a
(gen/sample (spec/gen (spec/and #{:a :c} #{:b :a :c})))
=> (:c :a :c :a :a :a :a :a :a :c)
As we can see spec/and matches single occurrences of what matches the two predicates #{:a :c} and #{:b :a :c}.
(gen/generate (spec/gen (spec/& #{:a :c} #{:b :a :c})))
=> [:c]
(gen/sample (spec/gen (spec/& #{:a :c} #{:b :a :c})))
=> ([:c] [:a] [:a] [:c] [:c] [:c] [:c] [:a] [:c] [:c])
spec/& on the other hand matches what's accepted by the predicates as part of a sequence.

Related

Clojure Spec to parse Reducible

The doc of clojure.spec.alpha/+ says:
Returns a regex op that matches one or more values matching
pred. Produces a vector of matches
And I can use it like this:
erdos=> (s/conform (s/+ (s/cat :e #{\a \b \c})) (seq "abc"))
[{:e \a} {:e \b} {:e \c}]
In the next step, I want to generalize it to run on Reducible values instead of sequences. But it will not work:
erdos=> (s/conform (s/+ (s/cat :e #{\a \b \c})) "abc")
:clojure.spec.alpha/invalid
How could I use clojure.spec regular expression operators on Reducibles instead of sequences? (but without creating temporary sequences.) Thank you!
You can define a conformer that converts the input (e.g. a string) to a sequence. Use and to compose it with the spec that operates on the sequence:
(s/def ::seq-from-string (s/conformer #(if (string? %) (seq %) ::s/invalid)
#(apply str %)))
(s/conform (s/and ::seq-from-string
(s/+ (s/cat :e #{\a \b \c})))
"abc")
;; => [{:e \a} {:e \b} {:e \c}]
(s/unform (s/and ::seq-from-string
(s/+ (s/cat :e #{\a \b \c})))
[{:e \a} {:e \b} {:e \c}])
;; => "abc"
Here is a more complex example with coll-of on top of that spec:
(s/conform (s/coll-of (s/and ::seq-from-string
(s/+ (s/cat :e #{\a \b \c}))))
["a" "bb"])
;; => [[{:e \a}] [{:e \b} {:e \b}]]
By the way, I am not sure why you want to avoid creating a temporary sequence around the string. When creating a sequence from a string, e.g. (seq "abc"), a lightweight sequence object StringSeq gets created that wraps the underlying string. I don't see the problem with that.

What is :<> in reagent hiccup?

I don't understand the tag ":<>" in the following code
clojure re-frame todomvc
(defn todo-app
[]
[:<>
[:section#todoapp
[task-entry]
(when (seq #(subscribe [:todos]))
[task-list])
[footer-controls]]
[:footer#info
[:p "Double-click to edit a todo"]]])
Can anyone help me on this?
That is creating a React Fragment:
https://reactjs.org/docs/fragments.html
Adding a bit more detail to the previous answer, the fragment gets spliced into a surrounding list instead of creating a child element. In this way, it is similar to the unquoted-splicing operator in Clojure ~# compared to the regular unquote operator ~. An example:
(defn middle-seq [] [ :d :e :f])
(defn middle-seq-frag [] [:<> :d :e :f])
When used to create a Reagent component, we see the difference:
[:a :b :c (middle-seq) :g :h :i] ;=> [:a :b :c [:d :e :f] :g :h :i]
[:a :b :c (middle-seq-frag) :g :h :i] ;=> [:a :b :c :d :e :f :g :h :i]
Otherwise, you would have to restructure the input and use concat:
(vec
(concat
[:a :b :c]
(middle-seq)
[:g :h :i] )) ;=> [:a :b :c :d :e :f :g :h :i]

Unsure of clojure type

Can anybody explain what the type below is in the code below which I seen in the clojure docs for string/replace?
(clojure.string/replace "The color is red" #"red" "blue")
I am talking specifically about the #"red" "blue"
Also, if I have an array-map like this:
{"red" "blue"}
How could I transform this array-map into this unknown type?
{"red" "blue"} ;=> #"red" "blue"???
If you have a map {"red" "blue"} and you'd like to use that to drive the replacement, you could do:
;; Generic form of your question - uses re-pattern to create a regex
(defn replace-with [s find replacement]
(clojure.string/replace s (re-pattern find) replacement))
;; Walk through every [find replace] pair in replacements map
;; and repeatedly apply it to string
(defn replace-with-all [s replacements]
(reduce (fn [s [f r]] (replace-with s f r))
s
replacements))
(replace-with-all "foo bar baz" {"foo" "blue" "baz" "red"})
;; "blue bar red"
In Clojure, #"....." is a Regular Expression definition. So you are replacing red with blue.
(replace s match replacement)
Replaces all instance of match with replacement in s.
match/replacement can be:
string / string
char / char
pattern / (string or function of match).
But I didn't understand what do you mean by 'transform this array-map into this unknown type'.

How can I get the positions of regex matches in ClojureScript?

In Clojure I could use something like this solution: Compact Clojure code for regular expression matches and their position in string, i.e., creating a re-matcher and extracted the information from that, but re-matcher doesn't appear to be implemented in ClojureScript. What would be a good way to accomplish the same thing in ClojureScript?
Edit:
I ended up writing a supplementary function in order to preserve the modifiers of the regex as it is absorbed into re-pos:
(defn regex-modifiers
"Returns the modifiers of a regex, concatenated as a string."
[re]
(str (if (.-multiline re) "m")
(if (.-ignoreCase re) "i")))
(defn re-pos
"Returns a vector of vectors, each subvector containing in order:
the position of the match, the matched string, and any groups
extracted from the match."
[re s]
(let [re (js/RegExp. (.-source re) (str "g" (regex-modifiers re)))]
(loop [res []]
(if-let [m (.exec re s)]
(recur (conj res (vec (cons (.-index m) m))))
res))))
You can use the .exec method of JS RegExp object. The returned match object contains an index property that corresponds to the index of the match in the string.
Currently clojurescript doesn't support constructing regex literals with the g mode flag (see CLJS-150), so you need to use the RegExp constructor. Here is a clojurescript implementation of the re-pos function from the linked page:
(defn re-pos [re s]
(let [re (js/RegExp. (.-source re) "g")]
(loop [res {}]
(if-let [m (.exec re s)]
(recur (assoc res (.-index m) (first m)))
res))))
cljs.user> (re-pos "\\w+" "The quick brown fox")
{0 "The", 4 "quick", 10 "brown", 16 "fox"}
cljs.user> (re-pos "[0-9]+" "3a1b2c1d")
{0 "3", 2 "1", 4 "2", 6 "1"}

Create a list from a string in Clojure

I'm looking to create a list of characters using a string as my source. I did a bit of googling and came up with nothing so then I wrote a function that did what I wanted:
(defn list-from-string [char-string]
(loop [source char-string result ()]
(def result-char (string/take 1 source))
(cond
(empty? source) result
:else (recur (string/drop 1 source) (conj result result-char)))))
But looking at this makes me feel like I must be missing a trick.
Is there a core or contrib function that does this for me? Surely I'm just being dumb right?
If not is there a way to improve this code?
Would the same thing work for numbers too?
You can just use seq function to do this:
user=> (seq "aaa")
(\a \a \a)
for numbers you can use "dumb" solution, something like:
user=> (map (fn [^Character c] (Character/digit c 10)) (str 12345))
(1 2 3 4 5)
P.S. strings in clojure are 'seq'able, so you can use them as source for any sequence processing functions - map, for, ...
if you know the input will be letters, just use
user=> (seq "abc")
(\a \b \c)
for numbers, try this
user=> (map #(Character/getNumericValue %) "123")
(1 2 3)
Edit: Oops, thought you wanted a list of different characters. For that, use the core function "frequencies".
clojure.core/frequencies
([coll])
Returns a map from distinct items in coll to the number of times they appear.
Example:
user=> (frequencies "lazybrownfox")
{\a 1, \b 1, \f 1, \l 1, \n 1, \o 2, \r 1, \w 1, \x 1, \y 1, \z 1}
Then all you have to do is get the keys and turn them into a string (or not).
user=> (apply str (keys (frequencies "lazybrownfox")))
"abflnorwxyz"
(apply str (set "lazybrownfox")) => "abflnorwxyz"