clojure

How to evaluate a sequence of impure functions in Clojure?

白昼怎懂夜的黑 提交于 2019-12-30 17:22:09
问题 How can I evaluate a list of (impure) functions in Clojure? For instance: [#(println "1") #(println "2") #(println "3")] The expected output is: 1 2 3 Is there a way to achieve this without using macros? Something like (map evaluate fns-seq) , maybe? (I need this for drawing some graphics using the Clojure.processing API.) 回答1: user> (let [fs [#(println "1") #(println "2") #(println "3")]] (doseq [f fs] (f))) 1 2 3 nil 回答2: This will eagerly consume the whole seq, calling all functions for

Polymorphic schema validation in Clojure

不问归期 提交于 2019-12-30 17:17:48
问题 I want to use a schema to validate a request object. One of the values in the map determines which other fields are valid. For example, these would all be valid: { :name "jane" :type :dog :barking true } { :name "alan" :type :bird :cheeping true } { :name "bert" :type :fish :swimming true } Some fields are common. But others depend upon the value of :type . For example, this would be invalid: { :name "phil" :type :bird :barking false } How can such schema be expressed? I'm happy to use either

In clojure how can I test if a a symbol has been defined?

旧时模样 提交于 2019-12-30 16:18:14
问题 I would like to see if a symbol has been "def" ed, but I can't see any ifdef syntax 回答1: user> (resolve 'foo) nil user> (def foo 3) #'user/foo user> (resolve 'foo) #'user/foo 回答2: resolve or ns-resolve may do what you're looking for: user> (def a 1) #'user/a user> (def b) #'user/b user> (resolve 'a) #'user/a user> (resolve 'b) #'user/b user> (resolve 'c) nil To get a boolean: user> (boolean (resolve 'b)) true EDIT: per MayDaniel's comment, this isn't exactly what you asked for, but it will

Listbox (JList) Won't update dynamically from custom ListModel

感情迁移 提交于 2019-12-30 11:06:05
问题 I'm working on a GUI app in Clojure using Seesaw and am having trouble getting a listbox (JList in Java) to update when my custom ListModel gets updated. Here's some of my code: (deftype ActionHistoryListModel [^{:unsynchronized-mutable true} listeners ^{:unsynchronized-mutable true} listening-to] ListModel (addListDataListener [this listener] (set! listeners (conj listeners listener))) (removeListDataListener [this listener] (set! listeners (remove #(= % listener) listeners))) (getSize [this

Clojure - tail recursive sieve of Eratosthenes

旧城冷巷雨未停 提交于 2019-12-30 08:23:34
问题 I have this implementation of the sieve of Eratosthenes in Clojure: (defn sieve [n] (loop [last-tried 2 sift (range 2 (inc n))] (if (or (nil? last-tried) (> last-tried n)) sift (let [filtered (filter #(or (= % last-tried) (< 0 (rem % last-tried))) sift)] (let [next-to-try (first (filter #(> % last-tried) filtered))] (recur next-to-try filtered)))))) For larger n (like 20000) it ends with stack overflow. Why doesn't tail call elimination work here? How to fix it? 回答1: Problem: filter does lazy

Operator Overloading in Clojure

守給你的承諾、 提交于 2019-12-30 08:04:26
问题 Even looking closely over documentation on Clojure, I do not see any direct confirmation as to whether or not Clojure supports operator overloading. If it does, could someone provide me with a quick snipplet of how to overload, let's say, the "+" operator to delegate to some predefined method that we can call myPlus . I am very new to Clojure, so someone's help here would be greatly appreciated. 回答1: Clojure's (as any Lisp's) operators are plain functions; you can define an "operator" like a

Piping data through arbitrary functions in Clojure

白昼怎懂夜的黑 提交于 2019-12-30 07:58:09
问题 I know that the -> form can be used to pass the results of one function result to another: (f1 (f2 (f3 x))) (-> x f3 f2 f1) ; equivalent to the line above (taken from the excellent Clojure tutorial at ociweb) However this form requires that you know the functions you want to use at design time. I'd like to do the same thing, but at run time with a list of arbitrary functions. I've written this looping function that does it, but I have a feeling there's a better way: (defn pipe [initialData,

load a new dependency in slime?

拈花ヽ惹草 提交于 2019-12-30 06:57:26
问题 I am using emacs and swank-clojure. How do I resolve the below scenario - I have added a new dependency to project.clj. I run lein deps in a shell to get the new dep. I have an existing slime session that is open and want to use a function from the new dep. How do I get the existing slime session to load the new dependency ? Thanks, Murtaza 回答1: You should have a look at pomegranate which is designed to provide similar if not identical capabilities as the one you describe. As pointed out by

When you type “hello, world” in Clojure REPL, why does it say 'nil'?

五迷三道 提交于 2019-12-30 06:02:34
问题 I typed this into Clojure REPL (using the enclojure Netbeans plugin): user=> "hello, world" "hello, world" nil What's the nil about? 回答1: Every function or macro call returns a value in Clojure, even things like if statements or looping constructs or toplevel function definitions or print statements, which in other languages are "statements". There's no statement/expression dichotomy in Lisps; everything is an expression. So println and friends print to standard-output as a side-effect and

When you type “hello, world” in Clojure REPL, why does it say 'nil'?

心已入冬 提交于 2019-12-30 06:02:08
问题 I typed this into Clojure REPL (using the enclojure Netbeans plugin): user=> "hello, world" "hello, world" nil What's the nil about? 回答1: Every function or macro call returns a value in Clojure, even things like if statements or looping constructs or toplevel function definitions or print statements, which in other languages are "statements". There's no statement/expression dichotomy in Lisps; everything is an expression. So println and friends print to standard-output as a side-effect and