This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-12-03
Channels
- # adventofcode (107)
- # announcements (1)
- # asami (14)
- # babashka (67)
- # beginners (89)
- # calva (34)
- # cider (17)
- # clj-kondo (5)
- # cljs-dev (2)
- # clojure (57)
- # clojure-europe (52)
- # clojure-india (1)
- # clojure-italy (1)
- # clojure-losangeles (2)
- # clojure-nl (6)
- # clojure-uk (39)
- # clojurescript (40)
- # community-development (3)
- # conjure (3)
- # cursive (17)
- # datomic (11)
- # docker (13)
- # events (3)
- # figwheel-main (3)
- # fulcro (12)
- # graalvm (7)
- # holy-lambda (7)
- # honeysql (9)
- # introduce-yourself (5)
- # malli (9)
- # minecraft (3)
- # missionary (21)
- # nextjournal (7)
- # off-topic (52)
- # pathom (3)
- # polylith (11)
- # portal (3)
- # re-frame (21)
- # reagent (34)
- # reclojure (7)
- # reitit (1)
- # reveal (11)
- # shadow-cljs (68)
- # tools-build (12)
- # tools-deps (5)
- # vim (4)
- # xtdb (9)
Hi! I would to know if it exists a more idiomatic way to compare two nested values in a same map than this:
{:id 1 :name "item" :change-sets {:planned-end {:current "2022-01-12T12:21:32Z" :previous "2022-01-04T12:01:32Z"}}}
(< (get-in change-sets [:planned-end :current]) (get-in change-sets [:planned-end :previous]))
you could use a let for :planned-end and then (:prevous planned-end)
and do the same for current
Thank for your suggestion. So like that, right?
(let [planned-end (get-in items [:changes-sets :planned-end])]
(< (:current planned-end) (:previous planned-end)))
or even better using deconstructing
(let [{:keys [previous current]} (get-in items [:changes-sets :planned-end])]
(< current previous))
(let [{{c :current p :previous} :change-sets} {:id 1 :name "item" :change-sets {:planned-end {:current "2022-01-12T12:21:32Z" :previous "2022-01-04T12:01:32Z"}}}]
(< c p));; => false
i missed a key
(let [{{{c :current p :previous} :planned-end} :change-sets} {:id 1 :name "item" :change-sets {:planned-end {:current "2022-01-12T12:21:32Z" :previous "2022-01-04T12:01:32Z"}}}]
[c p]);; => ["2022-01-12T12:21:32Z" "2022-01-04T12:01:32Z"];
I'd probably do this:
(let [{:keys [current previous]}
(get-in items [:changes-sets :planned-end])]
(< current previous ))
Hello, I'm new to Clojure. I want to know what is the best way to implement the Cron Job in Clojure. Can anyone help me regarding that? Thank you.
https://github.com/troy-west/cronut wraps the Quartz Java library. It's working well for us.
chime is a common choice, I think?
if you google "clojure cron" you'll find a lot of good stuff
Actually found many things, I was confused and not able to figure out which is simple and best way to do it
I try to install clojure tools but ran into a problem According to this page : https://clojure.org/guides/deps_and_cli I have to do these steps :
Soon you will want to build and save your own code that makes use of these libraries. Create a new directory and copy this deps.edn into it:
$ mkdir hello-world
$ cp deps.edn hello-world
$ cd hello-world
$ mkdir src
so there is no way to scaffold a clojure - tools project so I do not have to make the aliases as a beginner ?
Maybe you're looking for something like https://github.com/seancorfield/deps-new?
how do I get this working
clj -X roelof.aoc2021/-main "Roelof"
Key is missing value: Roelof
Args to fn executed with clj -X
are passed as a map, so clj -X roelof.aoc2021/-main :name "Roelof"
you sure with this code :
(defn greet
"Callable entry point to the application."
[data]
(println (str "Hello, " (or (:name data) "World") "!")))
I expect to see Hello , Roelof
but I get Hello, {:name Roelof}!
@U0EGWJE3E I notice that you're invoking -main
, but showed the code for greet
. Are you changing data
before passing it to greet
perhaps?
The whole namespace looks like this :
(ns roelof.aoc2021
(:gen-class))
(defn greet
"Callable entry point to the application."
[data]
(println (str "Hello, " (or (:name data) "World") "!")))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(greet {:name (first args)}))
Command-line args are collected into a single map and passed to the invoked function as the only argument. Accepting that map as varargs (`& args`) wraps it in a list. That means (first args)
is the args map, and {:name (first args)}
becomes {:name {:name "Roelof"}
.
(ns roelof.aoc2021
(:gen-class))
(defn greet
"Callable entry point to the application."
[data]
(println (str "Hello, " (or (:name data) "World") "!")))
(defn -main
"I don't do a whole lot ... yet."
[args]
(greet args))
clj -X roelof.aoc2021/-main :name Roelof
Hello, Roelof!
very wierd that I see this again
clj -X roelof.aoc2021/-main :name "Roelof"
Hello, {:name Roelof}!
Is there a way to cancel a future and get a stack trace of what it was currently doing? I know with cider nrepl I can say interrupt that does I think exactly this but now I have some other thread I'll try evaling my work with nrepl and interrupting
future returns an object implementing java.util.concurrent.Future so you can use its cancel method
(.cancel (future (Thread/sleep 3000) (println "done") 100) true)
Yea, I was looking for a stacktrace of what the thread is doing at that moment though if that makes sense. Ah it threw interrupted exception 👀
it is possible
(.cancel
(future
(try
(Thread/sleep 3000)
(catch InterruptedException e
(prn e))))
true)
Any nice patterns to log each stage of a pipeline?
I like as->
but my timbre spy log now shows a reference rather than the output.
(defn run
"Run the script, accepts a :url"
[args]
(as-> (args->url args) n
(spy :info (url->imgs n))
(spy :info (imgs->srcs n)) ;; Logs – clojure.lang.LazySeq@21bf36e6
(spy :info (srcs->uris n)) ;; Logs – LazySeq etc.
(spy :info (strings->urls n)) ;; Logs – LazySeqq etc.
(spy :info (uris->out n))))
Perhaps there is a helpful fix in timbre itself: https://github.com/ptaoussanis/timbre/pull/200 But I’m interested in good Clojure composition for this type of stuff. Thanks
If I have a LocalDateTime, how can I make a value from it that satisfies inst?
Google fails me.
(.toInstant (.atZone date (ZoneId/ofOffset "UTC" (ZoneOffset/ofHours 0))))
I have that line in one of my projects )
Yeah, you'll need either an Offset or Zoned date time before it can be converted to an instant
(inst?
(let [date (java.time.LocalDateTime/now)]
(.toInstant (.atZone date (java.time.ZoneId/ofOffset "UTC" (java.time.ZoneOffset/ofHours 0)))))) ;; => true
Found myself this fantastic SO answer on the subject: https://stackoverflow.com/a/32443004/44639
why is my recursion method takes very long to work
(ns collatz-conjecture)
(defn new-collatz-number[num]
(if (even? num)
(/ num 2)
(+(* num 3) 1)))
(defn recursion-helper[num steps]
(if (== num 1)
steps
(recur (new-collatz-number num) (+ steps 1))))
(defn collatz [num]
(recursion-helper num 0))
looks like exercism. might want to try getting this into just 1 function and using inc
if it helps, i did all the problems a few years ago: https://github.com/tschady/exercism-clojure
@U14K4HW3T map
returns a lazy sequence. Nothing is forcing the realization of that lazy sequence so you are not seeing the results printed to the repl from the println
function
Thanks @U11BV7MTK makes total sense, dorun
and doall
made the job here…
there’s also run!
that’s pretty tailored to this usecase. Lazy sequences should not be used for side effects like this, because you might or might not actually do any work
the REPL probably made you think this would print everytime because of the “P” for print.
run!
worked perfectly for my case :
(defn -main []
(run! pretty-out (play input-from-keyboard)))
A small game, just need to understand the differences now run!
dorun
doall
Thank guys
doall
causes the entire sequence to reside in memory so it can return it, but the difference between dorun
and run!
is much fuzzier for me. The former uses next/recur
while the latter uses reduce
(with a nil accumulator), but what’s the practical difference?
☝️cool, I will enjoy it …
if I'm inside a threading macro, what is a good way to do multiple things with the same value? So put something like a let block in the middle that is fed by the threading macro. Is that understandable what I mean?
(->> [1 2 3 3]
(reduce +) ;; = 9
(#(* % %)) ;; -> put 81 in x
Math/sqrt ;; -> put 3 in y
(* x y)) ;; should result in (* 81 3)