Fork me on GitHub
#clojure
<
2019-04-12
>
borkdude08:04:26

Best way to get the butlast element of a seq?

(first (take-last 2 coll))
?

jaihindhreddy08:04:12

(first (rest (reverse coll)))
maybe? and
(first (rest (rseq coll)))
for a vector I think

borkdude08:04:58

reverse is not lazy, so I think that would be worse than take-last

👍 4
thumbnail09:04:25

(last (drop-last col)) ?. also linear though i’m not sure you can prevent that

borkdude09:04:31

last + drop last is going to walk through the collection twice I think

quadron09:04:13

am I doing something unidiomatic if I end up with a channel inside a channel?

danielneal10:04:13

I don't know about unidiomatic but I think I'd struggle to understand what was going on of I came across something like that

Jan K10:04:58

If you're doing request-reply over channels, putting a reply-to channel along with the request is a reasonable way to do it I think.

4
borkdude10:04:07

yeah, see above 🙂

🙈 4
slipset11:04:44

Got stung by sort-by today 😞

slipset11:04:24

It has two arities, one with, and one without a comparator:

slipset11:04:39

What stung me is that the one without the comparator, uses compare which returns -1, 0, 1 (as one would expect) as a comparator, whereas the arity with a user-supplied comparator expects a comparator which returns a boolean value.

slipset11:04:24

Hmm, reading through the implementation, this seems even stranger, that indicates that I’m wrong 😞

slipset11:04:23

Ok, so the problem isn’t there it’s that I had big numbers 😕

slipset11:04:45

user=> (sort - [23413461236412630 10000000000000000 1 0 -1])
(-1 0 1 23413461236412630 10000000000000000)
user=>

reborg11:04:18

I don’t understand the (sort -), that’s not proper comparator. Do you want (sort >) perhaps?

slipset11:04:02

I guess I was thinking in terms of the result of running a comparator on two things, and not on a thing that implemented Comparator in java.

slipset11:04:08

(as the doc clearly stated, my bad)

slipset11:04:39

a (untyped) comparator is a binary operation that returns -1, 0, 1

reborg11:04:48

Alright :) - just happens to work, a function of two numbers returning an int…

slipset11:04:41

I guess a comparator is a (whatever, whatever) -> negative, 0, positive

slipset11:04:38

I think that’s the standard way of doing sorting chars in C. Just subtract the chars from each other and you’re good.

slipset11:04:47

What’s funny is that - seems to work as a comparator for numbers that are integers, but not for longs…

reborg11:04:20

