Fork me on GitHub
#beginners
<
2021-09-20
>
Sebastian08:09:23

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

Sebastian08:09:28

Found out i could use every?

popeye08:09:11

I started to learn macro and wrote below code,

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

popeye08:09:57

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

(find-square-macro (range 10))
(clojure.core/map
 (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)) 

delaguardo08:09:00

to define a macro you should use defmacro instead of defn

popeye08:09:36

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

zackteo12:09:33

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

practicalli-john16:09:58

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

zackteo02:09:02

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

zackteo13:09:10

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

javi13:09:00

i really enjoyed https://github.com/theleoborges/clojure-macros-workshop 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?

Apple14:09:41

(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"

delaguardo15:09:33

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

Maravedis15:09:24

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 : http://boofcv.org/javadoc/boofcv/io/image/ConvertBufferedImage.html#convertFrom(java.awt.image.BufferedImage,boofcv.struct.image.GrayU8)

Y Qu15:09:10

@delaguardo 👍it works perfectly.

delaguardo15:09:52

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".

Maravedis15:09:00

Oh yeah, makes sense.

Y Qu15:09:32

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

delaguardo15:09:22

^Gray8U x called type hint in clojure - here you can find more https://clojure.org/reference/java_interop#typehints 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. https://stackoverflow.com/questions/32641762/casting-nil-in-clojure

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

Ryan16:09:06

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

Ryan16:09:54

I tried

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

Ryan16:09:07

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

Ryan17:09:30

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

Benjamin16:09:52

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

hiredman17:09:25

concat

😛 2
hiredman17:09:50

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

greg17:09:47

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

delaguardo17:09:50

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

delaguardo17:09:03

https://clojure.org/guides/weird_characters For the reference but it still looks odd because as-of should not need it

greg17:09:36

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

delaguardo04:09:47

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

greg11:09:08

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: https://docs.datomic.com/cloud/tutorial/assertion.html#sample-data 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.

popeye17:09:56

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

dorab17:09:11

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

popeye17:09:45

if the parameter is function

dorab17:09:47

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.

dorab17:09:09

You might find the following two resources helpful (if you haven't already looked at them): http://clojure-doc.org/articles/language/macros.html https://purelyfunctional.tv/mini-guide/when-to-use-a-macro-in-clojure/

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

popeye17:09:31

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 https://learnxinyminutes.com/docs/compojure/

popeye17:09:37

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

popeye17:09:38

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

emccue18:09:53

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

emccue18:09:16

(defn translate [from to text]
  ...)

emccue18:09:28

and it called out to google translate to translate the text

emccue18:09:43

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

emccue18:09:29

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

popeye18:09:27

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

popeye18:09:01

if above translate is defined with macros

emccue19:09:40

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