This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-04-18
Channels
- # announcements (1)
- # babashka (39)
- # babashka-sci-dev (59)
- # beginners (60)
- # calva (14)
- # circleci (1)
- # clj-kondo (16)
- # clj-on-windows (1)
- # clojure (95)
- # clojure-europe (5)
- # clojure-norway (2)
- # clojurescript (34)
- # conjure (2)
- # core-async (55)
- # datomic (4)
- # emacs (54)
- # holy-lambda (5)
- # hyperfiddle (2)
- # interop (4)
- # lsp (8)
- # malli (3)
- # nrepl (4)
- # off-topic (34)
- # polylith (5)
- # reitit (3)
- # releases (2)
- # shadow-cljs (85)
- # specter (2)
- # testing (8)
- # tools-deps (12)
Im running a polylith project, and in every component there is ‘resources’ directory, I want to use that folder to save files for tests and load them during tests. I was reading that using http://clojure.java.io/resources should do that but it returns nil. any ideas?
1. you'll get better suggestions if you tell us what you've tried so far
2. resources
is usually reserved for files you want to end up in the deployed artefact (jar or whatever). It's better to use a different directory for test-related files, e.g. test-resources
.
3. You need to have a :paths
/`:extra-paths` entry in your deps.edn, e.g.: https://clojure.org/guides/deps_and_cli#extra_paths
if you do the above and your :extra-paths
in your dev alias is, for instance, ["tests", "test-resources"]
, then (slurp (io/resource "lala.txt"))
will give you the contents of test-resources/lala.txt
.
But be careful with this. Make sure the names you give to the files are unique because if you have both tests/lala.txt
and test-resources/lala.txt
you won't be able to access both (I think the latter will override the former but I'm not sure, never tried it).
tnx for all the useful information 🙏 I do have “resources” under :paths on deps.edn, but maybe it did’t work because I tried to use it from the tests dir? I have a file under "resources/pdf/test.pdf"
- and I tried (slurp (io/resources "pdf/test.pdf"))
- but io/resources
resolved to nil.
I’ll try to make “test-resources” and update.
the function is called io/resource
, are you sure you don't have any weird typos that's valid Clojure but does the wrong thing?
Can you share your deps.edn?
{:paths ["src" "resources"]
:deps {}
:aliases {:test {:extra-paths ["test"]
:extra-deps {}}}}
(-> "pdf/test.pdf"
io/resource
file->bytes)
the alternative is ofcourse to do
(-> "./components/pdf/resources/pdf/test.pdf"
file->bytes)
It works for me. You might have to restart your repl.
> maybe it did’t work because I tried to use it from the tests dir
what do you mean? where you're calling io/resource
from shouldn't matter
What you should be getting:
it doesn’t matter form which component the file is located?
assuming it’s under /resources on some component
but it happens also when I’m not running from repl
(when I run clojure.test)
The pdf file is under resources-test. Is resources-test in your :paths
key in deps.edn?
i.e., using your previous example:
{:paths ["src" "resources"]
:deps {}
:aliases {:test {:extra-paths ["test" "resources-test"]
:extra-deps {}}}}
yea of course
I discovered though that when I run ‘poly test’ he works fine, only when I run the test from intellij he has a problem with it
Right, does it work with a barebones clojure REPL?
now it is - and the thing that was missing is a link from the main deps.edn on the project to resources -
:test
{:extra-paths [
"components/pdf/test"
"components/pdf/resources"]}
thanks a lot for the help and for the patient 🙏
Hello friends can you explain me the difference between reduce and clojure.core.reducers/reduce?
Essentially the latter may be able to happen in parallel
If invoked on a foldable source like vectors or maps, the reducers reduce will partition the data and reduce each partition, then combine
I think the docs mention that we need to use fold in order for the operation to happen in parallel
true! more coffee needed. the https://clojure.org/reference/reducers page lays out the differences https://clojure.org/reference/reducers
most importantly maps are treated differently by r/reduce - it will be a function of (f [acc k v] ) instead of (f [acc kv] )
So no real difference?
Only fold is useful to run concurrently on specific structures
well, the reducing function for maps is a real difference. if not that, what do you consider "real"?
I think the other difference, that it calls f with no args if an init is not provided is actually the biggest difference. Reducers were the precursor to transducers. That init function is important when chaining reducing transforms together I believe. Normally reduce instead takes the first arg as the first value for the accumulator and reduces it against the second arg. But r/reducer doesn't do that, thus properly decoupling the target from the source. The reducing function can thus control what collection it needs to collect into by having a zero arity. At least something like that. I think that explains more why there is another reduce in reducer, I don't think just having a reduce that is like reduce-kv for maps would have justified it otherwise.
But I might be getting it confused with transducers, and maybe here it was just people thought a better reduce would be one that calls (f) for the init and always does reduce-kv for maps.
thank you both for the answers
I read the https://clojure.org/reference/reducers but couldn't understand the difference :(
Hi, in Ring... sometimes can access the :headers {map}
values via keyword, but usually by "string"
like "Content-Type"
... can use keywords & strings interchangeably here?
which underlying server are you using? the spec shows that the headers should be downcased string keys • spec: https://github.com/ring-clojure/ring/blob/master/SPEC#L98 • for the jetty adapter: https://github.com/ring-clojure/ring/blob/master/ring-servlet/src/ring/util/servlet.clj#L13
that's what I thought. is ring jetty adapter
Thanks for your sleuthing! Still perplexed as to why a keyword would work o.o
I obtained a string: "\\030ethmac=00:19:b8:08:52:d9\\031wifimac=3c:e1:a1:df:dd:e6\\020sn=nt-2026-c192x"
I want to split this string on any Java escape literal, but I can't figure out how.
Answers/advice appreciated....
You simply provide a regular expression to split on. You can supply literals in it:
(clojure.string/split "\\030....\\020...x" #"\\")
(spacing for emphasis)
You will get an empty string as the first element in the result vector, based on my test in cljs
["" "030ethmac=00:19:b8:08:52:d9" "031wifimac=3c:e1:a1:df:dd:e6" "020sn=nt-2026-c192x"]
something like that will be the result
If you want to generalize any escape literal then we'll have to think more 😅
Thanks!
But yes, I want to split in ANY escape literal....
ATM I use a combination of splits, and also StringEscapeUtils/unescapeJava
and it is kind of an unholy mess
It's possible to edit docstring of some function on ClojureDocs, or do I have to suggest edit as Github issue? I'm talking about https://clojuredocs.org/clojure.core/if - I see only "Evaluates test."
Special forms are handled here in the clojuredocs source: https://github.com/zk/clojuredocs/blob/master/src/clj/clojuredocs/search/static.clj#L37 So you could open an issue on that repo or send a Pull Request.
is there a way to pprint datalog queries?
[:find
?title
:in
$
?year
:where
[$ ?e :movie/title ?title]
[$ ?e :movie/year ?year]]
They're just data, so you can use clojure.pprint/pprint
:
(clojure.pprint/pprint
'[:find ?title :in $ ?year :where [$ ?e :movie/title ?title] [$ ?e :movie/year ?year]])
[:find
?title
:in
$
?year
:where
[$ ?e :movie/title ?title]
[$ ?e :movie/year ?year]]
;;=> nil
linebreaks on keywords and vectors only?