pretty much, Java already comes with that, Clojure builds on top. (sort #(compare %2 %1) [\a \b \c])

slipset11:04:37

user=> (sort - [1, 2 -3, 0, 5, 10, 100, -1000])
(-1000 -3 0 1 2 5 10 100)
user=>

reborg11:04:52

that’s likely integer truncation in action, you can see with (map (memfn intValue) [23413461236412630 10000000000000000 1 0 -1])

slipset12:04:12

sort seems to bottom out with this let here:

slipset12:04:20

(let [a (to-array coll)]
       (. java.util.Arrays (sort a comp))
       (with-meta (seq a) (meta coll)))

slipset12:04:00

And I guess I’m not able to parse (. java.util.Arrays (sort a comp))

slipset12:04:43

which sort is the sort in (. java.util.Arrays (sort a comp)) referring to?

slipset12:04:21

It should be referring to this:

slipset12:04:57

The plot thickens 🙂

slipset12:04:01

public abstract class AFunction extends AFn implements IObj, Comparator, Fn, Serializable

slipset12:04:58

and becomes even thicker:

slipset12:04:09

public int compare(Object o1, Object o2){
        Object o = invoke(o1, o2);

        if(o instanceof Boolean)
                {
                if(RT.booleanCast(o))
                        return -1;
                return RT.booleanCast(invoke(o2,o1))? 1 : 0;
                }

        Number n = (Number) o;
        return n.intValue();
}

slipset12:04:36

which probably means that sorting by a function which can return numbers which are bigger than Integers may or may not work. I guess.

borkdude13:04:53

any good clojure wrappers for lmdb-java?

borkdude13:04:15

Just tried the latter one

victorb13:04:40

anyone have any good recommendations for a library to help doing benchmarks of external binaries?

victorb13:04:44

(I don't want to benchmark things that are written in clojure/java, so basically will be using java.shell to run the programs but like to use clojure for setup/timing/teardown)

borkdude14:04:58

@victorbjelkholm429 you mean record the time of external processes?

victorb14:04:55

@borkdude was looking for a framework-like tool to help me structure the tests themselves, but thinking about it, wouldn't be hard to do myself. Conch looks very helpful for other things, so thanks for sharing that

vsvn15:04:29

anyone uses cloudfoundry?

jeroenvandijk17:04:46

For those interested in writing fast scripts in Clojure this might be interesting https://github.com/jeroenvandijk/clojure-scripting

anonfish17:04:59

Hey all, learn-awesome is an open-source, community effort towards collecting links to awesome learning resources categorized by topics, formats, & difficulty. The idea is to build humanity's universal skills graph. The page about Clojure could use some work though. Would appreciate people here sending pull requests 🙂 https://github.com/learn-awesome/learn-awesome/blob/master/programming-languages/clojure.md

dangercoder17:04:32

Anyone tried to use https://www.testcontainers.org/ for their integration tests but for Clojure?

dangercoder17:04:12

I only found a java library (which we can use i guess)

g20:04:34

not sure if this would be better in beginners, but it looks like my repl in cider is actively running some computation and blocking others. is there any way to tell what’s going on, or do i need to profile the jvm activity?

hiredman20:04:30

That is very unlikely

hiredman20:04:21

well, I guess it depends on how you read your statement

noisesmith20:04:30

you can get all the stacktraces in a vm without a profiler

hiredman20:04:42

I read it has "nrepl is blocking other threads from doing things"

g20:04:02

no, not quite

hiredman20:04:08

if you mean "I sent something to nrepl and it is still running, and I can send anything else to nrepl until it is done, what do I do?"

g20:04:09

just the repl is unresponsive, presumably because it’s doing something else

noisesmith20:04:46

with jstack, or even sending the process a signal, you can get the stack traces, and it should be pretty straightforward to see which one would be blocking io for the repl thread

hiredman20:04:57

a lot of it is going to highly specific to the nrepl client, and any middleware you are using

noisesmith20:04:09

but you can also get the impression it's blocked because of unclosed parens or strings

hiredman20:04:21

so you might be better off asking in #cider

noisesmith20:04:53

also, nrepl itself is a threaded implementation of a network protocol, you can disconnect or reconnect, no matter what the active IO is doing in one client

g20:04:40

gah, ok. it looks like my panacea of mashing c-c has reached its end-of-life. thanks

borkdude21:04:46

I’m running this code on Java source code from OpenJDK in a Java 11 environment:

$ clj
Clojure 1.10.0
user=> (import '[javax.tools ToolProvider DocumentationTool])
javax.tools.DocumentationTool
(let [dt (ToolProvider/getSystemDocumentationTool)]
    (.run dt nil nil nil
          (into-array ["-public"
                       "--source-path" "/Users/Borkdude/git/openjdk-jdk11/src/java.base/share/classes"
                       "java.lang"])))
/Users/Borkdude/git/openjdk-jdk11/src/java.base/share/classes/module-info.java:307: error: ';' expected
        
                     ^
/Users/Borkdude/git/openjdk-jdk11/src/java.base/share/classes/module-info.java:309: error: ';' expected
        
                     ^
2 errors
1
What’s wrong here?

hiredman21:04:17

module-info.java is not actually java source, if I recall

borkdude21:04:57

I would expect that java tools could cope with this?

borkdude21:04:09

or should I blacklist these files somehow

hiredman21:04:41

it may depend on if the tools version you are using understands modules

Alex Miller (Clojure team)21:04:54

module-info and package-info are intentionally invalid java source filenames

Alex Miller (Clojure team)21:04:22

such that their addition was guaranteed not to collide with existing source files

borkdude21:04:20

what should I do with this?

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by clojure.lang.InjectedInvoker/0x0000000800232040 (file:/Users/Borkdude/.m2/repository/org/clojure/clojure/1.10.0/clojure-1.10.0.jar) to method com.sun.tools.javac.file.JavacFileManager.getJavaFileObjects(java.io.File[])

borkdude21:04:07

the code looks like this inside a let:

dt (ToolProvider/getSystemDocumentationTool)
fm (.getStandardFileManager dt nil nil nil)
files (.getJavaFileObjects fm (into-array java.io.File [f]))

ghadi21:04:57

--illegal-access=debug will give you a trace

Alex Miller (Clojure team)21:04:01

making it non-reflective is prob your best bet

Alex Miller (Clojure team)21:04:28

(.getJavaFileObjects fm (into-array java.io.File [f])) is reflective

Alex Miller (Clojure team)21:04:41

type hint fm to the generic interface and you're probably good

Alex Miller (Clojure team)21:04:58

fm is probably typed as the concrete actual type

Alex Miller (Clojure team)21:04:20

(.getJavaFileObjects ^javax.tools.StandardJavaFileManager fm (into-array java.io.File [f]))

borkdude21:04:41

well, the warning from warn-on-reflection says:

Reflection warning, clj_kondo/Doclet.clj:62:15 - call to method getJavaFileObjects on javax.tools.StandardJavaFileManager can't be resolved (argument types: unknown)

Alex Miller (Clojure team)21:04:05

which has File[], Path[], and String[] overrides

Alex Miller (Clojure team)21:04:37

it does seem like it should know enough to work there but maybe the reflection stuff isn't matching componentype?

borkdude21:04:02

actually this works too:

files (.getJavaFileObjectsFromFiles fm [f])
without a warning

Alex Miller (Clojure team)21:04:56

I'd have to look more deeply to understand why

Alex Miller (Clojure team)21:04:15

could maybe hint ^"[Ljava.io.File;"

g22:04:46

could you guys help me decipher what is going on here? i got this from a thread dump, and i feel like i may have my jetty server configured incorrectly

"qtp247078080-91" #91 prio=5 os_prio=31 tid=0x00007fd0b02d1800 nid=0xea03 waiting for monitor entry [0x0000700012c87000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at ring.middleware.reload$reloader$fn__5707.invoke(reload.clj:10)
        - waiting to lock <0x0000000789c20aa0> (a java.util.concurrent.LinkedBlockingQueue)
        at ring.middleware.reload$wrap_reload$fn__5718.invoke(reload.clj:38)
        at common.server$wrap_exception_handling$fn__6898.invoke(server.clj:35)
        at ring.middleware.ratelimit$wrap_ratelimit$fn__5858.invoke(ratelimit.clj:50)
        at ring.middleware.json$wrap_json_response$fn__5126.invoke(json.clj:87)
        at ring.middleware.json$wrap_json_body$fn__5101.invoke(json.clj:44)
        at ring.middleware.content_type$wrap_content_type$fn__5078.invoke(content_type.clj:34)
        at ring.middleware.not_modified$wrap_not_modified$fn__5178.invoke(not_modified.clj:53)
        at ring.middleware.keyword_params$wrap_keyword_params$fn__5158.invoke(keyword_params.clj:36)
        at ring.middleware.params$wrap_params$fn__5196.invoke(params.clj:67)
        at ring.middleware.cors$handle_cors.invokeStatic(cors.clj:171)
        at ring.middleware.cors$handle_cors.invoke(cors.clj:160)
        at ring.middleware.cors$wrap_cors$fn__8210.invoke(cors.clj:185)
        at ring.middleware.session$wrap_session$fn__6847.invoke(session.clj:108)
        at ring.adapter.jetty$proxy_handler$fn__5043.invoke(jetty.clj:25)
        at ring.adapter.jetty.proxy$org.eclipse.jetty.server.handler.AbstractHandler$ff19274a.handle(Unknown Source)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
        at org.eclipse.jetty.server.Server.handle(Server.java:499)
        at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:258)
        at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
        at java.lang.Thread.run(Thread.java:748)

hipster coder19:04:57

I think it’s a deadlock too. After reviewing some videos on C concurrency… I can see the monitoring thread in the error. I am curious though… I thought Clojure tries to avoid using synchronization locking concurrency? Is the Clojure code using some Java locking code?

g22:04:46

i have an api being served with a series of middlewares, but i’m not sure where in this stack the problem actually is

g22:04:27

hmm. so perhaps i tried to reload code too many times?

g22:04:46

it seems like this middleware shouldn’t be in production. is that correct?

hiredman22:04:43

if that is actually a deadlock there should be another thread in the stacktrace in the critical section and not leaving it for some reason

g22:04:43

yeah there’s one other mention of it in the dump

hiredman22:04:04

but if you got a single dump that is just where the threads where at that time

hiredman22:04:23

it isn't an indication of anything

g22:04:46

i took it again and it showed the same thing; in addition the application isn’t responding anymore

g22:04:56

forgot to mention that

hiredman22:04:50

I would expect to see a lot more threads stuck then, I think the jetty default threadpool might be 50? every single one would stop in the same place