Fork me on GitHub
#beginners
<
2022-04-18
>
Daniel Shriki07:04:00

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?

pavlosmelissinos11:04:17

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

pavlosmelissinos11:04:21

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.

pavlosmelissinos11:04:39

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).

👍 1
Daniel Shriki11:04:04

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.

pavlosmelissinos12:04:42

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?

pavlosmelissinos12:04:42

Can you share your deps.edn?

Daniel Shriki12:04:36

{:paths ["src" "resources"]
 :deps {}
 :aliases {:test {:extra-paths ["test"]
                  :extra-deps {}}}}

Daniel Shriki12:04:58

(-> "pdf/test.pdf"
    io/resource
    file->bytes)

Daniel Shriki12:04:54

the alternative is ofcourse to do

(-> "./components/pdf/resources/pdf/test.pdf"
    file->bytes)

pavlosmelissinos12:04:54

It works for me. You might have to restart your repl.

pavlosmelissinos12:04:25

> 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

pavlosmelissinos12:04:02

What you should be getting:

Daniel Shriki12:04:48

it doesn’t matter form which component the file is located?

Daniel Shriki12:04:06

assuming it’s under /resources on some component

Daniel Shriki12:04:03

but it happens also when I’m not running from repl

Daniel Shriki12:04:59

(when I run clojure.test)

pavlosmelissinos13:04:41

The pdf file is under resources-test. Is resources-test in your :paths key in deps.edn?

pavlosmelissinos13:04:55

i.e., using your previous example:

{:paths ["src" "resources"]
 :deps {}
 :aliases {:test {:extra-paths ["test" "resources-test"]
                  :extra-deps {}}}}

Daniel Shriki13:04:03

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

pavlosmelissinos13:04:48

Right, does it work with a barebones clojure REPL?

Daniel Shriki13:04:57

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"]}

💪 1
Daniel Shriki13:04:59

thanks a lot for the help and for the patient 🙏

Serafeim Papastefanos14:04:36

Hello friends can you explain me the difference between reduce and clojure.core.reducers/reduce?

Alex Miller (Clojure team)14:04:07

Essentially the latter may be able to happen in parallel

Alex Miller (Clojure team)14:04:29

If invoked on a foldable source like vectors or maps, the reducers reduce will partition the data and reduce each partition, then combine

Serafeim Papastefanos15:04:51

I think the docs mention that we need to use fold in order for the operation to happen in parallel

Alex Miller (Clojure team)16:04:20

most importantly maps are treated differently by r/reduce - it will be a function of (f [acc k v] ) instead of (f [acc kv] )

Serafeim Papastefanos17:04:24

So no real difference?

Serafeim Papastefanos17:04:06

Only fold is useful to run concurrently on specific structures

Alex Miller (Clojure team)17:04:35

well, the reducing function for maps is a real difference. if not that, what do you consider "real"?

didibus01:04:44

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.

didibus01:04:17

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.

Serafeim Papastefanos04:04:40

thank you both for the answers

Serafeim Papastefanos14:04:49

I read the https://clojure.org/reference/reducers but couldn't understand the difference :(

sova-soars-the-sora16:04:46

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?

dpsutton16:04:10

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

sova-soars-the-sora16:04:20

that's what I thought. is ring jetty adapter

sova-soars-the-sora16:04:06

Thanks for your sleuthing! Still perplexed as to why a keyword would work o.o

dcj17:04:33

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....

sova-soars-the-sora18:04:40

You simply provide a regular expression to split on. You can supply literals in it: (clojure.string/split "\\030....\\020...x" #"\\") (spacing for emphasis)

sova-soars-the-sora18:04:55

You will get an empty string as the first element in the result vector, based on my test in cljs

sova-soars-the-sora18:04:22

["" "030ethmac=00:19:b8:08:52:d9" "031wifimac=3c:e1:a1:df:dd:e6" "020sn=nt-2026-c192x"]

sova-soars-the-sora18:04:37

something like that will be the result

sova-soars-the-sora18:04:57

If you want to generalize any escape literal then we'll have to think more 😅

dcj18:04:39

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

Martin Půda19:04:43

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."

seancorfield19:04:05

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.

👍 1
vlad_poh20:04:27

is there a way to pprint datalog queries?

[:find
 ?title
 :in
 $
 ?year
 :where
 [$ ?e :movie/title ?title]
 [$ ?e :movie/year ?year]]

flowthing07:04:21

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

Nundrum13:04:38

I'm guessing the point is: that's not very "pretty"?

flowthing14:04:47

Oh, yeah, I probably misunderstood. 🙂

sova-soars-the-sora15:04:21

linebreaks on keywords and vectors only?

sova-soars-the-sora15:04:36

left as an exercise to the reader 😏

😅 1