Fork me on GitHub

I have a function that maps over some list. I want the function to return true if any of the elements returned by map are true. Is there an easier way of doing that than filtering and checking if the list is empty? The map will return (false false false), if any of these are true, i want the overall function to return true


Found out i could use every?


I started to learn macro and wrote below code,

(defn find-square-macro [args]
  `(map #(* % %) ~args))


But I am getting below output when ran through repl , Y so?

(find-square-macro (range 10))
 (fn* [p1__7748__7749__auto__] (clojure.core/* p1__7748__7749__auto__ p1__7748__7749__auto__))
 (0 1 2 3 4 5 6 7 8 9)) 


to define a macro you should use defmacro instead of defn


Oh my blunder! I changed it to defn and forgot!!!!


Any recommendations on what resources to look at when trying to pick up macros? o:


Learn the first rule of Clojure macro club... don’t write macros Use macros, they are useful, however, consider quite deeply if a macro is really needed or if it can just be done in a function (it usually can), which will make it composable with other code (unlike Clojure macros that are not composable) That said, the Brave Clojure book has a nice intro to macros


That's my exact instinct too hahaha. And watched a Clojure video echoing those sentiments. But needed to create a macro as a small programming assignment for a clojure role


I think imma just go through the chapter on macros in brave clojure and perhaps joy of clojure and clojure essential reference


i really enjoyed by leonardo borges when i was learning macros.

Y Qu14:09:23

Hello, I want to translate GrayU8 gray = ConvertBufferedImage.convertFrom(input, (GrayU8)null); into clojure. However, I don't know how to write (GrayU8)null. It's an example from boofcv. Can anyone help me?


(def ^GrayU8 gray (ConvertBufferedImage/convertFrom input ^GrayU8 nil))

Y Qu15:09:16

@zengxh thanks. But I get error "Metadata can only be apppied to IMetas"


(let [^GrayU8 null nil]
  (def ^GrayU8 gray (ConvertBufferedImage/convertFrom input null)))


Doesn't it work to just put nil ? From the API, it seems like the class only checks for existence and create a new unsigned image if the second argument is null :,boofcv.struct.image.GrayU8)

Y Qu15:09:10

@delaguardo 👍it works perfectly.


doesn’t look good thou ) so maybe you can try as @clement.malaingre suggested

Y Qu15:09:18

@clement.malaingre it's a overloaded static method. If I just use nil, I will get "More than one matching method found".


Oh yeah, makes sense.

Y Qu15:09:32

@delaguardo thanks again though I don't understand why your suggestion works.


^Gray8U x called type hint in clojure - here you can find more tldr; it adds to metadata {:tag …Gray8U} but metadata can be added only to something that implements IMetas interface. nil is primitive type that can’t have metadata so you can bind it to some var and attach type hint to it.

Y Qu15:09:57

@delaguardo wow,thank you very much. I see. Just now, I find a similar answer on stackoverflow to your suggestion.

Jeff Evans15:09:43

anyone have sample usage for clojure.reflect? specifically, how to instantiate an object once I have the clojure.reflect.Constructor instance? or convert that back to java.lang.reflect.Constructor (upon which I could call .newInstance)? all the examples on just seem to be printing the member info as an output table


Ok this may be a new low for me, I can't seem to get CLJS code to convert a js timestamp to a unix one


I tried

(.floor js/Math (/ (.now js/Date) 1000))


it works in a repl, but not when in an app


(js/Math.floor (divide (.now js/Date) 1000))
this code ended up working.


  (when foo (foos))
  (when bar (bars))))
what is a good way to build a coll without nils with variable elements?



😛 2

(concat (when foo [(foos])) (when bar [(bars)]))


Reading Datomic docs I noticed that strange syntax with @ in front of (def ) form. What does it do?


It dereference the var #'db-before. Not sure why tho

delaguardo17:09:03 For the reference but it still looks odd because as-of should not need it


My best guess is that it's a REPL related sugary trick - to see instance/object details


Don't think so. Looks like a typo to me


No no, it is not a typo. I think it is purposeful. I started using it and it makes total sense to add it to some of defs. Here is another example: It is just a little trick to wrap a form result into a def (and hold the result), at the same time showing the result to the REPL.


I am learning macros, and I understand that we can use macros only when we we do not want evaluate the function when we pass to any other function so we use macros, is there any other thing I am missing, I have referred many resource , But I did understand that reason is writing macro is not to be evaluated while calling


Mostly. Slight correction. You do not evaluate the parameters during the call.


if the parameter is function


No matter what the parameter of a macro is (function or not), the parameters of a macro are not evaluated, but passed, as is, to the body of the macro.


You might find the following two resources helpful (if you haven't already looked at them):

Cora (she/her)17:09:13

macros are used to generate code using code. the macros are "run" (expanded) when the code is read, not when the code is run, but the code that is generated is executed at runtime


how that will be different, we can write it using function too! right ?

Cora (she/her)17:09:02

you can change when code is executed with a macro. normal code has execution rules. a macro can generate code that gets around those rules

Cora (she/her)17:09:09

in a normal function call like (my-function param1 param2) , param1 and param2 are both executed before my-function is called. with a macro you could, say, only execute one of them if you wanted to

Cora (she/her)17:09:14

think of it like if , with an if call it only executes one of the branches, not both. a macro will let you do things like that

Cora (she/her)17:09:35

you could wrap both branches in functions and pass them in, but macros make it nicer

Cora (she/her)17:09:55

defn is a macro

Cora (she/her)17:09:34

you can also find a number of macros in compojure that make defining routes a lot easier


yeah thats what I was saying, macros helps not to evaluate the parameters (parameters can be function again) while calling

Cora (she/her)17:09:54

it lets you define how they are evaluated

Cora (she/her)17:09:00

they also are opportunities for optimization. reitit, iirc, makes lots of macro-time decisions about how to generate routes so that it doesn't have to make those decisions at runtime, increasing performance


hmmm, I partially got it, we can design the code as we want!


Terrible code idea, but good example of what a macro can do - say you had a function like this


(defn translate [from to text]


and it called out to google translate to translate the text


(translate :english :spanish "Hello") ;; "Hola"


with a macro, you could call google translate once at compile time and the generated code wouldn't know that at all


so you mean when we just use this function while writing code (translate :english :spanish "Hello") it will be ready with translation ?


if above translate is defined with macros


yeah, so hypothetically if you ahead of time compiled your code your code would ship with "Hola" and none of the google translate logic

Cora (she/her)17:09:13

so what will happen is the location where the macro is called will be replaced with the code that the macro generates