Fork me on GitHub
#beginners
<
2021-08-01
>
bnstvn08:08:22

Are there any real performance difference between inline and external protocol implementations? How to choose which one to use if you can change the type? From “Clojure Programming” I understood that the main benefit of inline implementation is the performance, and probably should use extend* — while I mostly see defrecord/ deftype (I guess)

bnstvn08:08:45

Not sure i did the right thing on measuring performance, but :

(defprotocol Noop
  (dont-do [this]))

(defrecord InlineNoop []
  Noop
  (dont-do [_] nil))

(def inline-op (->InlineNoop))

(criterium.core/quick-bench
  (dont-do inline-op)) ;; ==> Execution time mean : 2.100368 ns

(extend-type String
  Noop
  (dont-do [_] nil))

(criterium.core/quick-bench
  (dont-do "asd")) ;; ==> Execution time mean : 2.118537 ns

bnstvn08:08:26

> Thus, inline implementations, while tempting because they appear so familiar (methods > in classes!), are more static than other protocol extension mechanisms, and are best > kept as an optimizing step. (Clojure Programming)

zackteo12:08:17

Hello! May I know what are good ways to debug clojurescript in production/in a docker?

zackteo13:08:42

Also if I compile my code into a jar, and I have the code (shell/sh "python3" "resources/test.py" "10") how do I make sure my program refers to the "resources" folder in my jar ?

Cora (she/her)13:08:29

and then use whatever your build tool is to add that path to your jar

zackteo13:08:18

I just tried that but that will return an object like for the code

( "test.py")

zackteo13:08:01

Otherwise I am using lein and the resources folder is already being added to the jar it produces

Cora (she/her)13:08:58

can you try .getPath on that object and see if that works?

zackteo13:08:44

Right i think this might work tho I need to test how that might work from a jar hmmm

Cora (she/her)13:08:39

all else fails you can read the file in and write to a tmp location and run it from there

zackteo13:08:12

Yeap that kinda was what I was thinking

Cora (she/her)13:08:31

you could slurp in the resource and then pass that as the value to -c

zackteo13:08:04

Right I guess I'll try that next hahaha

Cora (she/her)13:08:48

sorry, I'm obviously guessing at all of this

zackteo13:08:18

no worries 🙂 is cool!

zackteo13:08:59

@corasaurus-hex seems like .getPath doesn't really work in the case of a jar Caused by: java.io.FileNotFoundException: /home/zackteo/Documents/Capstone/Clojure/URA-Capstone-Frontend/deploy/ura-site-search-0.1.0-SNAPSHOT-standalone.jar!/Single-Objective-Models/Generalised_v2_final.py (No such file or directory)

Cora (she/her)14:08:34

I suppose that makes sense since it's located inside a zip file

Cora (she/her)14:08:45

the .deleteOnExit is really nice to have

Steiner15:08:35

I meet a problem when trying lein test the error is Caused by: java.lang.ClassNotFoundException: clojure.set and in the file, it is written

(def solution-10
  (loop [nums (set (cons 2 (range 3 2000000 2)))
         n 3]
    (if (> (* n n) 2000000) (reduce + nums)
        ;; else
        (recur (clojure.set/difference nums (range (* n n) 2000000 n))
               (+ n 2)))))
what the fuck? I can run it on repl, but I faild in test ??

noisesmith14:08:06

namespaces can be preloaded by a repl that aren't loaded in a regular process, no clojure namespace other than clojure.core is guaranteed to be loaded before you require it

alpox15:08:05

@steiner3044 is it possible that you imported clojure.set in your repl already but its not available from your testrunner?

Steiner15:08:54

@alpox may I ask, how ?

alpox15:08:28

In the REPL: (require '[clojure.set]) In a namespace form:

(ns xyz
  (:require [clojure.set]))

Steiner15:08:58

thanks 🙂

dpsutton15:08:36

just add a require to it in your namespace. (ns the-ns (:require [clojure.set :as set])) and then use set/difference

Mattias18:08:00

I was suddenly confused, again, trying to use Reveal with Atom/Chlorine. Reveal eats prepl, Chlorine wants socket repl. Probably a thousand ways to wire it up anyway, but I’m not fluent in repls enough to improvise. Help or pointers much appreciated, general or specific to my situation 🙂

Jelle Licht21:08:37

How can I run tests that depend on genclass -shenanigans in a deps.edn + build.clj based project?

seancorfield22:08:27

@wordempire You need to run the compile step, then run your tests: tools.build is not intended as a test runner (although we've made that work by using java-command and process to run tests as a subprocess).

Jelle Licht23:08:26

I already have my test-runner set up under a deps.edn alias; I currently have to run run my tools.build compile command, and then run the test runner; what exactly are you suggesting here? Having some tools.build step call out to my test runner alias using java-command or process?

seancorfield23:08:09

"I currently have to run run my tools.build compile command, and then run the test runner" -- that is the expected workflow.

seancorfield23:08:03

But at work, we've used create-basis, java-command, and process to run tests from build.clj. It's non-trivial but possible.

seancorfield23:08:35

If you normally run tests via -M:test, the you'd create-basis with {:aliases [:test]} and pass the basis to java-command along with a vector of command arguments, like :main-opts plus whatever else you'd provide on the command-line, and then you pass the result of that into process. I should write up an example of it on my blog -- or incorporate it into one of my OSS projects so folks can see how it is done.

seancorfield23:08:23

Like so:

(! 1672)-> head deps.edn 
{:paths ["src"]
 :deps {org.clojure/clojure {:mvn/version "1.10.3"}
        org.clojure/java.data {:mvn/version "1.0.86"}

        camel-snake-kebab/camel-snake-kebab {:mvn/version "0.4.2"}}
 :aliases
 {:build {:deps {io.github.clojure/tools.build {:git/tag "v0.1.7" :git/sha "8a3abc2"}}
          :ns-default build}
  :test {:extra-paths ["test"]
         :extra-deps {org.clojure/test.check {:mvn/version "1.1.0"}
...
(! 1673)-> cat build.clj 
(ns build
  (:require [clojure.tools.build.api :as b]))

(defn run-tests
  [_]
  (let [basis (b/create-basis {:aliases [:test]})
        cmds  (b/java-command {:basis     basis
                               :main      'clojure.main
                               :main-args ["-m" "cognitect.test-runner"]})]
    (b/process cmds)))
(! 1674)-> clojure -T:build run-tests

seancorfield23:08:02

You can see the whole deps.edn here: https://github.com/seancorfield/next-jdbc (for the rest of the :test alias that has the Cognitect test-runner in it.

Jelle Licht23:08:42

Thank you! This was pretty much what I was looking for (and not understanding) until you clearly laid it out here.