Fork me on GitHub
#beginners
<
2022-02-12
>
john2x00:02:05

Is there a way to toggle between instrument and unstrument all spec'd functions (or in a specific namespace)?

practicalli-johnny11:02:58

A common approach is to create a def of all things to instrument/ instrument and Pas that to the respective function https://practical.li/clojure/clojure-spec/generative-testing/example-projects/next-jdbc.html

Alex Miller (Clojure team)00:02:01

You can feed it the results of enumerate-namespace

Alex Miller (Clojure team)00:02:43

Or with no args it's “all” (but that's maybe too broad)

john2x00:02:25

thanks! I think no args sounds alright for me at the moment.

caumond09:02:22

Hi. I have a project A and its dependency B set as a local root in my deps.edn. In my B sub project there are some aliases I'd like to reach from the A root folder. Something like clojure -M:repl:dev.where dev is set in B project deps.edn . According to my experiments they are not invoked. Is there a way to do so? I am about to create a B-dev aliases in the A deps.edn but I find this awfull.

josef moudrik17:02:52

Hi! Newbie here.. How can I do floating point arithmetics in clojure? (with float java primitive, rather than double) For example, I was super surprised to see:

(type (+ (float 1) (float 2)))
=> java.lang.Double
but that seems to be caused by boxed float using double number ops in Numbers.Ops ops() function in Java for some reason. How to do "native" jvm float division from clojure? Anyway, I found https://insideclojure.org/2014/12/15/warn-on-boxed/, which got me excited, but alas! the following happens:
; running this
(defn sum [^float a ^float b] (+ a b))
; gives
Syntax error (IllegalArgumentException) compiling fn* at (src/playground/algo.clj:60:3).
Only long and double primitives are supported

; but !! this works fine
(defn sumn [^floats a] (reduce + a))
=> #'playground.algo/sumn
I don't get it. The https://clojure.org/reference/java_interop#primitives says that floats should be supported. Or not? Can someone pls explain?

delaguardo17:02:10

You can use arithmetic operations from Float class to ensure result is float as well https://docs.oracle.com/javase/10/docs/api/java/lang/Float.html#sum(float,float)

delaguardo17:02:09

(Float/sum (float 1) (float 2)) Something like this But I'm curious why you want this?

👍 1
delaguardo17:02:43

Double is way better for arithmetic

Alex Miller (Clojure team)17:02:03

Clojure embraces 64-bit numeric types and does not fully support either float or int (other than for the purposes of interop calls into Java APIs)

👍 1
1
josef moudrik17:02:24

@U04V4KLKC - what do you mean by better? I know just a bit about modern CPUs & next to nothing about JVM, but I'd be surprised if it could not take advantage of processor pipelines, etc, am I wrong? I imagine JVM should be able to sum 1024^2 floats faster than 1024^2 doubles, no? For large arrays that do not fit cache, mem transfers could also be a factor, etc. I think in TPUs & NN hw there definitely is trend to go even lower, e.g. have twice as many 16bit floats is a win, etc. Not sure how that translates to JVM, tho

josef moudrik17:02:35

@U04V4KLKC - thx for the Float/sum tip

josef moudrik17:02:09

@U04V4KLKC - anyway, to saturate Your curiosity 🙂 - I am playing with a 32-bit random number generator (I wanted a lazy FP generator which can be provided with seed) and as I only have 32bits of "randomness", I thought it a good idea to use floats, and in that I stumbled upon the "sum of floats is double" behavior..

andy.fingerhut18:02:08

If you are concerned about performance differences from using smaller types, you may be interested in libraries like core.matrix or Neanderthal, which give better performance by calling out to native libraries (Neanderthal always, core.matrix optionally depending on how you configure it)

👀 1
👍 1
Alex Miller (Clojure team)18:02:14

I think you'll find double arithmetic is pretty fast :)

🤞 2
Ben Sless04:02:25

Only place where I wished clojure supported ints is loops and array access. It's also easy to actually make mistakes there which cause unnecessary casts to be added

Ben Sless04:02:39

I'm the last guy to ask for compiler warnings but when ints and floats are promoted by the compiler in binding forms it might lead to more good than harm

Frederico Benevides20:02:26

Hi guys. I'm reading The Joy of Clojure and I don't understand one code to find a position. He changed the old code to the new one to accept a predicate. So he changed from (= e v) to (pred v). So when I call the pos function using (pos even? coll) I understand that this will be changed to (even? v) and so it's going to work.

My problem is how it continue working when I just send (pos 3 coll). Since the code is (pred v) it's going to be call like (3 v). How is possible this work? I don't know what I'm missing to understand this.

(defn index [coll]
  (cond
    (map? coll) (seq coll)
    (set? coll) (map vector coll coll)
    :else (map vector (iterate inc 0) coll)))

;; old code
(defn pos [e coll]
  (for [[i v] (index coll) :when (= e v)] i))

;; new code to use with predicate
(defn pos [pred coll]
  (for [[i v] (index coll) :when (pred v)] i))

(pos even? [:a 1 :b 2 :c 3 :d 4])
(pos 3 [:a 1 :b 2 :c 3 :d 4])

flowthing20:02:40

It doesn't work. Something else must be going on. You're possibly using a different implementation of pos than you think you're using…? For one, calling even? on the first element of that vector will throw because :a is not a number. (pos 3 ,,,) will fail for the reason you stated above:

λ clj -Srepro -M -r
Clojure 1.10.3
user=> (defn index [coll]
  (cond
    (map? coll) (seq coll)
    (set? coll) (map vector coll coll)
    :else (map vector (iterate inc 0) coll)))
#'user/index
user=> (defn pos [pred coll]
  (for [[i v] (index coll) :when (pred v)] i))
#'user/pos
user=> (pos 3 [:a 1 :b 2 :c 3 :d 4])
Error printing return value (ClassCastException) at user/pos$iter$fn (NO_SOURCE_FILE:2).
class java.lang.Long cannot be cast to class clojure.lang.IFn (java.lang.Long is in module java.base of loader 'bootstrap'; clojure.lang.IFn is in unnamed module of loader 'app')
(user=>

flowthing20:02:21

When you call (pos 3 coll) and it succeeds, I think you must be calling the first version instead of the second.

Frederico Benevides20:02:02

@U4ZDX466T It's true. It's not working. While I was retesting I ran the old code. thanks. I prefer to erase to not make people waste time

flowthing20:02:10

Sure thing. :thumbsup: