clojure

How do I use Clojure in Android Studio using Graclj?

戏子无情 提交于 2019-12-23 18:49:00
问题 My ultimate goal is to be able to write Clojure apps for Android, using Android Studio and Cursive. I started with leiningen but found out that it is a build system that is independent of what Andoid Studio uses ie Gradle. So I tried leiningen with Intellij, but couldn't get Android deploys to work except from the command line. Since I wanted to integrate with Android Studio, I decided to try Graclj: https://github.com/graclj/graclj which is a Gradle plugin for Clojure. I can get the Graclj

Installation of Leiningen 2.X in Mac OS X

与世无争的帅哥 提交于 2019-12-23 18:27:31
问题 I'm using leiningen ver 1.X, and I'm trying to update it to ver 2.X. As is written in this site, I downloaded the script as "lein2", but when I tried to run the "lein2" command, I get this error message. Could not find artifact lein-newnew:lein-newnew:pom:0.3.4 in central (http://repo1.maven.org/maven2) Could not transfer artifact lein-newnew:lein-newnew:pom:0.3.4 from/to clojars (https://clojars.org/repo/): Specified destination directory cannot be created: /Users/smcho/.m2/repository/lein

How can I tell if a ref is being updated by a dosync in Clojure?

孤者浪人 提交于 2019-12-23 17:47:08
问题 I have some updates which need to be made to a ref, but I want to choose a time to perform the updates when the ref is not in heavy use. Is there a way I can programatically tell when the ref is in a transaction? 回答1: you could add-watch that would update an access-time each time your ref is written (you mentions it was write mostly). This would give you a huristic for busy. (def my-ref (ref {})) (def my-ref-atime (atom 0)) (add-watch my-ref (fn [key ref old new] (swap! my-ref-atime current

sorted-map returns nil value for existing key

﹥>﹥吖頭↗ 提交于 2019-12-23 17:37:06
问题 I try to get value by key from sorted map with comparator by value it returns nil . (def tmap {1 {:v 1} 2 {:v 2} 3 {:v 3}}) (def tmap-sorted (apply sorted-map-by #(let [val-comp (- (compare (get-in tmap [%1 :v]) (get-in tmap [%2 :v])))] (if (= val-comp 0) 1 val-comp)) (flatten (vec tmap)))) ; => {3 {:v 3} 2 {:v 2} 1 {:v 1}} (get tmap-sorted 3) ;=> nil Expected: {:v 3} Actual: nil 回答1: You are creating a custom Comparator with compare that is being used in PersistentTreeMap (the type of tmap

Clojure: iterate over map of sets

為{幸葍}努か 提交于 2019-12-23 17:21:35
问题 This is pretty much a follow-up to my last question (Clojure idiomatic way to update multiple values of map), but not quite the same. (keep in mind that I'm fairly new to Clojure and functional languages alike) suppose I have the following data structure, defined as a map of sets: (def m1 {:1 #{2} :2 #{1 3} :3 #{1}}) and a map of maps as such: (def m2 {:1 {:1 0 :2 12 :3 23} :2 {:1 23 :2 0 :3 4} :3 {:1 2 :2 4 :3 0}}) What I want to do is update the registries of m2 that have a correspondence

With clojure read/read-string function, how do i read in a .clj file to a list of objects

我的未来我决定 提交于 2019-12-23 16:43:48
问题 As titled, If I do (read-string (slurp "somefile")) This will only give me the first object in the file, meaning if "somefile" is as below: (a obj) (b obj) Then I only get (a obj) as the result. How do i get a list of all objects, like this? ((a obj) (b obj)) Thanks. 回答1: (defn read-all [input] (let [eof (Object.)] (take-while #(not= % eof) (repeatedly #(read input false eof))))) 回答2: I usually wrap stuff in a list, (read-string (str \( (slurp "somefile") \))) 来源: https://stackoverflow.com

Clojure unit-testing. How do I test if a function throws an exception?

我的未来我决定 提交于 2019-12-23 16:27:25
问题 I see there's a way to test if a function throws an exception of class C. But is there a way to test whether a function throws any exception. Or to assert that it should NOT throw an exception? 回答1: For tests that don't expect exceptions, write your test as normal. Any exceptions thrown will fail the test. For tests that could throw any exception, then use Exception or Throwable (Exception's superclass). For example: (deftest mytest (is (thrown? Exception (/ 1 0)))) (/ 1 0) will throw a java

Unloading Clojure vars from JVM thread-local space

五迷三道 提交于 2019-12-23 16:16:15
问题 I'm writing a plugin for BaseX in Clojure, built via "lein uberjar" with the Clojure interpreter included. For the most part, this works well. However -- when running via the BaseX HTTP instance, evaluation takes place inside Jetty's thread pool rather than having threads thrown away after a client disconnects. As loading the plugin loads Clojure's classes through a custom classloader, and throwing away the (AOT-compiled) object instance which acts as the plugin's entry point does not discard

Clojure Maps, order of key value creation

邮差的信 提交于 2019-12-23 16:09:57
问题 In a clojure profiling library, there is a an atom storing some profiling information and it is modified by a number of functions. In the comments, they assert that the summary statistics (one of the functions) is called last for efficiency purposes, but I could not see how this was enforced in the actual code. I then noticed that one manner in which this could enforced is the ordering in the construction of a map, so I recreated a simpler example to demonstrate. (defn t [] (let [a (atom {})]

Clojure snake skips positions

荒凉一梦 提交于 2019-12-23 15:44:13
问题 I'm a Clojure beginner and I've been playing around with a snake game code presented here https://github.com/stuarthalloway/programming-clojure/blob/master/src/examples/snake.clj. When I run the game the snake seems to skip positions while moving, it doesn't go square by square as I expected. But when I turn the snake and hold down the direction key it starts to behave normally and moves square by square, as soon as I release the key it starts skipping again. I guess this has something to do