What is :<> in reagent hiccup? - clojure

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]

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 the difference between `&` and `and` in Clojure Spec?

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.

Convert set to regex pattern in clojure

If I have this set
(def my-set #{"foo.clj" "bar.clj" "baz.clj"})
How can I turn it to this pattern string:
"foo\.clj|bar\.clj|baz\.clj"
My attempt : 
(defn set->pattern-str [coll]
(-> (clojure.string/join "|" coll)
(clojure.string/replace #"\." "\\\\.")))
(set->pattern-str my-set)
=> "foo\\.clj|baz\\.clj|bar\\.clj" ;I get the double backslash
Better ideas?
In case your set of strings might have other metacharacters than just . in them, a more general approach is to ask the underlying java.util.regex.Pattern implementation to escape everything for us:
(import 'java.util.regex.Pattern)
(defn set->pattern-str [coll]
(->> coll
(map #(Pattern/quote %))
(clojure.string/join \|)
re-pattern))
IDEone link here. Remember, IDEone is not a REPL, and you have to tell it to put values on stdout with e.g. println before you can see them.
You were close to the final solution. Double backslash is displayed because it is shown escaped. When you turn it into a seq you will see individual characters:
(seq "foo\\.clj")
;;=> (\f \o \o \\ \. \c \l \j)
And working solution:
(def my-set #{"foo.clj" "bar.clj" "baz.clj"})
(def my-set-pattern
(-> (clojure.string/join "|" my-set)
(clojure.string/replace "." "\\.")
(re-pattern)))
(re-matches my-set-pattern "foo.clj")
;;=> "foo.clj"
(re-matches my-set-pattern "bar.clj")
;;=> "bar.clj"
(re-matches my-set-pattern "baz.clj")
;;=> "baz.clj"
(re-matches my-set-pattern "foo-clj")
;;=> nil
Edit: OK, this one does in fact work. Probably want to break it apart a bit more if it's meant to be long lived code, but this is the simplest way I could find to do it with minimal string munging.
(defn is-matching-file-name [target-string]
(re-matches
(re-pattern (clojure.string/escape (String/join "|" my-set) {\. "\\."}))
target-string))
The clojure.string/escape here takes two arguments: the string to escape, and a mapping of the characters to escape to the replacement strings. The key in this map is the literal \. and the value needs two backslashes since we want to include one backslash preceding any . in the final string to be used as the argument for the re-pattern function.

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"