clojure

clojure filter nested map to return keys based on inner map values

大憨熊 提交于 2019-12-24 01:25:45
问题 For this nested map called "tables", (def tables {:tableA {:occupied false :party nil} :tableB {:occupied true :party nil} :tableC {:occupied false :party nil}}) how do I filter and get back the keys where :occupied = false ? correct result should be (:tableA :tableC) can I do this with "filter" HOF? Should I be using a list comprehension? 回答1: You could do it pretty easily with keep : (keep (fn [[k v]] (if-not (:occupied v) k)) tables) However, as you observed, using for is often a good

Why do core.async go blocks return a channel?

允我心安 提交于 2019-12-24 01:01:40
问题 I understand that 'go blocks' (whether go or go-loop or possibly other constructs) return a channel. However I have never understood this channel's purpose. I'd like to know how to use it. Perhaps I'm creating extra channels when I don't need to be. 回答1: I use the return channel of a go -block as a handle that I can pass to another function (not just a macro) which wants to synchronize with the completion of the go -block. Alternatively, I can preform blocking reads on the channel to

Can Clojure generate function by macro?

丶灬走出姿态 提交于 2019-12-24 00:56:45
问题 I'm trying to generator fn through macro.But I meet a strange issue. Code is Here. (defmacro test2 [lmk] (fn [lmk2] (println lmk lmk2))) ((test2 12) 13) ;;----->Error (defmacro test3 [] (fn [lmk] (println lmk))) ((test3) 12) ;;----->OK (defmacro test4 [] `(fn [lmk] (println lmk))) ((test4) 12) ;;----->Error I know defn can work well.I just want to know why I can't use defmacro here.And why test3 works well,however test2 fails. 回答1: In test2, prior to compilation, the test2 macro will be

Clojure apply that does not realize the first four elements of a lazy sequence?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 00:46:46
问题 It appears that apply forces the realization of four elements given a lazy sequence. (take 1 (apply concat (repeatedly #(do (println "called") (range 1 10))))) => "called" => "called" => "called" => "called" Is there a way to do an apply which does not behave this way? Thank You 回答1: Is there a way to do an apply which does not behave this way? I think the short answer is: not without reimplementing some of Clojure's basic functionality. apply 's implementation relies directly on Clojure's

Futures somehow slower then agents?

可紊 提交于 2019-12-24 00:46:16
问题 The following code does essentially just let you execute something like (function (range n)) in parallel. (experiment-with-agents 10000 10 #(filter prime? %)) This for example finds the prime numbers between 0 and 10000 with 10 agents. (experiment-with-futures 10000 10 #(filter prime? %)) Same just with futures. Now the problem is that the solution with futures doesn't run faster with more futures. Example: ; Futures (time (experiment-with-futures 10000 1 #(filter prime? %))) "Elapsed time:

How to require a namespace programmatically

余生长醉 提交于 2019-12-24 00:45:11
问题 I'm working on a Liberator project in Clojure. I've defined a series of routes which return JSON data computed by logic in some other namespace. I would like to be able to change the namespace that implements the logic programmatically so I can do something like this: JAVA_OPTS='-DgameLogicNamespace=foo.logic.mock' lein ring server-headless 8080 I am currently doing it like this: (ns foo.routes (:require [compojure.core :refer :all] [liberator.core :as lib :refer [defresource request-method

How do you write to a log file in Clojure, using core.async?

◇◆丶佛笑我妖孽 提交于 2019-12-24 00:40:08
问题 I want to use core.async as a logger that writes out to a file, so I created a test.txt file, stuck it in my resources folder and wrote this code: (use 'clojure.java.io) (use 'clojure.core.async) (def print-chan (chan)) (go (loop [] (when-let [v (<! print-chan)] (with-open [wrtr (writer "resources/test.txt" :append true)] (.write wrtr v)) (recur)))) (>!! print-chan 42) When I run this, however, I find that it will only replace what is in the file, and not append to it. Also, sometimes the

Compojure app not playing well with with-redefs

丶灬走出姿态 提交于 2019-12-24 00:28:45
问题 I'm writing a Compojure application and am using clj-webdriver to graphically test it. I'm trying to use with-redefs to mock out the function that pulls out data from persistence to just return canned values, but it's ignoring my function overwrite. I know with-redefs works in terms of vars, but it's still not working: project.clj relevant pieces: (defproject run-hub "0.1.0-SNAPSHOT" :main run-hub.handler/start-server) handler.clj: (ns run-hub.handler (:require [compojure.core :refer :all]

How to calculate cyclomatic complexity of a Clojure function?

ぃ、小莉子 提交于 2019-12-24 00:18:49
问题 What would be a reasonable way to calculate the cyclomatic complexity of a Clojure function? It's easy to count decision points based on functions like 'if' and 'cond', but it starts to get tricky with macros. Anyone has tried this for Clojure or maybe another functional language? 回答1: Macros are an abstraction and should not contributed to the CC calculation, any more than a function call would. That said, I don't think that CC is particularly interesting for Clojure. I would be more

Is it possible to get the Cursive Plugin installed with Android Studio?

吃可爱长大的小学妹 提交于 2019-12-24 00:01:08
问题 I am currently starting my first project that will use Clojure to write an Android App using Android Studio. My idea was to install the Cursive Plugin as this Plugin is available for IntelliJ Community Edition on which Android Studio is based. Unfortunately my (maybe naive?) approach to just go to Settings -> Plugins -> Browse repositories ... and search for "Cursive" did not show any hits. Is there any other way to install the Cursive Plugin with Android Studio? 回答1: You can download it