Fork me on GitHub
#clojure
<
2019-02-18
>
craftybones04:02:02

Is there a way to refer to an unevaluated form inside a macro without doing the following ?

(defmacro debug [form]
  `(do (println (list [email protected])) ~form))

vemv04:02:57

(defmacro debug [form]
  `(do
     (println ~(list 'quote form))
     ~form))
?

craftybones05:02:19

@U45T93RA6 Thanks. I guess you can’t do it without somehow making a list of it again.

vemv05:02:56

that's not a list, in the same way that 'x is not a list (clojure implements 'x as a list though... which is why my macro looks like that)

kenj06:02:32

I love how approachable the core.async src is. I mean, 90% is way over my head, but it’s like 1000x less scary than I imagined it for what it’s accomplishing.

dpsutton07:02:31

watch tim baldridge's videos on the macros. they are well worth the time

👍 5
roklenarcic10:02:17

Is there a good writeup about #= reader conditional? I tried using it and it has very confusing semantics:

'{:a #=(vec (list [1 2 3]))}
=> {:a [list [1 2 3]]}
{:a #=(vec (list [1 2 3]))}
=> {:a [#object[clojure.lang.PersistentList$Primordial 0x488c6b35 "[email protected]"] [1 2 3]]}
It's like it evaluates first function but quotes the argument list of the said function

Alex Miller (Clojure team)10:02:51

It’s happening at read time so no evaluation has occurred on the args

Alex Miller (Clojure team)10:02:15

It’s intentionally not doc’ed because it’s not a public feature

witek13:02:40

I often need to assoc a key to a map if it does not already exist. This is how I usually do it (if (:foo m) m (assoc m :foo (compute-foo))). Is there a nicer way to do this?

sgerguri13:02:37

(assoc m :foo (or (m :foo) (compute-foo)))

witek13:02:07

But this would invoke (compute-foo) even if :foo is already set. 😞

sgerguri13:02:07

I had or in my first response, then changed it to a map-get operation, then back to or because of this. or is lazy.

witek13:02:14

or is lazy? Thanks!

sgerguri13:02:37

user=> (doc or)
-------------------------
clojure.core/or
([] [x] [x & next])
Macro
  Evaluates exprs one at a time, from left to right. If a form
  returns a logical true value, or returns that value and doesn't
  evaluate any of the other expressions, otherwise it returns the
  value of the last expression. (or) returns nil.

NoahTheDuke15:02:16

I have a vec of symbols and quoted function calls to be used in a let-binding macro that I've quoted, and I want to turn it into a map so I can use the symbol as the key and get the quoted function back. I currently have the vec quoted, and in a thread macro I call (partition 2) (map (juxt first identity)) (into {}) which neatly handles what I'm looking to do. My question is, why do I need to call (map (juxt first identity))?

NoahTheDuke15:02:32

If I leave it out, I get the clojure.lang.Symbol cannot be cast to java.base/java.util.Map$Entry error, but if I put it in, I've just created a list of ['symbol '(symbol (function call))], right?

NoahTheDuke15:02:49

What am I missing in this situation?

Mitch15:02:07

(into {}) expects a sequence of 2-vectors, [key val], that will become key-val pairs in your map. So, (map (juxt first identity)) puts them in that form.

NoahTheDuke16:02:59

Ah, okay. Thanks

superancetre15:02:27

I used float-array to pass a Java array of float to a Java method. That method returns me back an array of float. Is there a sister function of float-array that does the inverse, so that I can use the array of float with Clojure? Or am I misunderstanding something?

superancetre15:02:46

Like how to print the array?

dpsutton15:02:45

i believe vec can take arrays and knows how to make PersistentVectors from them

superancetre15:02:31

Yes that work, thanks a lot!

👍 5
superancetre15:02:26

I added an entry into clojuredocs to make it more visible https://clojuredocs.org/clojure.core/float-array

parens 10
superancetre15:02:07

(into [] f) works as well (with f a Java array of floats)

jjttjj17:02:27

I need a way to use partition-all with a step of 1 but as a transducer... is there some way to get this by composing the transducer producing functions available in clojure.core or am I best off just sort of "copying" the transducer portion of the partition-all source code to meet my needs

keymone17:02:47

whats a good http client that allows a callback per chunk for streaming requests?

madstap19:02:39

@jjttjj You can use the partition from the xforms library.

(require '[net.cgrand.xforms :as xfs])

(= [[0 1 2] [1 2 3] [2 3 4]]
   (into [] (xfs/partition 3 1) (range 5)))

jjttjj19:02:09

@madstap thanks! I just found this a minute before your response

lilactown20:02:34

is there a trick to turn a clojure form into a list recursively at macro-time?

lilactown20:02:08

ex:

(defn foo [])
(defn bar [])
(defn baz [])

(my-macro (foo (bar (baz))))
;; => (#function[user/foo] (#function[user/bar] (#function [user/baz])))

isak20:02:16

@lilactown how about this: (defmacro my-macro [xs] (flatten xs)) and then "(macroexpand `(my-macro (foo (bar (baz))))" ?

lilactown20:02:11

ah sorry, I probably didn’t explain well. I don’t want to flatten, I want to retain the nested-ness

lilactown20:02:16

I just don’t want it to be evaluated

isak20:02:06

oh sorry, i misread. But in that case the macro can just return it without calling flatten, and the rest is the same

lilactown20:02:37

I went with this for now:

(defn listify [f]
  (if (seq? f)
    `(list [email protected](map listify f))
    f))

(defmacro my-macro [& body]
  `(list [email protected](map listify body)))

5
yedi20:02:50

hey all, so nil is considering less than everything else when comparing two things. is there a value that's considered more than everything else?

dpsutton20:02:52

across all types? seems like a tough order

dpsutton20:02:13

the greatest number

dpsutton21:02:15

there's the obvious

(defn my-compare [x y]
  (cond (= x y) 0
        (= x :value/max) 1
        (= y :value/max) -1
        :else            (compare x y)))

dpsutton21:02:47

I wanted to do something like this:

(deftype MAX []
  java.lang.Comparable
  (compareTo [this _]
    1))
(def absolute-biggest-thing-ever (MAX.))
which works for (compare MAX 3) but not the other way around. you get class cast exceptions

roklenarcic22:02:17

im probably missing something obvious but why

(meta ^:x 'x)
returns nil

hiredman22:02:38

'x => (quote x)

hiredman22:02:42

(quote x) => x

hiredman22:02:46

@dpsutton you have to use a custom comparator instead of relying on the default one that uses Comparable

hiredman22:02:25

if you attach metadata to the form (quote x), then evaluate the form and get x, the metadata isn't on x, it is still on the form from before evaluation

dpsutton22:02:18

like the my-compare example i gave or something else?

andy.fingerhut22:02:35

@yedi In Clojure's clojure.core/compare implementation, is there some value that is greater than everything else? No. There is a greatest Long (Long/MAX_VALUE), and a greatest Double value (+Inf), but there is no single value that is comparable to all other values, and greater than all of them. nil is special that way.

andy.fingerhut22:02:42

Why? Probably because Rich new that nil-punning was going to be common in Clojure, and didn't want exceptions raised when sorting collections using clojure.core/compare that contained nil mixed with other types. That is just my guess, though.

andy.fingerhut23:02:54

You are free to write your own custom comparator function that does whatever you like. If for some reason there is some value you wanted to be comparable to, and greater than, all other values, it isn't clear to me what would be a reasonable choice for such a value. You could choose nil for that, but then it would be greater than all other values, not less than all other values.