clojure

Difference in F# and Clojure when calling redefined functions

时光毁灭记忆、已成空白 提交于 2019-12-22 03:51:35
问题 In F#: > let f x = x + 2;; val f : int -> int > let g x = f x;; val g : int -> int > g 10;; val it : int = 12 > let f x = x + 3;; val f : int -> int > g 10;; val it : int = 12 In Clojure: 1:1 user=> (defn f [x] (+ x 2)) #'user/f 1:2 user=> (defn g [x] (f x)) #'user/g 1:3 user=> (g 10) 12 1:4 user=> (defn f [x] (+ x 3)) #'user/f 1:5 user=> (g 10) 13 Note that in Clojure the most recent version of f gets called in the last line. In F# however still the old version of f is called. Why is this

Getting local variables from a stack frame on the JVM

风格不统一 提交于 2019-12-22 03:51:07
问题 Is there any way to get a map or other data structure of the local variables in the current scope in on the JVM without using a debugger? That is, to get the locals of the current stack frame? I know that there are stacktrace objects, but StackTraceElement has no way to get access to any state. It just tells you what method was called where, but not what was in it. 回答1: Variable names can be included in class files to aid debuggers, but javac doesn't do it by default. It requires the -g:vars

Can't delete Parenthesis in IntelliJ/Cursive

荒凉一梦 提交于 2019-12-22 03:50:54
问题 I'm using IntelliJ/Cursive to write Clojure. I found out that the only way to erase parenthesis is to totally erase the content inside them, and only then, the parenthesis can be deleted. For example, let's say that I have the following code: (list) and I want to delete only the opening parenthesis. Once I hit backspace on the opening parenthesis, the IDE ignores this act. Only when I erase the word "list", the parenthesis can be deleted. Does anyone have any idea how to solve this? 回答1: From

Create optional fields on Clojure record?

为君一笑 提交于 2019-12-22 03:35:44
问题 When I instantiate a clojure record I get an error if I do not set all the fields of the record. How can I specify some of the fields to be optional? 回答1: defrecord declares a type and a constructor, but the type implements the clojure map interface. You just need to put the required fields in the declaration. For example, (defrecord MyRecord [required1 required2]) (defn make-my-record [r1 r2 & [opt1 opt2]] (assoc (MyRecord. r1 r2) :optional1 opt1 :optional2 opt2)) Can be used like, user>

Detect operating system in Clojure

感情迁移 提交于 2019-12-22 01:58:56
问题 Is there an equivalent of Common Lisp's *features * in Clojure, so you can detect the OS and other environment configuration? Or do I just go through the Java API for that? 回答1: Probably use the Java API. It's easy enough, no sense re-inventing the wheel. user> (System/getProperty "os.name") "Linux" user> (System/getProperty "os.version") "2.6.36-ARCH" user> (System/getProperty "os.arch") "amd64" 回答2: To add to Brian Carper's answer, you could easily create a map of system properties via the

Apply-recur macro in Clojure

对着背影说爱祢 提交于 2019-12-22 01:58:45
问题 I'm not very familiar with Clojure/Lisp macros. I would like to write apply-recur macro which would have same meaning as (apply recur ...) I guess there is no real need for such macro but I think it's a good exercise. So I'm asking for your solution. 回答1: Well, there really is no need for that, if only because recur cannot take varargs (a recur to the top of the function takes a single final seqable argument grouping all arguments pass the last required argument). This doesn't affect the

Why does my string function return a clojure.lang.LazySeq@xxxxxx?

青春壹個敷衍的年華 提交于 2019-12-22 01:55:48
问题 I've defined the following 3 functions using the leiningen REPL: (defn rand-int-range [floor ceiling] (+ floor (rand-int (- ceiling floor)))) (defn mutate-index "mutates one index in an array of char and returns the new mutated array" [source idx] (map #(if (= %1 idx) (char (+ (int %2) (rand-int-range -3 3))) %2) (iterate inc 0) source)) (defn mutate-string [source] (str (mutate-index (.toCharArray source) (rand-int (alength (.toCharArray source)))))) When I run (mutate-string "hello") ,

How do I dynamically find metadata for a Clojure function?

ε祈祈猫儿з 提交于 2019-12-22 01:28:22
问题 Say I have the following code: (defn ^{:graph-title "Function 1"} func-1 [x] (do-something-with x)) (defn get-graph-title [func] (str ((meta func) :graph-title))) I expect this to return "Function 1", but it returns nil. I think this stems from the following difference, which I don't totally comprehend: (meta func-1) => {:ns some-ns-info, :name func-1} (meta #'func-1) => {:ns some-ns-info, :name func-1, :graph-title "Function 1"} Can someone explain this to me? 回答1: The metadata is attached

Does number fall in interval in Clojure?

人盡茶涼 提交于 2019-12-22 01:28:10
问题 Is there a better way than the following: (defn in-interval? "Returns a predicate that tests if its argument falls in the inclusive interval [a, b]." [a b] (fn [x] (and (>= x a) (<= x b)))) In use: ((in-interval? 5 8) 5.5) ; true ((in-interval? 5 8) 9) ; false I don't want to use range , for example, because that constructs a lazy sequence. 回答1: Is there a better way than the following: Yes. (<= 5 8 8.5) It works with any number of arguments and check if they are ordered. With 3 arguments, it

Why does Clojure recur think it should only have one argument?

和自甴很熟 提交于 2019-12-22 01:04:38
问题 Edit: The answer to this is I was looking at the function, not loop parameters. In the second of the following two functions, I cannot figure out why the recur thinks it so only supposed to be passed one argument. CompilerException java.lang.IllegalArgumentException: Mismatched argument count to recur, expected: 1 args, got: 2, compiling:(/home/cnorton/projects/clojure/clj_in_action/mr1/src/mr1.clj:84) I am not seeing what is incorrect. (defn determine-rover-move [rover-coord mov] (println