clojure

ClojureScript function prints strings, but will not return hiccup

心不动则不痛 提交于 2019-12-25 01:44:07
问题 I have a ClojureScript component: (defn main-panel [] (def nodes (-> @(re-frame/subscribe [::subs/nodes]))) (defn list-nodes [node] (prn (node :name))) (reagent/create-class {:component-did-mount (fn [] (api-service/get-file-tree)) :reagent-render (fn [] (doseq [node nodes] (if (= (node :depth) 0) (list-nodes node))))})) which prints strings to the console. But when I change the first function to: (defn list-nodes [node] [:<> [:h1 (node :name)]]) I don't get any html that is rendered - no

How to define the partitions (factorizations w.r.t. concatenation) of a sequence as a lazy sequence of lazy sequences in Clojure

倾然丶 夕夏残阳落幕 提交于 2019-12-25 01:39:56
问题 I am new to Clojure and I want to define a function pt taking as arguments a number n and a sequence s and returning all the partitions of s in n parts, i.e. its factorizations with respect to n -concatenation. for example (pt 3 [0 1 2]) should produce: (([] [] [0 1 2]) ([] [0] [1 2]) ([] [0 1] [2]) ([] [0 1 2] []) ([0] [] [1 2]) ([0] [1] [2]) ([0] [1 2] []) ([0 1] [] [2]) ([0 1] [2] []) ([0 1 2] [] [])) with the order being unimportant. Specifically, I want the result to be a lazy sequence

Clojure - how to let the library users choose which printing function to use, to display data structures?

。_饼干妹妹 提交于 2019-12-24 22:00:59
问题 I'm writing a small debugging library and I would like to let users choose how to display data structures. I was imagining that users could require it in this kind of way: (ns some-user-namespace (:require [clojure.pprint] [my.library :with-args {print-fn clojure.pprint/pprint}])) Is something like this possible, and if not, how can I solve this problem? 回答1: It's not possible to do it this way. If you really to offer this kind of setup, you could provide a configuration function to be called

clojure: ExceptionInInitializerError in Namespace.<init> loading from a non-default classpath

本小妞迷上赌 提交于 2019-12-24 17:54:52
问题 In attempting to load an AOT-compiled class from a non-default classpath, I receive the following exception: Traceback (innermost last): File "test.jy", line 10, in ? at clojure.lang.Namespace.<init>(Namespace.java:34) at clojure.lang.Namespace.findOrCreate(Namespace.java:176) at clojure.lang.Var.internPrivate(Var.java:149) at aot_demo.JavaClass.<clinit>(Unknown Source) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:247) at sun.reflect

Inspect state atom which gets updated by a ring handler

混江龙づ霸主 提交于 2019-12-24 16:00:41
问题 Consider the following scenario: A minimal boot task starts up a http server: (boot (serve :handler 'myapp.server/handler :port 3000)) (This might be launched in several ways, here it's ok to just run it from a nrepl session, e.g. started by boot repl from the terminal) The handler is represented by the function handler inside the namespace myapp.server . The corresponding file looks like this: (ns myapp.server (:require ...)) (defonce server-state (atom {:nr 0})) (defn handler [req] (prn

Distributing list items to variables in clojure

…衆ロ難τιáo~ 提交于 2019-12-24 14:29:44
问题 When I pass this function (into [] (map #(+ %1 %2) [1 2] [5 6])) I get this result: [6 8] What should I do to get this: [6 7 7 8] while keeping this #(+ %1 %2) ? Seems like map isn't the right function in this case. 回答1: Use for when you want a Cartesian product: user=> (for [x [1 2] y [5 6]] #_=> (+ x y)) (6 7 7 8) 回答2: for is one option as Alex answer shows. map can also be used (with mapcat ) as shown below: user=> (mapcat #(map (partial + %1) [5 6]) [1 2]) (6 7 7 8) 来源: https:/

How do convert this code and my thinking to a functional mindset (Clojure)?

浪尽此生 提交于 2019-12-24 14:28:22
问题 How do I convert this JavaScript code to Clojure? I am trying to draw a (x,y) world where the cells are on or off according to the fill property. In the example below I am trying to print the rows then columns but my next step is to move the fill property around (up, down, left, right). So, I don't want an answer which wouldn't work if I were not printing the data structure. My goal is to understand how to think about this problem in a functional way. It was easy for me to solve this problem

How to figure out what protocols the type implements?

筅森魡賤 提交于 2019-12-24 12:44:16
问题 Given some type or record, how can I get all the protocols it implements? Let's say we have the following code: (defprotocol P1 (op1 [this])) (defprotocol P2 (op2 [this])) (defrecord R [] P1 (op1 [_] 1) P2 (op2 [_] 2)) And what I need is a function that does something like this: (all-protocols-for-type R) ;; => (P1 P2) It will be good if there's something backend-agnosting, because I'd like to have a mechanism for both Clojure and ClojureScript. UPD: the intention for this is to introspect

Clojure rearrangement and mapping of vector of vectors

放肆的年华 提交于 2019-12-24 12:23:34
问题 Here is the situation: I have a vector of vectors ("data"), a set of headers, a subset of headers ("primary headers"), a constant ("C"), an element-wise function ("f"), and the remaining headers ("secondary headers"). My goal is to take the "data" and produce a new vector of vectors. Example data: [[1.0 "A" 2.0] [1.0 "B" 4.0]] Example headers: ["o1" "i1" "i2"] Example primary headers: ["i1" "i2"] Example secondary headers: ["o1"] Example new vector of vectors: [[(f "A") (f 2.0) C (f 1.0)] [(f

“Updating” a map/data structure in consecutive function calls in Clojure

爱⌒轻易说出口 提交于 2019-12-24 10:46:37
问题 I want to "update" a map through multiple calls of functions, in Clojure. The idea is expressed as below: (defn foo1 [a-map] (assoc a-map :key1 "value1")) (defn foo2 [a-map] (assoc a-map :key2 "value2")) (defn foo3 [a-map] (assoc a-map :key3 "value3")) (defn -main [] (let [a-map {}] (do (foo1 a-map) (foo2 a-map) (foo3 a-map) a-map))) Apparently this piece of code is wrong because the a-map is not updated outside the scope of subroutines. It's written like this simply because it's clearer as