Fork me on GitHub
#clojure
<
2020-10-19
>
kimim04:10:58

Hello, how to map java's try-with-resource statement in clojure? thanks.

seancorfield04:10:18

@kimi.im It depends on the type of resource, but with-open is used with anything "closable".

👍 3
bendy09:10:52

I'm trying to use juxt with two java function invocations (map (juxt .foo .bar) coll), however this doesn't work. Is there a nicer way then just fully expanding it to (map #([(.foo %) (.bar %)]) coll)?

delaguardo09:10:53

Have a look at memfn macro

p-himik09:10:07

Was about to mention it. :) And that expansion wouldn't work anyway.

delaguardo09:10:35

(map (juxt #(.foo %) #(.bar %)) coll) should also work

delaguardo09:10:52

clojure’s documentation suggesting to use that form instead of memfn macro

bendy09:10:29

@U04V4KLKC perfect! exactly what I was looking for, thanks!

emccue18:10:43

(juxt (memfn foo) (memfn bar))

svt09:10:54

Anyone knows how to convert com.rabbitmq.client.impl.LongStringHelper$ByteArrayLongString into clojure string?

delaguardo09:10:06

(String. your-byte-array-here) ?

svt10:10:48

:cause No matching ctor found for class java.lang.String

delaguardo10:10:00

and what is the type of argument?

svt10:10:36

ByteString Array

svt10:10:58

I’m getting it from queue message property header I’m using [com.novemberain/langohr "5.1.0"]

delaguardo10:10:00

Is it something from your codebase? I can’t find such java class

delaguardo10:10:08

try use type function it will return fullyqualified class name

svt10:10:31

Type: com.rabbitmq.client.impl.LongStringHelper$ByteArrayLongString

svt10:10:16

#object[com.rabbitmq.client.impl.LongStringHelper$ByteArrayLongString 0x33be57eb {"foo": "bar"}

svt10:10:31

This is the type I’m getting

delaguardo11:10:33

aha! try this (String. (.getBytes your-byte-array-long-string))

delaguardo11:10:17

or even better (str your-byte-array-long-string) because there is toString method implemented for that type of data

💯 9
simon20:10:05

How do you perceive inline tests in the attr-map compared to other test methods such as with-test or deftest? I see the advantage that tests are as near to a function as they can be, ie. writing tests is easy. On the other hand, a file can grow very quickly in length and the name and signature of a function is buried between tests and function code. What's the opinion of professional Clojure coders? Do you always use a specific way (in certain scenarios)?

hiredman20:10:51

I perceive them to be deprecated

hiredman20:10:11

tests in the attribute map are a thing that predates clojure.test as a library, and not something that belongs in modern code (anything written in the last 10 or so years)

lukasz20:10:36

I don't think I've ever seen them in the wild :thinking_face: (tests in the attribute map, that is)

hiredman20:10:49

attribute map is really the right thing to call it, attr-map is just want the docstring for defn calls it

hiredman20:10:03

it is really just metadata that gets put on the var

hiredman20:10:28

it is confusing because regrettably clojure.test also stores tests in metadata, but that semantics are different

hiredman20:10:21

there is clojure.core/test which is the primitive thing for running a test in the metadata system that predates clojure.test

hiredman20:10:04

is presumes that tests failures will throw an exception, which clojure.test doesn't, and it only can run tests on a single var

simon21:10:50

Thanks! I've seen it in another project but think I'll move to deftest then 😉