How to get the output of `doc` function in clojure? - clojure

I tried to save the output of the doc function, eg:
user> (def doc-str-split (doc str/split))
-------------------------
clojure.string/split
([s re] [s re limit])
Splits string on a regular expression. Optional argument limit is
the maximum number of splits. Not lazy. Returns vector of the splits.
#'user/doc-str-split
user> doc-str-split
nil
user>
However, I got nil for the doc-str-split. I tried to get the type of the doc output:
user> (type (doc str/split))
-------------------------
clojure.string/split
([s re] [s re limit])
Splits string on a regular expression. Optional argument limit is
the maximum number of splits. Not lazy. Returns vector of the splits.
nil
Still get a nil. How to save the output of the doc function?

You can use with-out-str to capture the output, like so:
user> (def doc-str-split (with-out-str (doc str/split)))
#'user/doc-str-split
user> (println doc-str-split)
-------------------------
clojure.string/split
([s re] [s re limit])
Splits string on a regular expression. Optional argument limit is
the maximum number of splits. Not lazy. Returns vector of the splits.
nil
user> (type (with-out-str (doc str/split)))
java.lang.String

You can get just the doc string like so:
user.core=> (def doc (prn (:doc (meta #'clojure.string/split))))
"Splits string on a regular expression. Optional argument limit is
the maximum number of splits. Not lazy. Returns vector of the splits."

Related

How to correctly check if a string is equal to another string in Clojure?

I am looking for better ways to check if two strings are equal in Clojure!
Given a map 'report' like
{:Result Pass}
, when I evaluate
(type (:Result report))
I get : Java.Lang.String
To write a check for the value of :Result, I first tried
(if (= (:Result report) "Pass") (println "Pass"))
But the check fails.
So I used the compare method, which worked:
(if (= 0 (compare (:Result report) "Pass")) (println "Pass"))
However, I was wondering if there is anything equivalent to Java's .equals() method in Clojure. Or a better way to do the same.
= is the correct way to do an equality check for Strings. If it's giving you unexpected results, you likely have whitespace in the String like a trailing newline.
You can easily check for whitespace by using vec:
(vec " Pass\n")
user=> [\space \P \a \s \s \newline]
As #Carcigenicate wrote, use = to compare strings.
(= "hello" "hello")
;; => true
If you want to be less strict, consider normalizing your string before you compare. If we have a leading space, the strings aren't equal.
(= " hello" "hello")
;; => false
We can then define a normalize function that works for us.
In this case, ignore leading and trailing whitespace and
capitalization.
(require '[clojure.string :as string])
(defn normalize [s]
(string/trim
(string/lower-case s)))
(= (normalize " hellO")
(normalize "Hello\t"))
;; => true
Hope that helps!

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"}

Clojure join fails to create a string from the result of filter function

I'm trying to write a function that takes a string and returns a result of a filter function (I'm working through 4clojure problems). The result must be a string too.
I've written this:
(fn my-caps [s]
(filter #(Character/isUpperCase %) s))
(my-caps "HeLlO, WoRlD!")
Result: (\H \L \O \W \R \D)
Now I'm trying to create a string out of this list, using clojure.string/join, like this:
(fn my-caps [s]
(clojure.string/join (filter #(Character/isUpperCase %) s)))
The result is however the same. I've also tried using apply str, with no success.
You have to convert the lazy sequence returned by filter into a string, by applying the str function. Also, use defn to define a new function - here's how:
(defn my-caps [s]
(apply str (filter #(Character/isUpperCase %) s)))
It works as expected:
(my-caps "HeLlO, WoRlD!")
=> "HLOWRD"
The last code snippet you pasted works fine. join indeed does return a string.
Try this:
(defn my-caps [s]
(->> (filter #(Character/isUpperCase %) s)
(apply str)))
filter function returns a lazy sequence. If you want to get a string, you should transform the sequence to string by applying str function.

Convert hyphenated string to CamelCase

I'm trying to convert a hyphenated string to CamelCase string. I followed this post: Convert hyphens to camel case (camelCase)
(defn hyphenated-name-to-camel-case-name [^String method-name]
(clojure.string/replace method-name #"-(\w)"
#(clojure.string/upper-case (first %1))))
(hyphenated-name-to-camel-case-name "do-get-or-post")
==> do-Get-Or-Post
Why I'm still getting the dash the output string?
You should replace first with second:
(defn hyphenated-name-to-camel-case-name [^String method-name]
(clojure.string/replace method-name #"-(\w)"
#(clojure.string/upper-case (second %1))))
You can check what argument clojure.string/upper-case gets by inserting println to the code:
(defn hyphenated-name-to-camel-case-name [^String method-name]
(clojure.string/replace method-name #"-(\w)"
#(clojure.string/upper-case
(do
(println %1)
(first %1)))))
When you run the above code, the result is:
[-g g]
[-o o]
[-p p]
The first element of the vector is the matched string, and the second is the captured string,
which means you should use second, not first.
In case your goal is just to to convert between cases, I really like the camel-snake-kebab library. ->CamelCase is the function-name in question.
inspired by this thread, you could also do
(use 'clojure.string)
(defn camelize [input-string]
(let [words (split input-string #"[\s_-]+")]
(join "" (cons (lower-case (first words)) (map capitalize (rest words))))))

Clojure: get list of regex matches

Perhaps I'm going about this all wrong, but I'm trying to get all the matches in a string for a particular regex pattern. I'm using re-matcher to get a Match object, which I pass to re-find, giving me (full-string-match, grouped-text) pairs. How would I get a sequence of all the matches produced by the Match object?
In Clojuresque Python, it would look like:
pairs = []
match = re-matcher(regex, line)
while True:
pair = re-find(match)
if not pair: break
pairs.append(pair)
Any suggestions?
You probably want to use the built in re-seq and Clojure's built in regex literal. Don't mess with underlying java objects unless you really have too.
(doc re-seq)
clojure.core/re-seq
([re s])
Returns a lazy sequence of successive matches of pattern in string,
using java.util.regex.Matcher.find(), each such match processed with
re-groups.
For example:
user> (re-seq #"the \w+" "the cat sat on the mat")
("the cat" "the mat")
In answer to the follow-up comment, group captures will result in a vector of strings with an element for each part of the group in a match:
user> (re-seq #"the (\w+(t))" "the cat sat on the mat")
(["the cat" "cat" "t"] ["the mat" "mat" "t"])
You can extract a specific element by taking advantage of the elegant fact that vectors are functions of their indices.
user> (defn extract-group [n] (fn [group] (group n)))
#'user/extract-group
user> (let [matches (re-seq #"the (\w+(t))" "the cat sat on the mat")]
(map (extract-group 1) matches))
("cat" "mat")
Or you can destructure the matches (here using a for macro to go over all the matches but this could also be done in a let or function argument binding):
user> (dorun
(for [[m1 m2 m3] (re-seq #"the (\w+(t))" "the cat sat on the mat")]
(do (println "m1:" m1)
(println "m2:" m2)
(println "m3:" m3))))
m1: the cat
m2: cat
m3: t
m1: the mat
m2: mat
m3: t