This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-09-20
Channels
- # announcements (16)
- # aws (1)
- # babashka (24)
- # beginners (54)
- # calva (25)
- # cider (4)
- # cljs-dev (2)
- # clojure (16)
- # clojure-dev (39)
- # clojure-europe (18)
- # clojure-nl (1)
- # clojure-uk (4)
- # clojurescript (66)
- # core-async (16)
- # cursive (13)
- # datomic (15)
- # deps-new (1)
- # duct (22)
- # fulcro (20)
- # jobs-discuss (1)
- # kaocha (3)
- # lsp (11)
- # malli (8)
- # off-topic (38)
- # other-languages (18)
- # polylith (18)
- # portal (15)
- # react (2)
- # reagent (9)
- # reitit (7)
- # releases (10)
- # reveal (1)
- # sci (13)
- # shadow-cljs (3)
- # specter (4)
- # sql (4)
- # xtdb (16)
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
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))
(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))
to define a macro you should use defmacro
instead of defn
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 https://github.com/theleoborges/clojure-macros-workshop by leonardo borges when i was learning macros.
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?
(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 : http://boofcv.org/javadoc/boofcv/io/image/ConvertBufferedImage.html#convertFrom(java.awt.image.BufferedImage,boofcv.struct.image.GrayU8)
@delaguardo 👍it works perfectly.
doesn’t look good thou ) so maybe you can try as @clement.malaingre suggested
@clement.malaingre it's a overloaded static method. If I just use nil, I will get "More than one matching method found".
@delaguardo thanks again though I don't understand why your suggestion works.
^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.
@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
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
(filter
identity
(vector
(when foo (foos))
(when bar (bars))))
what is a good way to build a coll without nils with variable elements?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
https://clojure.org/guides/weird_characters For the reference but it still looks odd because as-of should not need it
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 def
s.
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.
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
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): http://clojure-doc.org/articles/language/macros.html https://purelyfunctional.tv/mini-guide/when-to-use-a-macro-in-clojure/
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
yeah thats what I was saying, macros helps not to evaluate the parameters (parameters can be function again) while calling
Terrible code idea, but good example of what a macro can do - say you had a function like this
with a macro, you could call google translate once at compile time and the generated code wouldn't know that at all