Fork me on GitHub

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


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


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


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.


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))


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


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


Otoh the str function is also very handy.


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?


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


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 %)


See also clojure.string/upper-case

👍 4