clojure

Checking Clojure pre-conditions without running the function?

大憨熊 提交于 2019-12-24 10:41:41
问题 I have one function that does some (possibly lengthy) work (defn workwork [x] ...) and some other functions to check if the call will succeed ahead of time (defn workwork-precondition-1 [x] ...) . The precondition functions should be evaluated every time workwork is called (e.g. using :pre ). The precondition functions should also be collected (and:ed) in a single function and made available to client code directly (e.g. to disable a button). Which is the idiomatic way to solve this in

change struct-object state

空扰寡人 提交于 2019-12-24 10:13:33
问题 I would like to change the state of the objects andre and blastoise, adding a new property(attribute/state) to the object... the name of this new property I want to add is "tax". I tryed the code below but didnt work out... help me plz: (def andre {:owner "Andre" :type "car" :cur-speed 101 :license-plate "ABC"}) (def blastoise {:owner "Blastoise" :type "truck" :cur-speed 120 :license-plate "XYZ"}) (def car-tax[andre blastoise]) (defn calculate-car-tax [v] (for [element v] (if (> (element :cur

Count number of times a letter is in a word

自闭症网瘾萝莉.ら 提交于 2019-12-24 09:59:58
问题 I have the word describe , and I want to see how many times each letter appears in the word. Eg "e" appears twice, "d" appears once etc I have tried (for [letter (map str (seq describe))] (count (re-seq letter describe))) But I get the error ClassCastException java.lang.String cannot be cast to java.util.regex.Pattern clojure.core/re-matcher (core.clj:4667) Any help would be much appreciated 回答1: You can use frequencies to count the frequency at which each character appears in the string,

Determining Why Clojure REPL (load-file) and cake build yield error

邮差的信 提交于 2019-12-24 09:58:19
问题 Since Clojure 1.3, I am confused about the directory structure needed to build something in Clojure. I am using cake to build and cake repl. Here is what works. I have a working build directory addr_verify. The main's and ns's name is addr-verify. The project.clj refers to addr-verify as main, and in addr_verify/src there is addr_verify.clj. The ns inside addr_verify.clj refers to the addr-verify name space. Now, I had a directory mr1, but cake won't compile it right at line 1 (ns mr1 (use [

pasting a large amount of text into the clojure repl running in tmux results in errors and mixed up text

六眼飞鱼酱① 提交于 2019-12-24 09:57:59
问题 I'm having trouble pasting multiple function definitions into the clojure repl running in tmux. (the general case is pasting over a large amount of code) When I manually paste the following clojure definitions (as one paste operation) into the clojure repl not running in tmux it pastes over fine. But when pasting from tslime or directly into tmux running the clojure repl, some of the final defs get their text garbled and only some of the definitions get completed properly. (gets screwy around

How to find source of Connection Reset Error

感情迁移 提交于 2019-12-24 08:36:18
问题 Where can I go look to find the source of a connection reset error? Here are the details: I have a Clojure applet that uses clj-http.client . I need to track down what is sending the following error Feb 14, 2013 5:16:04 PM org.apache.http.impl.client.DefaultRequestDirector execute INFO: I/O exception (java.net.SocketException) caught when processing request: Connection reset Feb 14, 2013 5:16:04 PM org.apache.http.impl.client.DefaultRequestDirector execute INFO: Retrying request We have

What gotcha's exist when working with transient in Clojure 1.3?

 ̄綄美尐妖づ 提交于 2019-12-24 08:24:32
问题 I'm looking into techniques to speed up my application and I'm trying out Transients. I know I'm supposed to treat them with some caution, but what are the kinds of issues that arise? Any advice would be appreciated! 回答1: Use transients like the usual persistent data structures - that is, always use the return value from a mutator instead of the structure you called the mutator on. However, don't trust that the old version stays the same, and don't access transients from other threads. 来源:

How to obtain paths to all the child nodes in a tree that only have leaves using clojure zippers?

蓝咒 提交于 2019-12-24 07:11:19
问题 Say I have a tree like this. I would like to obtain the paths to child nodes that only contain leaves and not non-leaf child nodes. So for this tree root ├──leaf123 ├──level_a_node1 │ ├──leaf456 ├──level_a_node2 │ ├──level_b_node1 │ │ └──leaf987 │ └──level_b_node2 │ └──level_c_node1 | └── leaf654 ├──leaf789 └──level_a_node3 └──leaf432 The result would be [["root" "level_a_node1"] ["root" "level_a_node2" "level_b_node1"] ["root" "level_a_node2" "level_b_node2" "level_c_node1"] ["root" "level_a

Clojure + Clojurescript: Macro to read code of current file

吃可爱长大的小学妹 提交于 2019-12-24 05:57:50
问题 What I've Already Tried (defmacro magic [] (slurp *file*)) This works fine in clojure, but not in clojurescript (atleast not with lein figwheel). Original Question I need the following to work in both Clojure and Clojurescript. I think a macro is the right solution, but I'm open to other techniques. I want a way to read the current file as a string. For example, (ns my-test (:require blah)) (def foo 20) (println (blah/magic)) this should then result in (being printed out) (ns my-test (

Which changes to clojurescript atoms cause reagent components to re-render?

和自甴很熟 提交于 2019-12-24 05:46:14
问题 Consider the following reagent component. It uses a ref function, which updates a local state atom, based on the real size of a span element. This is done in order to re-render the component displaying its own size (defn show-my-size-comp [] (let [size (r/atom nil)] (fn [] (.log js/console "log!") [:div [:span {:ref (fn [el] (when el (reset! size (get-real-size el))))} "Hello, my size is:" ] [:span (prn-str @size)]]))) If the implementation of get-real-size returns a vector, the log message