This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-12-24
Channels
- # adventofcode (6)
- # announcements (4)
- # aws (21)
- # babashka (36)
- # beginners (58)
- # calva (3)
- # cider (2)
- # clj-kondo (21)
- # clojars (3)
- # clojure (35)
- # clojure-dev (4)
- # clojure-europe (5)
- # clojure-nl (8)
- # clojure-uk (8)
- # clojuredesign-podcast (7)
- # clojurescript (10)
- # core-async (3)
- # data-science (2)
- # datomic (2)
- # defnpodcast (11)
- # duct (4)
- # figwheel-main (1)
- # fulcro (34)
- # graalvm (12)
- # graphql (4)
- # joker (14)
- # kaocha (1)
- # midje (1)
- # off-topic (5)
- # pedestal (1)
- # re-frame (3)
- # reagent (4)
- # reitit (1)
- # shadow-cljs (4)
- # testing (12)
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?
is there a simpler way to convert a namespaced keyword into a namespaced symbol, compared to that?
(symbol (namespace :a/b) (name :a/b))
oh .. I tried (symbol :a/b)
and it worked, never mind.
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)
nil
(cmd)some.other.ns=> (defmacro knows-the-ns [] `(quote ~(ns-name *ns*)))
#'some.other.ns/knows-the-ns
(cmd)some.other.ns=> (some.other.ns/knows-the-ns)
some.other.ns
(cmd)some.other.ns=> (in-ns 'user)
#object[clojure.lang.Namespace 0x3b77a04f "user"]
(cmd)user=> (some.other.ns/knows-the-ns)
user
(cmd)user=> (in-ns 'clojure.core)
#object[clojure.lang.Namespace 0x27305e6 "clojure.core"]
(cmd)clojure.core=> (some.other.ns/knows-the-ns)
clojure.core
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))
#'user/ns-known
(ins)user=> (user/ns-known)
user
(cmd)user=> (in-ns 'clojure.core)
#object[clojure.lang.Namespace 0x27305e6 "clojure.core"]
(cmd)clojure.core=> (user/ns-known)
user
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. https://clojuredocs.org/clojure.core/test 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 ...]
(do-somthing-with-sample-data))
inside of function body is applicable only to the case when you need that data to compute the function
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
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
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.
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.
@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 [""
""
""
""
"unix:///var/run/geth.ipc"]]
(let [channel (open url :route-fn (fn [_] client))]
(is (= [:send!-fn :close-fn] (keys channel)))
(try
(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: https://github.com/Hindol/json-rpc@US3519YR5 you could also stub Clojure functions using with-redefs
.
https://clojuredocs.org/clojure.core/with-redefs
This would be my preferred method of mocking where possible since you won’t need to create protocols specifically for testing.
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
[x]
(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.
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 😄
my solution looks like this:
(defn get-classifications
[item]
(map #(:classifier %) (:classifiers item)))
(defn item-with-classification
[classification]
(filter #(some #{classification} (get-classifications %)) items))
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
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.
Hi there! Can anyone help me out with clojurewerkz/buffy? How do you read from an input-stream into buffy buffer?
i'm trying to understand the :aliases
section in the deps.edn
file. I read the aliases section from https://clojure.org/reference/deps_and_cli 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.
https://github.com/StediInc/cdk-clj/blob/master/examples/basics/deps.edn
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.
ah okay, that is immensely helpful
Meaning, creating one does not affect the other.
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"
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
.
oh, thanks for pointing that out 🙂
ah okay, I think im getting it
thank you. this has been really helpful