clojure

Clojure - Is it possible to increment a variable within a doseq statement?

邮差的信 提交于 2020-01-14 16:37:53
问题 I am trying to iterate over a list of files in a given directory, and add an incrementing variable i = {1,2,3.....} to their names. Here is the code I have for iterating through the files and changing each file's name: (defn addCounterToExtIn [d] (def i 0) (doseq [f (.listFiles (file d)) ] ; make a sequence of all files in d (if (and (not (.isDirectory f)) ; if file is not a directry and (= '(\. \i \n) (take-last 3 (.getName f))) ) ; if it ends with .in (fs/rename f (str d '/ i (.getName f)))

Getting argument type hints of a function in Clojure

青春壹個敷衍的年華 提交于 2020-01-14 13:06:41
问题 I'm looking to extract the type hint information of a function's arguments, but I can't seem to find a way to access that information. For example, say I have the following function: (defn ^Double do-something [^String a, ^String b] 5.0) Pulling the tag is straightforward: (:tag (meta #'do-something)) ; => java.lang.Double For the arguments, however, something like this won't work: (:arglists (meta #'do-something)) ; => ([a b]) This just gives me the arguments and not the type information. Is

Interface which contains conj?

☆樱花仙子☆ 提交于 2020-01-14 10:33:34
问题 As an exercise, I am developing a data structure similar to Vector. I have implemented all interfaces which IPersistentVector extends, but I have not found the interface where 'conj' is defined. Which interface is that? Thanks! 回答1: clojure.lang.IPersistentCollection/cons . It was named cons originally, and that's stuck around in the interface even though the Clojure function for it is now called conj . 来源: https://stackoverflow.com/questions/8781213/interface-which-contains-conj

Clojure STM ambiguity factor

三世轮回 提交于 2020-01-14 10:13:39
问题 In Clojure we use STM for Concurrency. My question is STM uses point in time value of data , isn't this introduce ambiguity ? How could we know what value is accessed ? 回答1: The STM in Clojure provides (through refs and dosync) a transaction context where all updates are guaranteed to be made "at the same time" to all the refs involved when viewed from the outside world. The objective is to maintain consistency of the values in the system, the typical example is the transfer of money between

Howto use lib-noir stateful-sessions in Compojure

蓝咒 提交于 2020-01-14 09:31:28
问题 I think I have a fairly straightforward problem here. But I've been looking at this screen too long. So I'm trying (and failing) to get stateful sessions working in Compojure. The refheap code paste is here . You can see me trying to use lib-noir (line 62) to initialize stateful sessions. Then when the app is running, I try to make a call to session/put! some data in the session (line 43). Now, this stacktrace says that in session.put!, lib-noir is trying to swap out a session var that hasn't

In clojure, how to do code templating when implementing a macro using recursion

我的梦境 提交于 2020-01-14 09:16:50
问题 I am trying to implement a macro to recursively converting an infix list into a prefix one. I encounter a problem as follows: ;;this works (defmacro recursive-infix [form] (list (second form) (first form) (if (not (seq? (nth form 2))) (nth form 2) (recursive-infix (nth form 2))))) ;;this doesn't work (defmacro my-recursive-infix [form] `(~(second form) ~(first form) (if (not (seq? ~(nth form 2))) ~(nth form 2) (my-recursive-infix ~(nth form 2))))) (macroexpand '(recursive-infix (10 + 10))) ;

In clojure, how to do code templating when implementing a macro using recursion

拜拜、爱过 提交于 2020-01-14 09:16:47
问题 I am trying to implement a macro to recursively converting an infix list into a prefix one. I encounter a problem as follows: ;;this works (defmacro recursive-infix [form] (list (second form) (first form) (if (not (seq? (nth form 2))) (nth form 2) (recursive-infix (nth form 2))))) ;;this doesn't work (defmacro my-recursive-infix [form] `(~(second form) ~(first form) (if (not (seq? ~(nth form 2))) ~(nth form 2) (my-recursive-infix ~(nth form 2))))) (macroexpand '(recursive-infix (10 + 10))) ;

What are the semantic implications of :volatile-mutable versus :unsynchronized-mutable?

我只是一个虾纸丫 提交于 2020-01-14 08:55:16
问题 I was studying a clojure lib when I noticed that a mutable field was annotated with ^:unsynchronized-mutable. Mutable is mutable, but I had no idea what the unsynchronized part meant, so I read the docs, which contain: Note well that mutable fields are extremely difficult to use correctly, and are present only to facilitate the building of higher level constructs, such as Clojure's reference types, in Clojure itself. They are for experts only - if the semantics and implications of :volatile

How do I check if a variable implements an interface in clojure?

社会主义新天地 提交于 2020-01-14 07:51:09
问题 So I have defined say a keyword: (def a :hello) how do I check that it implements the IFn interface? 回答1: For the general case, you can use the instance? predicate: (instance? <class-or-interface> <object>) Quoting the documentation: (instance? c x) evaluates x and tests if it is an instance of the class c. Returns true or false. For example: (instance? java.lang.String "test") > true (instance? java.io.Serializable "test") > true For the code in the question, do something like this:

How to convert map to URL query string in Clojure/Compojure/Ring?

醉酒当歌 提交于 2020-01-14 07:23:41
问题 In Clojure / Compojure, how do I convert a map to a URL query string? {:foo 1 :bar 2 :baz 3} to foo=1&bar=2&baz=3 Is there any utility method to do this in compojure? 回答1: Yes, there is a utility for this already that doesn't involve Hiccup or rolling your own string/join/URLEncoder function: user=> (ring.util.codec/form-encode {:foo 1 :bar 2 :baz 3}) "foo=1&bar=2&baz=3" user=> Compojure depends on ring/ring-core, which includes ring.util.codec, so you already have it. 回答2: Something like: