Fork me on GitHub
#clojure
<
2020-01-20
>
Nir Rubinstein11:01:19

2 seemingly simple questions: 1. Why is ({:a 1} :a) faster than (:a {:a 1})? 2. Per the documentation, the = operator dispatches to equals function of the concrete class. That being the case, why is (= "a" a) much slower than (.equals "a" a) (when a is (def a "a")

andy.fingerhut11:01:51

clojure.core/= only dispatches to equals of the concrete class as a last resort, after checking several other cases.

andy.fingerhut11:01:32

If you look at the source code for clojure.core/= you see it calls this Java method, assuming we are talking about Clojure/Java, not ClojureScript: https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Util.java#L24-L36

andy.fingerhut11:01:58

What method did you use to measure the relative speed of ({:a 1} :a) and (:a {:a 1}) ? Realize that microbenchmarks like that can be a bit challenging to get useful performance results from, given how short they are, JIT compilation on the JVM, various JVM options that can modify JIT behavior, etc. Criterium is a good library to use for as stable of results as people know how to achieve: https://github.com/hugoduncan/criterium

borkdude11:01:05

Funny, I did a benchmark yesterday where I saw the reverse for 1.

borkdude11:01:51

Also I discovered that (foo :x) doesn't work for defrecords..

andy.fingerhut11:01:31

Huh, that defrecord thing surprises me a bit, but I use defrecord so rarely that I probably would not have noticed.

borkdude11:01:29

Also one surprising thing: (:x foo :default) does not return :default when :x is nil in the defrecord foo

Nir Rubinstein11:01:14

Thanks a lot guys!!!

zilti13:01:35

Are pre- and postconditions not available for fn, only for defn? I tried to add a {:post [...]} block to an fn with a spec2 check, but it seems like it gets ignored completely

alexmiller14:01:41

prepost map goes after the arglist - is that where you have it?

jumpnbrownweasel14:01:16

Seems to work, this gives an assertion failure:

((fn [x] {:post [(= % 1)]} (inc x)) 2)

alexmiller14:01:24

user=> (def f (fn [i] {:post [(int? %)]} (if (odd? i) (str i) i)))
#'user/f
user=> (f 1)
Execution error (AssertionError) at user/f (REPL:1).
Assert failed: (int? %)
user=> (f 2)
2

alexmiller14:01:46

it's easy to forget the vector in :post too

zilti14:01:05

Hm I had the vector... Maybe a spec2 problem? I'll test some more, and if I run into something I'll report

Crispin14:01:58

Hi everyone. Anyone know how to stop clojure truncating tracebacks? I'm doing a lein test and getting a traceback ending in ... 102 more. How do I see those 102 more?

alexmiller14:01:27

the 102 more are repeated in one of the other nested exception stack traces (so you are likely actually seeing those elsewhere already)

Crispin14:01:26

Ah ok. Thanks for that. It was a nested exception, yes.

zilti14:01:47

Hmm what was that again when I get a "No matching ctor found for class" for a function?

manutter5115:01:39

That means Java was trying to construct a class, and it couldn’t find a constructor that takes the types of arguments you gave it.

zignd16:01:06

in prismatic/schema, is there any type annotation for functions?

zignd16:01:44

in the example above s/fn is an invalid option, but I'm looking for something that would do it, to describe that the function returns a function

borkdude17:01:06

@zignd Maybe you can use (s/pred ifn?)

zignd17:01:41

Thanks! Works perfectly!

jmckitrick18:01:27

I’ve been out of the loop a bit, so I’d like to ask if Luminus is still one of the best out-of-the-box template/framework options for Clojure.

Chase19:01:53

As an offshoot of that question about Luminus, for folks who are exploring the built in deps/cli tools over leiningen, will something like https://github.com/seancorfield/clj-new be able to create similar full stack, composable templates like Luminus?

Chase19:01:25

Hmmm, it actually looks like you can create the actual preexisting Luminus templates with this clj-new tool?

seancorfield19:01:10

Yes, clj-new can execute Leiningen templates (and Boot templates), as well as newer clj-template templates. If you find one that won't run, it's a bug and I'll (try to) fix it. Also, be aware that if clj-new runs a Leiningen template, it's not magically going to produce a deps.edn file -- it'll be a project.clj-based project!

Chase19:01:03

I see. So I would be looking to create a bespoke template with clj-new that mirrors the Luminus ones if I wanted the same structure?

seancorfield19:01:54

You could just run clj -A:new luminus chasote/webapp and then create an appropriate deps.edn based on whatever ends up in project.clj.

Chase19:01:33

Got it. Thanks for the clarification

seancorfield19:01:42

The :dependencies vector would become the main :deps entry and then each profile would become an alias -- but you'd have to figure out the equivalent to Leiningen plugins yourself.

Chase19:01:52

I'm not entirely sold on moving over yet but so far I like the deps tools and have been switching my editor integration to also take advantage of the built in socket (p)repl tools too (I'm using neovim/conjure). It's not quite ready yet (shadow-cljs doesn't work with the prepl) so I'm also trying to convert a shadow-cljs project to figwheel-main (which does work) which has been difficult.

Chase19:01:06

If I recall correctly, you've switched to socket based repl workflow too?

seancorfield19:01:55

Yeah, I use Atom (since 2015? When I switched away from Emacs) and ProtoREPL is no longer maintained so I switched to Chlorine which works with a Socket REPL.

seancorfield19:01:23

So now I can use Atom/Chlorine locally and also on any remote process started with a Socket REPL JVM option.

seancorfield19:01:41

No nREPL, no Compliment. Just out-of-the-box Clojure.

seancorfield19:01:21

I don't do any ClojureScript -- I suspect that's the area where deps.edn is weak in comparison to lein-based or shadow-based tooling.

Chase19:01:59

Yep, which is a strong interest of mine so I think I might be switching too early. Just as a thanks though, know that one motivation for gravitating towards certain tooling is if you are actually involved because I've seen how helpful you are on here.

seancorfield19:01:11

One day I may go back to ClojureScript -- we last tried it in 2014/2015 and it wasn't really ready for primetime back then. Tooling (and the core language itself) has improved dramatically since then.

jmckitrick20:01:50

But just to confirm, luminus is still a prime contender for framework/template for Clojure these days?

seancorfield00:01:51

@U061KMSM7 I think the answer to that is subjective. I've never used the Luminus template (except as a test case for clj-new 🙂 ). I don't particularly like some of the libraries it bundles in and I don't much care for the structure of the basic project it creates. And of course I don't use Leiningen these days. But I know there are folks who seem to like the somewhat "batteries-included" approach of Luminus.

jmckitrick01:01:39

Any other alternatives you would recommend?

jmckitrick01:01:17

Or particular libraries you would swap out?

seancorfield02:01:48

I prefer to start from scratch and only add in the libraries I actually need. These days that's going to be Component, Ring, Compojure. Almost certainly next.jdbc. Then it will depend on what I'm building.

vemv02:01:43

https://github.com/juxt/edge seems a fair contender for "deps-based web template"

seancorfield02:01:18

I'd say that's probably more opinionated than Luminus -- and depends on some far less widely used libraries (such as Juxt's fork of Aleph -- and Aleph itself is pretty niche) -- so it really depends on whether you buy into those libraries.

👍 4
seancorfield02:01:25

These templates are fine if you understand what you're getting yourself into. The Juxt folks are very supportive, so you'll get plenty of help if you buy into their stack but if you decide to use New Relic for production monitoring and metrics, I wouldn't be surprised if Aleph is poorly supported by it (http-kit is not well supported, for example, which was why we switched back to Jetty).

👍 4
seancorfield19:01:10

Yes, clj-new can execute Leiningen templates (and Boot templates), as well as newer clj-template templates. If you find one that won't run, it's a bug and I'll (try to) fix it. Also, be aware that if clj-new runs a Leiningen template, it's not magically going to produce a deps.edn file -- it'll be a project.clj-based project!

otwieracz23:01:49

I've got question regarding component and danielsz/system

otwieracz23:01:09

:handler        (-> (new-middleware {:middleware [not-found-handler
                                                        [fulcro-wrap-api :component]
                                                        fulcro-server/wrap-transit-params
                                                        fulcro-server/wrap-transit-response
                                                        [wrap-resource "public"]
                                                        wrap-content-type]})
                          (component/using [:datomic-db]))
   :wires-http-server (component/using (new-http-kit :port (config :dev :wires-http-port))
                                       {:handler :handler})]

otwieracz23:01:06

Unfortunately, component/using within wires-http-server does not work as expected. Look at this trace:

otwieracz23:01:12

(reset)
TRACE t51411: (system.components.http-kit/new-http-kit :port 3000)
TRACE t51411: => #system.components.http_kit.WebServer{:options {:port 3000}, :server nil, :handler nil}
Execution error (NullPointerException) at system.components.http_kit.WebServer/start (http_kit.clj:11).
null

otwieracz23:01:54

Why does handler is not updated?

hiredman23:01:33

what does new new-http-kit do?

otwieracz23:01:58

It's standard component from danielsz/system

hiredman23:01:23

oh, I bet reset is starting your system and one of your components has a bad stop behavior which causes it to not restart correctly

otwieracz23:01:52

Look - the :handler in TRACE is nil

otwieracz23:01:24

The problem is that :handler in new-http-kit call is not updated with component/using

lukasz23:01:47

According to the source you need to pass the handler to the factory function

lukasz23:01:54

not as a component dependency

otwieracz23:01:20

So how should I arrange dependencies in this case?

otwieracz23:01:01

Hmm, but new-web-server is marked as deprecated...

lukasz23:01:45

I ended up creating my own components for simple use cases like this, where you have a clear separation between the server and handler component