clojure

Difference between using “def” to update a var and “alter-var-root”

落花浮王杯 提交于 2019-12-18 03:03:25
问题 What's the difference between using "def" to update a var and using "alter-var-root"? e.g. (def x 3) (def x (inc x)) vs (def x 3) (alter-var-root #'x inc) 回答1: I find alter-var-root very rarely comes up in idiomatic Clojure code; not that there is anything wrong with it, it's just intended for corner cases. If you find yourself using it to build loops and such it's a sign something needs a different approach. I mostly see it in initialization routines for setting access credentials or loggers

using clojure symbol function to make indirect function call

橙三吉。 提交于 2019-12-18 03:03:15
问题 I'm expecting below that I should be able to call my function squared indirectly via the symbol function, but its not working. What am I doing wrong here: user=> (defn squared [x] (* x x)) #'user/squared user=> (squared 2) 4 user=> ((symbol "squared") 2) nil user=> ((symbol "user" "squared") 2) nil user=> 回答1: The symbol itself does not hold your function, the Var it names does. This says "take the contents ( @ = deref ) of the Var ( resolve ) named by the symbol ( symbol ) whose name is

Convert a sequence of strings to integers (Clojure)

最后都变了- 提交于 2019-12-18 03:02:25
问题 I currently am having an issue where I have to read a text file from the command line containing at least one integer. I'm reading the file, doing a regular-expression match to ignore whitespace. (re-seq #"[0-9]+" (slurp (first *command-line-args*))) After this I have to write a whole function just to convert this sequence of strings into a sequence of integers. Apparently I cannot map Integer. to the sequence (unless I am using map incorrectly). Is there some elegant way of handling this,

enabling UTF-8 encoding for clojure source files

醉酒当歌 提交于 2019-12-18 02:42:14
问题 I'm working on a project which involves maven, java and clojure. The problem I'm facing is this, I have some UTF-8 chars in my clojure source files because of which my source code is not interpreted correctly by the java compiler, I kinda got it working by setting the environment variable JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8 , but what I want is to pass this property through MAVEN. I have already tried setting MAVEN_OPTS=-Dfile.encoding but this doesn't seem to work. I have also tried

enabling UTF-8 encoding for clojure source files

萝らか妹 提交于 2019-12-18 02:41:29
问题 I'm working on a project which involves maven, java and clojure. The problem I'm facing is this, I have some UTF-8 chars in my clojure source files because of which my source code is not interpreted correctly by the java compiler, I kinda got it working by setting the environment variable JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8 , but what I want is to pass this property through MAVEN. I have already tried setting MAVEN_OPTS=-Dfile.encoding but this doesn't seem to work. I have also tried

Executing a function with a timeout

无人久伴 提交于 2019-12-17 23:17:28
问题 What would be an idiomatic way of executing a function within a time limit? Something like, (with-timeout 5000 (do-somthing)) Unless do-something returns within 5000 throw an exception or return nil. EDIT: before someone points it out there is, clojure (with-timeout ... macro) but with that the future keeps executing that does not work in my case. 回答1: This isn't something you can do 100% reliably on the JVM. The only way to stop something after a while is to give it a new thread, and then

clojure: adding a debug trace to every function in a namespace?

风流意气都作罢 提交于 2019-12-17 22:40:00
问题 just started using log4j in one of my home-projects and I was just about to break out the mouse and cut-and-paste (trace (str "entering: " function-name)) into every function in a large module. then the voice of reason caught up and said "there has simply got to be a better way"... I can think of making a macro that wraps a whole block of functions and adds the traces to them or something like that? Any advice from the wise Stack-overflowing-clojurians? 回答1: No need for a macro: (defn trace

Anonymous function shorthand

早过忘川 提交于 2019-12-17 22:29:40
问题 There's something I don't understand about anonymous functions using the short notation #(..) The following works: REPL> ((fn [s] s) "Eh") "Eh" But this doesn't: REPL> (#(%) "Eh") This works: REPL> (#(str %) "Eh") "Eh" What I don't understand is why (#(%) "Eh") doesn't work and at the same time I don't need to use str in ((fn [s] s) "Eh") They're both anonymous functions and they both take, here, one parameter. Why does the shorthand notation need a function while the other notation doesn't?

Clojure: rest vs. next

橙三吉。 提交于 2019-12-17 22:12:21
问题 I'm having a hard time understanding the difference between rest and next in Clojure. The official site's page on laziness indicates that the preference should probably be to use rest , but it doesn't really explain clearly the difference between the two. Can anybody provide some insight? 回答1: As the page you linked described, next is stricter than (the new behaviour of) rest because it needs to evaluate the structure of the lazy cons to know whether to return nil or a seq. rest on the other

How do we do both left and right folds in Clojure?

给你一囗甜甜゛ 提交于 2019-12-17 21:55:27
问题 This question was migrated from Software Engineering Stack Exchange because it can be answered on Stack Overflow. Migrated 6 years ago . Reduce works fine but it is more like fold-left. Is there any other form of reduce that lets me fold to right ? 回答1: Let's look at a possible definition of each: (defn foldl [f val coll] (if (empty? coll) val (foldl f (f val (first coll)) (rest coll)))) (defn foldr [f val coll] (if (empty? coll) val (f (foldr f val (rest coll)) (first coll)))) Notice that