clojure

Can't call public method of non-public class: public (Google gcloud library)

≡放荡痞女 提交于 2019-12-19 04:04:40
问题 I am attempting to use the gcloud library. (ns firengine.state (:import [com.google.cloud AuthCredentials] [com.google.cloud.datastore DatastoreOptions])) (-> (DatastoreOptions/builder) (.projectId "<project_id>") (.authCredentials (AuthCredentials/createForJson (clojure.java.io/input-stream service-account-path))) .build) The above clojure code is translated from the following code snippet (ellided, click on "Run elsewhere"). import com.google.cloud.AuthCredentials; import com.google.cloud

Combining Clojure defprotocol and defrecord

依然范特西╮ 提交于 2019-12-19 03:10:24
问题 As far as I can tell, if I want to define a protocol ( defprotocol ) that will only be implemented by one defrecord , I still have to define the protocol first, then define the defrecord that implements it: (defprotocol AProtocol (a-method [this]) (b-method [this that])) (defrecord ARecord [a-field b-field] AProtocol (a-method [this] ...) (b-method [this that] ...)) Is there no way to combine the two, perhaps with an "anonymous" protocol? 回答1: Don't do this. A "private" or "anonymous"

Is it possible to do the Reader Monad from Haskell in Clojure?

别等时光非礼了梦想. 提交于 2019-12-19 02:32:40
问题 I've had a look at the algo.monads and fluokitten documentation. I've also read through monad blog entries by Jim Duey, Konrad Hinsen and Leonardo Borges. The only reference I can find to the Reader Monad in Clojure is this google groups discussion. My question is: Is it possible to do the Reader Monad from Haskell in Clojure? Could you provide an example? 回答1: Sure. A Reader is just a function that takes an environment and extracts some value from it. With Reader , m-result takes some value

Why does read-line not return after hitting ENTER (seems like a hang) using lein run, but works with lein repl?

社会主义新天地 提交于 2019-12-19 02:27:28
问题 The problem at hand is that when I run my program with lein run it gets to the (read-line) part and I can't get out of it, meaning: read-line never returns. Here is the relevant code: (def command (atom "")) (defn print-prompt [] (print "prompt> ") (flush) ) (defn ask-for-input [] (print-prompt) (let [x (str (read-line))] (println (str "User input: " x)) (reset! command x) ) ) I never see the "User input: " string on screen. The strange part is, if I run lein repl and call (ask-for-input)

Clojure - Why does execution hang when doing blocking insert into channel? (core.async)

邮差的信 提交于 2019-12-18 21:26:05
问题 Consider the following snippet: (let [chs (repeatedly 10 chan)] (doseq [c chs] (>!! c "hello")) (doseq [c chs] (println (<!! c)))) Executing this will hang forever. Why is that? If I do (go (>! c "hello")) instead, it works just fine. 回答1: To make an asynchronous put, use clojure.core.async/put! (let [chs (repeatedly 10 chan)] (doseq [c chs] (put! c "hello")) (doseq [c chs] (println (<!! c)))) This works in this example as <!! will always unblock because of all necessary puts happening

Cartesian product in clojure

孤街浪徒 提交于 2019-12-18 20:14:28
问题 I'm trying to implement a method that will take a list of lists and return a the cartesian product of these lists. Here's what I have so far: (defn cart ([] '()) ([l1] (map list l1)) ([l1 l2] (map (fn f[x] (map (fn g [y] (list x y)) l2)) l1) ) ) (defn cartesian-product [& lists] (reduce cart lists) ) ;test cases (println (cartesian-product '(a b) '(c d))) ; ((a c) (a d) (b c) (b d)) (println (cartesian-product ())) ;() (println (cartesian-product '(0 1))) ; ((0) (1)) (println (cartesian

mapcat breaking the lazyness

☆樱花仙子☆ 提交于 2019-12-18 20:04:23
问题 I have a function that produces lazy-sequences called a-function. If I run the code: (map a-function a-sequence-of-values) it returns a lazy sequence as expected. But when I run the code: (mapcat a-function a-sequence-of-values) it breaks the lazyness of my function. In fact it turns that code into (apply concat (map a-function a-sequence-of-values)) So it needs to realize all the values from the map before concatenating those values. What I need is a function that concatenates the result of

mapcat breaking the lazyness

蹲街弑〆低调 提交于 2019-12-18 20:04:13
问题 I have a function that produces lazy-sequences called a-function. If I run the code: (map a-function a-sequence-of-values) it returns a lazy sequence as expected. But when I run the code: (mapcat a-function a-sequence-of-values) it breaks the lazyness of my function. In fact it turns that code into (apply concat (map a-function a-sequence-of-values)) So it needs to realize all the values from the map before concatenating those values. What I need is a function that concatenates the result of

define a synonym for a Clojure macro

限于喜欢 提交于 2019-12-18 19:44:54
问题 So following on from Clojure macro to create a synonym for a function , I discovered that def can't be used to define a synonym for a macro. Below are examples I tried that Clojure doesn't allow. ;(def def-function defn) ;(def case cond) ;(def function fn) Is it possible to define synonyms/aliases for macros in Clojure? Would it require using defmacro? 回答1: You can use a macro: user=> (defmacro def-function [& args] `(defn ~@args)) #'user/def-function user=> (def-function g [] 2) #'user/g

Using clojure.contrib functions in slime REPL

旧街凉风 提交于 2019-12-18 19:01:02
问题 I want to use the functions in the clojure.contrib.trace namespace in slime at the REPL. How can I get slime to load them automatically? A related question, how can I add a specific namespace into a running repl? On the clojure.contrib API it describes usage like this: (ns my-namespace (:require clojure.contrib.trace)) But adding this to my code results in the file being unable to load with an "Unable to resolve symbol" error for any function from the trace namespace. I use leiningen 'lein