Fork me on GitHub

I guess that js->clj` only available in Clojurescript and I have to use a lib for parsing JSON.


There is no function js->clj built into Clojure that runs on the JVM, if that is what you are asking, and yes, there are libraries for reading JSON readily available, including the data.json contrib library and Cheshire


That is what you get when you try to evaluate a symbol that has no definition. Were you expecting something else?

Vincent Cantin08:12:59

is there a simpler way to convert a namespaced keyword into a namespaced symbol, compared to that? (symbol (namespace :a/b) (name :a/b))

Vincent Cantin08:12:00

oh .. I tried (symbol :a/b) and it worked, never mind.

Vincent Cantin10:12:32

What is the correct way to get the namespace where a macro is used? I tried (ns-name *ns*) but it does not resolve to the right namespace when I run it in the tests (note: I used the test runner inside IntelliJ).


*ns* is the namespace that's currently active, and people's intuitions of what that should be is often (usually?) wrong.


do you want the ns of the form that invoked the macro?


here this macro accurately knows the namespace it is used in:

(cmd)user=> (ns some.other.ns)
(cmd)some.other.ns=> (defmacro knows-the-ns [] `(quote ~(ns-name *ns*)))
(cmd)some.other.ns=> (some.other.ns/knows-the-ns)
(cmd)some.other.ns=> (in-ns 'user)
#object[clojure.lang.Namespace 0x3b77a04f "user"]
(cmd)user=> (some.other.ns/knows-the-ns)
(cmd)user=> (in-ns 'clojure.core)
#object[clojure.lang.Namespace 0x27305e6 "clojure.core"]
(cmd)clojure.core=> (some.other.ns/knows-the-ns)


it still knows where it was first expanded if used from another ns

(ins)clojure.core=> (in-ns 'user)
#object[clojure.lang.Namespace 0x3b77a04f "user"]
(ins)user=> (defn ns-known [] (some.other.ns/knows-the-ns))
(ins)user=> (user/ns-known)
(cmd)user=> (in-ns 'clojure.core)
#object[clojure.lang.Namespace 0x27305e6 "clojure.core"]
(cmd)clojure.core=> (user/ns-known)

👍 4
Vincent Cantin13:12:00

I did the same as you did and it works in my program but not in the tests. I suspect that it’s the test environment of IntelliJ which is launching/evaluating the tests without switching to the namespace of the file in which they are defined.


weird... - could be a #cursive bug


Hi all, I want to have some example data (e.g. maps containing strings) for my functions. Is it ok to provide these via the meta data feature or would this be a abuse?


this is completely ok if you want to add tests to your function. but it is better to keep them separately or inside of function body if you need those samples as a part of function’s logic


thx for the answer, what do you mean by > inside of function body how, as a comment?


(let [sample-data ...]


inside of function body is applicable only to the case when you need that data to compute the function


ok, I understand, thx again!


hi, so I have a function getdata where I make vectors to combine some data: (map #(vec [%1 %2 %3 %4]) (getartist desc) (getsong desc) shortcode acc_captions) I want to output this to a csv file like so:

(with-open [writer (io/writer "out-file.csv")]                                              │·····
  (csv/write-csv writer                                                                     │·····
                 [["Titel" "Interpret" "Link" "Beschreibung"]                               │·····
                  getdata]                                                          │·····
                 :separator \;))  
but it doesn't do what it's supposed to do, I guess because I need to somehow untangle the vector of vectors first? (first getdata) works, but I'm not sure how to do if for all the vectors, can anyone help?


(cons ["Titel" "Interpret" "Link" "Beschreibung"] getdata) something like this? write-csv expects data to be in the form of vector of vectors


that did the trick! thanks very much


Hi all, just confirming ring-mock is still the best way to do TDD with ring ?


I'm a beginner, but do you actually need mocks in FP?

Enyert Vinas13:12:17

I think you need mocks in any paradigm. Because is the way to test the difference responses from an external source. At the end you want to isolate your unit tests from side effects


That's true


You can also pass in instances or functions that make the external calls to your functions and not use a lib at all. I haven't used a mocking lib in any language in 5ish years. It has the added perk of making repl dev easier too.


thanks, are you able to point to any examples of the above for ring?


Also look at shrubbery. Makes it really easy to create mock implementations from a protocol. You will still need to structure your code so that the external dependency is passed in as a parameter, like @UR2C8GK1R said.


What exactly about ring? Do you want to mock HTTP calls?


@UJRDALZA5 Nope, just > pass in instances or functions that make the external calls to your functions and not use a lib at all for TDD in ring


I did this recently. I created a protocol Client wrapping the external dependency.

(defprotocol Client
  "A JSON-RPC client."
  (open [this url] "Opens a connection to the given URL")
  (send! [this conneciton message] "Sends a JSON-RPC request to the open connection")
  (close [this conneciton] "Closes the connection"))
Here is the test.
(deftest send!-test
  (let [id       (uuid)
        response (json/write-str {:jsonrpc "2.0"
                                  :result  "0x0"
                                  :id      id})
        client   (mock client/Client {:send! response
                                      :close nil})]
    (testing "open can open channels for all supported schemes"
      (doseq [url [""
        (let [channel (open url :route-fn (fn [_] client))]
          (is (= [:send!-fn :close-fn] (keys channel)))
            (is (= {:result "0x0"
                    :id     id}
                   (send! channel "eth_blockNumber" ["latest"] :id id)))
            (finally (close channel))))))))
The mocked client is passed via :route-fn. Complete code in this repo:


@US3519YR5 you could also stub Clojure functions using with-redefs. This would be my preferred method of mocking where possible since you won’t need to create protocols specifically for testing.

Jordan Robinson15:12:17

Hi all, I have a small question that has had me stumped for a little while. I need to be able to filter this vector on the nested field "classifier"

[{:name "foo",
  :type "foo",
  :path "foo/foo",
  :classification [{:other_type "x", :classifier "A"}]}
  {:name "bar",
  :type "bar",
  :path "foo/bar",
  :classification [{:other_type "y", :classifier "B"}]}]
I can get the value with (:classifier (first (:classification (first coll)))) where coll is the data, but that of course assumes that it would be the first value. I've tried quite a few things around filter and get-in but because the value is nested in a vector, then a map, then a vector, then a map, this seems quite difficult Is there something obvious that I'm missing?


I think the step you’re looking for is that you want two iterators - one to go over the the initial vector, and one to go over the :classification values for each map. Maybe something like:

(defn foo
  (filter #(some (comp (partial = "A") :classifier) (:classification %)) x))


This keeps all of the objects in the initial vector with a classifier A, you can change the #(some ...) to check if the value under :classification is what you want to keep. Using an iterator instead of first allows you to check more than just one item in that vector.

Jordan Robinson18:12:15

hey, thanks very much! That helps a lot - I found my own solution about 30 mins ago and was about to post an update, but I think your solution is much more elegant so I'll probably use that 😄

Jordan Robinson18:12:11

my solution looks like this:

(defn get-classifications 
  (map #(:classifier %) (:classifiers item)))

(defn item-with-classification 
  (filter #(some #{classification} (get-classifications %)) items))

Jordan Robinson18:12:06

I definitely think yours is better though, thanks again! 👍


(for [x m [k v] x :where (= k : classification) y v :where (= "A" (:classifier y))] x) or something like it


Get rid of the map destructuring that is more of a generic tree traversal thing


Hello, small question regarding destructuring in a let. Is it possible to do in one pass something like [a _ b] ["foo" "bar" "baz"] and having "a" as a keyword ? I tried [#(keyword %) _ b] but it's not working.


destructuring is about "pulling out" not transforming values


Thanks. I just wanted to be sure that I was not missing a feature ;-)


Hi there! Can anyone help me out with clojurewerkz/buffy? How do you read from an input-stream into buffy buffer?

Renzo Tomlinson22:12:31

i'm trying to understand the :aliases section in the deps.edn file. I read the aliases section from but don't really understand the context in which these aliases working. I sorta understand the concept for defining aliases, allows me to bind a symbol to a namespace outside of mine to make it easier to reference, but I don't see if or how that translates to defining aliases in the deps.edn The particular deps.edn i'm struggling with is from the cdk-clj example.


A quick comment: An alias inside of Clojure, and :aliases inside of deps.edn, aren't really directly related to each other, except that they both have alias in their names.

Renzo Tomlinson22:12:17

ah okay, that is immensely helpful


Meaning, creating one does not affect the other.

Renzo Tomlinson22:12:29

should I think of deps.edn as "outside" of clojure?


It is outside of the Clojure program, yes.


An alias in deps.edn is a way of saying "here is one, or several, dependencies, or JVM command line options to use, etc. that I sometimes want to use when starting Clojure, but probably not all of the time. Give that collection a short name, so I can use that short name on the command line to include those"

Renzo Tomlinson22:12:30

i think i sorta get it. there's a json file (cdk.json) that I have defined which just has

{"app":"clojure -A:dev -i cdk/stedi/lambda_creator/cdk.clj}
cdk is using this command to run clojure with alias dev


Just being pedantic, the alias is :dev which is specified directly after -A. The : is not a separator, it is part of the alias, just like in deps.edn.

Renzo Tomlinson21:12:32

oh, thanks for pointing that out 🙂

Renzo Tomlinson23:12:39

ah okay, I think im getting it

Renzo Tomlinson23:12:46

thank you. this has been really helpful