This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-07-17
Channels
- # announcements (2)
- # babashka (1)
- # beginners (54)
- # calva (19)
- # clj-kondo (7)
- # cljs-dev (6)
- # cljsrn (18)
- # clojure (11)
- # clojure-europe (19)
- # clojurescript (7)
- # conjure (3)
- # cursive (7)
- # datomic (4)
- # figwheel-main (2)
- # fulcro (4)
- # helix (5)
- # honeysql (24)
- # instaparse (2)
- # lsp (20)
- # malli (17)
- # off-topic (6)
- # pathom (2)
- # practicalli (2)
- # shadow-cljs (2)
- # tools-deps (34)
- # vim (7)
I’m using (doseq [date dates] …)
to do some per-day calculations. I’m wondering, what other “looping” constructs might be good? Conceptually I’d like to turn everything into map
ping operations but I’m starting with a little bit more explicitness in transfomations
How do you invoke this function here https://clojuredocs.org/clojure.core/recur#example-542692d0c026201cdc326ea6
(def factorial
(fn [n]
(loop [cnt n
acc 1]
(if (zero? cnt)
acc
(recur (dec cnt) (* acc cnt))
; in loop cnt will take the value (dec cnt)
; and acc will take the value (* acc cnt)
))))
I was thinking something like (take 5 factorial)
but it’s not returning a sequence, it’s just a def
with an internal (fn [n] …)
so I don’t understand how @ invoke
oh, even as def
instead of defn
?
so it is!
Thanks. I did not know you could do that.
o.o mini mind blown
thanks for that. Great tip. I’ll have to macroexpand
errthang going forward
It makes a lot of sense. Everything is a function or… a collection … or primitive
everything is a java.lang.Object, some of those Objects happen to implement the right methods to be called (functions) or accessed for contents (collections)
or both
so here’s some weird looking stuff
(def sum-it-up
(fn [n]
(fn [y]
(+ n y))))
And((sum-it-up 3) 2)
=> 5
😄 clear as mud
I want to register some code within a macro to be called in a different context. I think I need to close over the refer somehow, or ns qualify it, but not sure how to do it
(register ; macro
(fn [x]
;; bar is from [com.something :refer [bar]]
(bar x)))
You could qualify bar yourself, com.something/bar
or if the com.something
ns is required in the ns that the macro is being defined, then using a syntax quote will automatically qualify all the symbols inside the macro. It would be easier to comment on an attempt that you’ve already made.
Is the code snippet a pseudo code for the macro definition of register or is that how you expect to call the macro? (I'm attempting to write one)
Not sure if the following is your intent,
(defmacro register [[x binding] & body]
`(do
(com.something/bar ~binding)
(let [~x ~binding]
~@body)))
(macroexpand-1 '(register [x 2] (when x "hello")))
It expands to,
(do (com.something/bar 2) (clojure.core/let [x 2] (when x "hello")))
not quite -- the thing is that bar
is actually part of the body, and the macro has to figure out its nature at the time the macro is evaled
it's a little hard to explain the practical use case without going into how pathom works
Ooh got it. From my pov it's quite difficult to do that if bar is not going to be passed in a definite position. But I'll let others chime in at this point
I don’t understand the question but I’m a total newb so… what’s the output you want?
bit hard to explain, but I actually just figured out the problem I was mulling over here over on #pathom
If you wanted bar to refer to the function bar that exist at the time the macro is defined, then you just need to use backquote bar and it'll expand automatically to whatever bar refers too at that time. But if you want bar to refer to whatever it is bound too when the macro is called, then you want
~'bar`
(def primes-display (atom [2]))
(defn get-primes [max]
(loop [n 3]
(let [mars (map #(not (= 0 (rem n %1))) @primes-display)] ;result bools table for is not divis?
(if (every? true? mars)
(swap! primes-display conj n)
)
(if (or
(not (> n max))
(not (> n (last @primes-display))))
(recur (inc (inc n)))
@primes-display))))
Hi, the above is a prime number sieve i made using an atom. It works I reckon, but I don’t know how to make it better so that it can handle numbers significantly larger than 3000.
I am under the impression that (map f coll)
will be a lazy coll, and every?
will short-circuit on a false.Maybe #code-reviews would be better for this? Unsure.
Apparently it can handle numbers as high as 15,000 in the online REPL as long as I don’t print the result…. Just getting the time
it takes… maybe the online REPL I’m testing it in is rate limited
@roelof there was this meetup: https://asamonek.github.io/events/2021/01/01/beginner-clojurians-online-meetup.html it might still be active, haven't checked
what would be the clojure way of writing this function? should I use loop
and recur
instead of reduce
here?
(defn all-symbols
[form]
(reduce (fn [syms exp]
(cond (symbol? exp) (conj syms exp)
(list? exp) (cset/union syms (all-symbols exp))
:else syms))
#{}
form))
Just an alternative using postwalk
(defn all-symbols
[form]
(let [syms (atom #{})]
(clojure.walk/postwalk (fn [x]
(when (symbol? x)
(swap! syms conj x)))
form)
@syms))
wow tree-seq is super useful ty for bringing this to my attention @UMPJRJU9E
why does this work:
(zipmap #{true false} (repeat :value))
but not this:
(zipmap (repeat :value) #{true false})