Fork me on GitHub
#beginners
<
2018-10-07
>
coinedtalk07:10:18

Coming from Python... handling strings in clojure is a nightmare. Any advice?

coinedtalk07:10:29

Should I maybe embrace lisp and courageously face and embrace cl-format? :p

andy.fingerhut07:10:52

I know some Python, and more Clojure. Anything in particular you are looking to do that is giving you nightmares?

andy.fingerhut07:10:27

Clojure has subs function that is similar to Python slices, but does not do negative indices. You can work around that by writing a function that lets you do negative indices if you want, or just use (- (count my-str) 5) instead of -5. Java regexes are available in Clojure, which for common simple things lets you do everything the Python import re does. I try to stay away from the fancier regex features like forward/backwards context, Unicode character sets, etc.

andy.fingerhut07:10:26

Clojure has format and printf that is closer to C printf and Python ("Print int here %d and string here %s" % (my_int, my_str))

andy.fingerhut07:10:52

I wouldn't recommend embracing Clojure's cl-format unless it sounds like your cup of tea.

coinedtalk08:10:00

Must have missed subs somehow... thanks :) and yeah on second thought it might be less of a headache just to make some helper functions like that

orestis09:10:58

Otoh the str function is also very handy.

sambaran201010:10:56

Hi Guys, I am trying to update value for some specific key in a map using .toUpperCase . But getting the Compiler exception java.lang.RuntimeException: Unable to resolve symbol: .toUpperCase in this context (def books [{:name "The adventure of sherlock holmes", :author "Doyle"} {:name "Midnight's Children", :author "Rushdie"}]) (map #(if (contains? % :name) (update-in % [:name] .toUpperCase)) books) CompilerException java.lang.RuntimeException: Unable to resolve symbol: .toUpperCase in this context. Can someone please help me in understanding this issue?

sambaran201010:10:26

figured out ... (map #(if (contains? % :name) (update-in % [:name] (fn [name] (.toUpperCase name) ) )) books)

rmprescott04:10:18

This is a common problem. You can't pass a Java function expecting it to be a Clojure function. You've wrapped it in a (fn ...) here. Another approach is the shorthand: #(.toUpper %)

orestis10:10:47

See also clojure.string/upper-case