Fork me on GitHub
#beginners
<
2019-07-02
>
dmaiocchi10:07:07

Reading on the web for clojure polymorphism, there is a statement which says use multimetods when you really need them otherwise you just protocols

dmaiocchi10:07:06

Do you agree on this in a general manner? Or what is your experience with this, just for curiosity sake šŸ¶

tavistock11:07:13

multimethods are the devil, they can take your nice functions and smear it across several namespaces and change them on import, but sometimes thats what you want (they are also pretty hard to manipulate in the repl)

tavistock11:07:24

in general you want to use the most specific/least general abstraction at hand imo, multimethods are extremely open and protocols are contained

ghadi12:07:23

protocols are just as open as multimethods

dmaiocchi12:07:44

As much I read, multimetods are less performant then protocols. And they dispatch on a multiples values rather then protocols where you have your type dispatch

ghadi12:07:11

that's true, they are less performant than protocols if you're only measuring method call dispatch and not the work done by the body of the multimethod

clj 4
Alex Miller (Clojure team)12:07:49

Donā€™t fear multimethod perf - the overhead might be 70 ns vs 15 ns for protocol calls. Yes, thatā€™s more, but probably dwarfed by the parts of your program doing actual work, unless itā€™s in a very hot loop.

Alex Miller (Clojure team)12:07:37

There used to be some much bigger perf issues with multimethods. Those were fixed long ago but the fud lingers.

clj 8
Alex Miller (Clojure team)12:07:24

Both are useful. If you are fundamentally doing type based dispatch, then use protocols. If not, use multimethods.

clj 4
Alex Miller (Clojure team)12:07:07

This is an old repo with some timings (needs an update) https://github.com/puredanger/poly-timing

Alex Miller (Clojure team)13:07:17

updated those timings. kind of shocking how much better more recent jdk is on a few of those.

clj 4
jumar13:07:06

could it be that your computer is also significantly faster? šŸ™‚

Alex Miller (Clojure team)13:07:53

well I changed 3 things at the same time so it's hard to tell :) but I suspect it's a combination.

Alex Miller (Clojure team)13:07:53

new computer is possibly actually slower in core speed, certainly not much faster. but the places where the general shape of the answer differs are I suspect java

šŸ‘ 8
rschmukler16:07:19

Hey all - perhaps a silly question here... I'm trying to use spec-tools and it seems like the conditional reader macro is being ignored when I import the CLJC file. Reference of the line: https://github.com/metosin/spec-tools/blob/master/src/spec_tools/impl.cljc#L3 Error:

