This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-10-13
Channels
- # announcements (1)
- # babashka (30)
- # beginners (43)
- # biff (24)
- # calva (72)
- # cider (12)
- # clj-commons (24)
- # clj-on-windows (10)
- # cljsrn (23)
- # clojure (123)
- # clojure-bay-area (6)
- # clojure-europe (43)
- # clojure-losangeles (2)
- # clojure-nl (2)
- # clojure-uk (9)
- # clojurescript (125)
- # core-async (9)
- # cursive (2)
- # data-science (3)
- # datomic (30)
- # devops (1)
- # emacs (13)
- # events (5)
- # fulcro (15)
- # graalvm (3)
- # gratitude (1)
- # humbleui (11)
- # lsp (5)
- # nbb (24)
- # off-topic (11)
- # pedestal (5)
- # releases (1)
- # remote-jobs (1)
- # sci (15)
- # scittle (16)
- # shadow-cljs (15)
- # sql (11)
- # tools-deps (9)
- # xtdb (5)
How can I iterate through two vectors using map-indexed something like this
(map-indexed (fn [idx x1 y1] [idx x1 y1]) [5 3] [7 9])
Do you mean this?
=> (map-indexed (fn [i [x y]] [i x y]) [[:x1 :y1] [:x2 :y2] [:x3 :y3]])
([0 :x1 :y1] [1 :x2 :y2] [2 :x3 :y3])
hello, how can i parse this time ‘2022-07-15T16:49:27+08:00’ to java-time/local-date-time
(java-time/local-date-time "yyyy-MM-dd'T'HH:mm:ss" "2022-07-15T16:49:27+08:00")
the pattern how to represet the ‘+08:00’clojure.java-time
isn't going to help you much here -- it's fairly straightforward to use Java Time directly via interop:
user=> (def s "2022-07-15T16:49:27+08:00")
#'user/s
user=> (import '(java.time LocalDateTime) '(java.time.format DateTimeFormatter))
java.time.format.DateTimeFormatter
user=> (LocalDateTime/parse s DateTimeFormatter/ISO_OFFSET_DATE_TIME)
#object[java.time.LocalDateTime 0x7b44b63d "2022-07-15T16:49:27"]
user=>
See https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html
We use clojure.java-time
at work -- it's good for helping with conversion between date time types and for date arithmetic but we are slowly moving away from it to using just interop and Java Time. We've migrated away from several wrapper libraries for date/time over the years (`date-clj`, clj-time
-- which I used to maintain).
@U04V70XH6 thanks ver much, thanks for your suggestion ^_^
the ,
(comma) I'd say. in between vector elements or map pairs 😆
Why does this work:
(.getPath(first(.listFiles (
;; => "notebooks/notes_further_to_stat_rethink.clj"
but not this:
(map .getPath (.listFiles
(
; Syntax error compiling at (.calva/output-window/output.calva-repl:68:1).
; Unable to resolve symbol: .getPath in this context
Or even this:
(doseq [file (.listFiles
(
.getPath file)
; Syntax error compiling at (.calva/output-window/output.calva-repl:111:1).
; Unable to resolve symbol: .getPath in this context
(doseq [file (.listFiles ( "notebooks"))]
(.getPath file)) ; parens are important here to make a function call
Because (.instanceMember instance)
is a macro that expands to (. instance instanceMember)
(macroexpand '(.getPath (first (.listFiles (cloju
"notebooks")))))
(. (first (.listFiles ( "notebooks"))) getPath))
Also, doseq
returns nil
-- it's intended for side-effects -- so you would want for
there instead to get a sequence back.
Thanks all! So a clojure function on it's own will work in: (map func seq) But .getPath is not a function but forms a macro with it's first argument.
@U90R0EPHA How does that work? So any symbol starting with a .
is considered a macro by clojure and a macro is created on the fly?
@UC1DTFY1G Unfortunately I am not yet privy to the how on that, only to the evidence. But definitely seems to be something along those lines: anything starting with a dot in the function position of an expression becomes a macro.
;; `.getPath` interpreted as a symbol
user=> .getPath
Syntax error compiling at (REPL:0:0).
Unable to resolve symbol: .getPath in this context
;; `.getPath` seen as okay even though we know it is unresolvable
;; but `foo` is still unresolvable
user=> (.getPath foo)
Syntax error compiling at (REPL:1:1).
Unable to resolve symbol: foo in this context
;; `.getPath` clearly recognized as part of a `.` macro
;; but missing the class/instance
user=> (.getPath)
Execution error (IllegalArgumentException) at rebel-readline.clojure.main/eval1451$repl* (main.clj:82).
Malformed member expression, expecting (.member target ...)
And if you use the bare .
form that it expands to, then instance or class members will be recognized as members instead of unrecognized as symbols.
(. (first (. ( "notebooks") listFiles)) getPath)
macroexpand
is your friend:
user=> (macroexpand '(.getPath foo))
(. foo getPath)
user=>
Has anyone used ctmx with integrant? I'm having a problem that my system mapped passed into my ctmx routes is nil
when one of the nested component functions is called as part of a hx-get|post
request.
Most of the examples I've seen seem to be using global-state like mount for accessing resources (such as a database) inside components.
Should I be injecting those resources into the request map using a middleware/interceptor?
(is there a more appropriate place to ask ctmx related questions?)
i usually see 2 solutions: an interceptor that inject the resources make your routing spec a function that closes over the state (i prefer the first one, it's a bit simpler)
Yea, I usually do the second. But the way ctmx is working it's losing the closed state. Anyways I injected the system map into the request map, which is working well enough
(defn handle-message [{:keys [op] :as message}]
(condp = op
:transact
(let [subscribers (->> @sync-state
vals
(filter (fn [sess]
(and (= app-id (str (:id (:app sess))))
(= user-id (str (:id (:user sess))))))))
_ (log/infof "subs! %s" subscribers)]
(doseq [sub-sess subscribers]
(log/info "sending update!")
(ws/send
(json/write-str
{:op "tx-succ"
:tx completed-tx})
(:send-channel sub-sess))))))
When I run this code, I get the following prints:
INFO: subs! clojure.lang.LazySeq@1
But, I never get into "send update!"
Am I not understanding doseq?Oi. Carry on — I did (vec subscribers)
and saw that it was empty. I assumed LazySeq@1 meant a count of 1, but it may indicated some other thing
it should be the identity hash code of the seq
Clojure 1.11.1
user=> (str (map inc (range 10)))
"clojure.lang.LazySeq@c5d38b66"
user=>
Ah, makes sense! Thanks @U0NCTKEV8
or I guess an empty seq really can have that identity hashcode
user=> (str (filter number? (repeat 10 "a")))
"clojure.lang.LazySeq@1"
user=>
-Q: how does it know if it is empty or not, without eval the sequence?
• A: call str
on a lazy-seq make it be evaluated
(str (filter zero? (range)))
will freeze your repl
btw, it seems to be a bug.
In my understanding, .toString on lazy-seq do not show (1 2 3...)
to avoid evaluation...
if it evaluates, why not print in a regular/edn representation?