clojure

Clojure hot code swapping for uberjars/.classes

我的未来我决定 提交于 2019-12-21 12:38:44
问题 I want to have hot code swapping between project updates, but I haven't found any information about how to load .class files dynamically. More specifically, I want something like that: Make lein uberjar , obtain some-client-0.0.0-standalone.jar . Run it with java -jar some-client-0.0.0-standalone.jar . Make changes to project. Obtain new program version, some-client-0.0.1-standalone.jar , copy it to some-client-0.0.0-standalone.jar directory. Client receive sequence of commands needed to

is there a way to read all the forms in a clojure file?

☆樱花仙子☆ 提交于 2019-12-21 12:25:28
问题 If I use (-> "<file>.clj" (slurp) (read-string)) That will only read the first form in the file (typically the ns declaration). Is there a way to retrieve a list of forms from the file? I'd prefer if no external libraries are used. 回答1: stick a [ and ] onto the start and end of the string to make the whole file into a single vector form: user> (clojure.pprint/pprint (read-string (str "[" (slurp "/home/arthur/hello/project.clj") "]"))) [(defproject hello "0.1.0-SNAPSHOT" :description "FIXME:

How to filter a list based on user input with ClojureScript and Om?

大憨熊 提交于 2019-12-21 12:18:56
问题 I just started to use Om (a reactjs based library for ClojureScript). I would like to filter a list based on user input. The following works but the solution seems to be to complicated. Is there a better one ? (ns om-tut.core (:require-macros [cljs.core.async.macros :refer [go]]) (:require [om.core :as om :include-macros true] [om.dom :as dom :include-macros true] [clojure.string :as string])) (enable-console-print!) (def app-state (atom {:list ["Lion" "Zebra" "Buffalo" "Antelope"]})) (defn

Why is this prime sieve implementation slower?

我的未来我决定 提交于 2019-12-21 12:18:32
问题 I was just experimenting a bit with (for me) a new programming language: clojure. And I wrote a quite naive 'sieve' implementation, which I then tried to optimise a bit. Strangely enough though (for me at least), the new implementation wasn't faster, but much slower... Can anybody provide some insight in why this is so much slower? I'm also interested in other tips in how to improve this algorithm... Best regards, Arnaud Gouder ; naive sieve. (defn sieve ([max] (sieve max (range 2 max) 2)) (

How to type hint

牧云@^-^@ 提交于 2019-12-21 12:17:24
问题 How would I type hint this to get rid of the remaining reflection calls? (def B (amap ^"[[D" A i ^"[[D" B (amap ^doubles (aget A (int i)) j ^doubles row (* 2 (aget row (int j)))))) There's two reflection calls left, but I don't know how to get rid of them. 回答1: You don't show your complete code or the reflection warnings, but if they are what I think they are, you'll need to: hint A: (def ^"[[D" A ...) wherever you define it cast the return value of the innermost expression to double: (double

Clojure: How to replace an element in a nested list?

久未见 提交于 2019-12-21 11:29:28
问题 I have this deeply nested list (list of lists) and I want to replace a single arbitrary element in the list. How can I do this ? (The built-in replace might replace many occurrences while I need to replace only one element.) 回答1: As everyone else already said, using lists is really not a good idea if you need to do this kind of thing. Random access is what vectors are made for. assoc-in does this efficiently. With lists you can't get away from recursing down into the sublists and replacing

Reading unbuffered keyboard input in Clojure

試著忘記壹切 提交于 2019-12-21 10:51:13
问题 How does one read a single keystroke from the terminal (not Swing) in Clojure? I have tried a few things including various versions of the JLine library, but have not gotten it working (see example below). I will happily accept a working, Unix-only (Mac, Linux, ...) example. Ideally I'd like to know how to switch buffering off for both stdin and stdout. Here's something close: ;; project.clj dependencies: ;; [[org.clojure/clojure "1.4.0"] ;; [jline/jline "2.8"]]) (ns slosh.core (:import

Reading a zip file using java api from clojure

烈酒焚心 提交于 2019-12-21 09:33:21
问题 I'm trying to rewrite following snippet in clojure, but it all comes out ugly, maybe someone will suggest a more elegant solution? import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class ZipFileRdrExp { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("C:\\MyZip.zip"); ZipInputStream zis = new ZipInputStream(fis); ZipEntry ze; while

How do I modify a portion of a vector in Clojure?

眉间皱痕 提交于 2019-12-21 09:19:36
问题 I am wondering if I'm missing something basic involving vector manipulation. Let's say I have the following: (def xs [9 10 11 12 13]) (def idx [0 2]) (def values [1 3]) If I want to return the vector [1 10 3 12 13] in Matlab, I would write xs(idx) = values . In Clojure, is there a primitive way of achieving this? Right now I'm using the following function: (defn xinto [seq idx val] (apply assoc seq (interleave idx val))) Thanks. 回答1: It's a bit awkward because you've split up idx and values

Binary search in clojure (implementation / performance)

江枫思渺然 提交于 2019-12-21 09:18:34
问题 I wrote a binary search function as part of a larger program, but it seems to be slower than it should be and profiling shows a lot of calls to methods in clojure.lang.Numbers. My understanding is that Clojure can use primitives when it can determine that it can do so. The calls to the methods in clojure.lang.Numbers seems to indicate that it's not using primitives here. If I coerce the loop variables to ints, it properly complains that the recur arguments are not primitive. If i coerce those