user=> (require '[spec-tools.core])
Syntax error (FileNotFoundException) compiling at (spec_tools/impl.cljc:1:1).
Could not locate cljs/analyzer/api__init.class, cljs/analyzer/api.clj or cljs/analyzer/api.cljc on classpath.

rschmukler16:07:36

I'm invoking it from Java, using tools.deps (updated add-libs branch as of 3 days ago) - am I missing something obvious here?

Alex Miller (Clojure team)16:07:26

That probably wonā€™t work with add-libs

rschmukler16:07:07

Even if I'm not dynamically loading it? ie. a new clojure process?

Alex Miller (Clojure team)16:07:18

Sorry, I misread above, thought you said reader literals

Alex Miller (Clojure team)16:07:41

I assume you loaded it as a maven artifact?

Alex Miller (Clojure team)16:07:20

add-libs will try to add a lib and itā€™s transitive deps in a way that meshes with the current full deps you have

hiredman16:07:41

are you sure the version of spec-tools you are using matches the code you are looking at on master?

Alex Miller (Clojure team)16:07:09

Depending what those are, it may have used an older version of a dep that is incompatible with spec-tools

hiredman16:07:55

if the dependency is on version 0.1.1 or later

Alex Miller (Clojure team)16:07:51

Can you try adding it to your deps and restarting your repl to remove the add-lib variable?

Alex Miller (Clojure team)16:07:50

And what do you mean by ā€œloading it from Javaā€?

rschmukler16:07:56

Hey! Sorry all. Of course I get a call just as I ask...

rschmukler16:07:24

So, clojure -Stree reports spec-tools as 0.10.0 - so I think we are good there.

Jamie Rumbelow16:07:08

Hi all. Iā€™m getting ready to make the case for Clojure for a new work project (to a team who still use select! in ruby). One thing Iā€™m concerned about is the additional deployment and maintenance overhead, particularly since Iā€™ve not provisioned a server to run the JVM before (I come from a LAMP background). Whatā€™s the state of the art in deploying clojure apps right now? Are there standard, well-maintained docker images you can point an nginx instance at? And has anybody here worked with applications spread across JVM and non-JVM tech?

rschmukler16:07:09

Loading it from java, I just mean that it's a Java process (so I expect cljs reader conditional to be ignored)

Chris Swanson16:07:56

@jamie962 I don't think you need to worry too much. you can deploy clj apps to heroku if you expect scale, or if you are just running internally, all you need is a JRE installed

rschmukler16:07:56

Got it working! Ended up deleting .cpcache... Not sure why that fixed it, but šŸ¤·

Chris Swanson16:07:38

personally for deploying clj apps internally, i'd use a docker image with java installed and just build and copy the jar file in

Jamie Rumbelow16:07:47

@chrisjswanson thanks - good to know. we deploy to a kubernetes cluster anyway so the docker angle sounds like a good bet.

Chris Swanson16:07:01

well the above may be your best friend then šŸ™‚

Jamie Rumbelow16:07:43

so i can just copy an uberjar in to that container and then run it with java -jar?

Jamie Rumbelow16:07:54

thatā€™s easier than I expected

Chris Swanson16:07:29

since you use k8s you have no worry of watching the process lifecycle, it's already handled

Chris Swanson16:07:37

just java -jar and be free šŸ™‚

Jamie Rumbelow16:07:48

super! thank you!

Jamie Rumbelow16:07:14

if I did need to worry about process lifecycle, what sort of concerns would there be?

Jamie Rumbelow16:07:23

does the jvm quit after it hits memory limits?

Chris Swanson16:07:58

well if you were just running in a vm , for example, then if your app crashed you'd need a nanny script to detect that and re-run it

Jamie Rumbelow16:07:28

but thatā€™s not a jvm/clj specific problem

Chris Swanson16:07:29

but k8s will do it for you, just launch a new instance of the image

Chris Swanson16:07:49

java will crash if you go over your heap size

Chris Swanson16:07:00

so if anticipate high memory usage,

Jamie Rumbelow16:07:04

Iā€™m pretty au fait with most of this stuff, itā€™s where we sidle into jvm territory that I begin to lose confidence

ghadi16:07:36

If you're using an uberjar to deploy, I suggest the adoptopenjdk images

šŸ‘ 4
ghadi16:07:57

If you need lein at runtime that's a different deal...

ghadi16:07:24

We use lein to build an uberjar, then run it in a standard Java container with java -jar

Chris Swanson16:07:21

clojure image maybe more intended for dev, and then just deploy with adoptopenjdk image. same pattern applies

Chris Swanson16:07:56

you don't have to worry too much about JVM , I've found the metrics it exposes to actually be quite helpful. do look into key jvm metrics to track though, they can let you know if your app leaks memory, etc

dmaiocchi20:07:05

Not coming from jvm world. What is the meaning for snapshot at the end of the release numbers? Can be removed?

dmaiocchi20:07:48

Is this applicable for clojure too?

hiredman20:07:31

it generally means "code that will eventually become the given version"

hiredman20:07:38

so 0.11.7-SNAPSHOT means, this is some code that exists somewhere between the release of 0.11.7 and the version before 0.11.7

dmaiocchi20:07:39

OK so one should remember to remove it when realising šŸ˜

seancorfield20:07:48

...between 0.11.6 and...

hiredman20:07:00

so if you were cutting a release of some project you might switch from the snapshot version to the release version, cut the release, and then switch it to the next snapshot version

hiredman20:07:21

in general using snapshots is bad, it is using unreleased software

seancorfield20:07:42

Another important thing to remember is that 0.11.7-SNAPSHOT can be a different artifact at different times so you don't want to depend on an artifact by name that could change out from under you.

hiredman20:07:55

a lot of projects don't publish snapshot artifacts anyway

seancorfield20:07:23

For example, Clojure has daily builds and they are all named 1.11.0-master-SNAPSHOT every day -- and each of them could be different builds.

dmaiocchi20:07:27

OK thx I got it clj clojure-berlin

dmaiocchi20:07:35

I might remove it

dmaiocchi20:07:00

Since I don't plan to do so often releases.'šŸ˜

dmaiocchi20:07:43

Currently I'm trying to do a o.o.1:grin:

Trevor22:07:17

Does this exception make sense to folks?

java.lang.ClassCastException: com.badlogic.gdx.physics.bullet.collision.SWIGTYPE_p_p_btCollisionObject cannot be cast to com.badlogic.gdx.physics.bullet.collision.btCollisionObject
Is the SWIGTYPE version of btCollisionObject something clojure came up with that makes it so it can't compare itself against the java version of the class?

Trevor22:07:22

Oh, looks like SWIG is something to do with the C++ bindings

jason poage22:07:06

So it seems im back to my original problem with clojurescript. I have a script I import into my nodejs project using a <script> tag in the public html file. now im trying to do unit testing and this workaround isnt working anymore. Any ideas of how to import a compiled clojurescript into a nodejs app?

lilactown22:07:03

you have a node.js server that is showing an HTML file with a ClojureScript app added via a script tag?

jason poage23:07:54

but now i cant even get my cljs file to build and im not sure what happenedā€¦

jason poage23:07:03

it doesnt give me a file name

jason poage23:07:58

java.lang.ClassCastException: clojure.lang.PersistentVector cannot be cast to clojure.lang.IPersistentMap at clojure.lang.RT.dissoc (RT.java:887) clojure.core$dissoc.invokeStatic (core.clj:1511) clojure.core$dissoc.invoke (core.clj:1504) leiningen.cljsbuild.config$backwards_compat_crossovers$fn__854.invoke (config.clj:70) clojure.core$map$fn__5851.invoke (core.clj:2753) clojure.lang.LazySeq.sval (LazySeq.java:42) clojure.lang.LazySeq.seq (LazySeq.java:51) clojure.lang.RT.seq (RT.java:531) clojure.lang.LazilyPersistentVector.create (LazilyPersistentVector.java:44) clojure.core$vec.invokeStatic (core.clj:377) clojure.core$vec.invoke (core.clj:367) leiningen.cljsbuild.config$backwards_compat_crossovers.invokeStatic (config.clj:70) leiningen.cljsbuild.config$backwards_compat_crossovers.invoke (config.clj:63) leiningen.cljsbuild.config$backwards_compat.invokeStatic (config.clj:77) leiningen.cljsbuild.config$backwards_compat.invoke (config.clj:76) leiningen.cljsbuild.config$normalize_options.invokeStatic (config.clj:152) leiningen.cljsbuild.config$normalize_options.invoke (config.clj:148) leiningen.cljsbuild.config$extract_options.invokeStatic (config.clj:185) leiningen.cljsbuild.config$extract_options.invoke (config.clj:179) leiningen.cljsbuild$cljsbuild.invokeStatic (cljsbuild.clj:258) leiningen.cljsbuild$cljsbuild.doInvoke (cljsbuild.clj:249) clojure.lang.RestFn.applyTo (RestFn.java:142) clojure.lang.Var.applyTo (Var.java:705) clojure.core$apply.invokeStatic (core.clj:667) clojure.core$apply.invoke (core.clj:660) leiningen.core.main$partial_task$fn__6592.doInvoke (main.clj:284) clojure.lang.RestFn.applyTo (RestFn.java:139) clojure.lang.AFunction$1.doInvoke (AFunction.java:31) clojure.lang.RestFn.applyTo (RestFn.java:137) clojure.core$apply.invokeStatic (core.clj:667) clojure.core$apply.invoke (core.clj:660) leiningen.core.main$apply_task.invokeStatic (main.clj:334) leiningen.core.main$apply_task.invoke (main.clj:320) leiningen.core.main$resolve_and_apply.invokeStatic (main.clj:343) leiningen.core.main$resolve_and_apply.invoke (main.clj:336) http://leiningen.do$do.invokeStatic (do.clj:40) http://leiningen.do$do.doInvoke (do.clj:32) clojure.lang.RestFn.applyTo (RestFn.java:139) clojure.lang.Var.applyTo (Var.java:705) clojure.core$apply.invokeStatic (core.clj:667) clojure.core$apply.invoke (core.clj:660) leiningen.core.main$partial_task$fn__6592.doInvoke (main.clj:284) clojure.lang.RestFn.applyTo (RestFn.java:139) clojure.lang.AFunction$1.doInvoke (AFunction.java:31) clojure.lang.RestFn.applyTo (RestFn.java:137) clojure.core$apply.invokeStatic (core.clj:667) clojure.core$apply.invoke (core.clj:660) leiningen.core.main$apply_task.invokeStatic (main.clj:334) leiningen.core.main$apply_task.invoke (main.clj:320) leiningen.core.main$resolve_and_apply.invokeStatic (main.clj:343) leiningen.core.main$resolve_and_apply.invoke (main.clj:336) leiningen.core.main$main$fn_6681.invoke (main.clj:452) leiningen.core.main$_main.invokeStatic (main.clj:442) leiningen.core.main$_main.doInvoke (main.clj:439) clojure.lang.RestFn.applyTo (RestFn.java:137) clojure.lang.Var.applyTo (Var.java:705) clojure.core$apply.invokeStatic (core.clj:665) clojure.main$main_opt.invokeStatic (main.clj:491) clojure.main$main_opt.invoke (main.clj:487) clojure.main$main.invokeStatic (main.clj:598) clojure.main$main.doInvoke (main.clj:561) clojure.lang.RestFn.applyTo (RestFn.java:137) clojure.lang.Var.applyTo (Var.java:705) clojure.main.main (main.java:37)

hiredman23:07:39

my guess would be an error in your project.clj

lilactown23:07:10

that would make sense for the error that was posted

lilactown23:07:19

as for unit tests, it doesn't sound like it has anything to do with node.js

lilactown23:07:30

once you've resolved the above error: how are you trying to run unit tests?

jason poage23:07:45

yeah that was most definitely the issue. thanks

jason poage23:07:00

the error above is independent from my nodejs problem. but i think i found what im looking for online, finally, but i wont know until i try it and see if it works. it is definitely something i havent tried yet so its worth a shot

jason poage23:07:58

i was trying what was described here and im having no luck so far https://lambdaisland.com/blog/2017-05-02-nodejs-scripts-clojurescript

lilactown23:07:40

@jason821 it sounds like you don't want to run your ClojureScript in Node.js though

lilactown23:07:46

you're including it as a script tag on an HTML page?

lilactown23:07:31

so is the ClojureScript supposed to run in the browser as a UI app, or is it supposed to run on the server side in Node.js?

jason poage23:07:14

which ever way it works tbh, but preferably in nodejs

jason poage23:07:27

because most of my project is written in javascript

lilactown23:07:35

that doesn't make sense to me

lilactown23:07:58

Node.js runs on the server, so you're not going be able to, for example, update the page dynamically based on your clojurescript code

lilactown23:07:42

what does your clojurescript code do?

jason poage23:07:25

i dont want to dynamically load it as clojurescript, nodejs will automatically refresh if anything changes

jason poage23:07:47

so if i have a compiled js file that works on nodejs, thats what i need

lilactown23:07:21

there are ways to compile CLJS to be imported into a Node.js runtime

lilactown23:07:40

I don't really know what you mean by "dynamically load it as clojurescript"

lilactown23:07:28

shadow-cljs is a build tool which has built-in configurations for developing node modules

jason poage23:07:05

that was your verbage lol

jason poage23:07:37

i compile the cljs manually, the nodejs will update it. ideally i would like to have cljs reload but thats not how my project is setup

jason poage23:07:00

im trying out shadow cljs and im trying to figure out if it will compile it to javascript to load in my nodejs app by importing it from javascript files

jason poage23:07:46

i could just be going about it the wrong way either way, the js file needs to import the clojurescript after its compiled to javascript

jason poage23:07:33

i have an idea, maybe im suppose to load the javascript app with shadow cljs instead of the other way around. but im not too sure since the app starting out being javascript