Fork me on GitHub
Muhammad Hamza Chippa00:10:55

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])


(map (fn [i x y] …) (range) coll1 coll2)

nice 2

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’

seancorfield02:10:12 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=> (import '(java.time LocalDateTime) '(java.time.format DateTimeFormatter))
user=> (LocalDateTime/parse s DateTimeFormatter/ISO_OFFSET_DATE_TIME)
#object[java.time.LocalDateTime 0x7b44b63d "2022-07-15T16:49:27"]


We use 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 ^_^


what's the clojure equivalent of this

parens 1

Ask in #C0459PCHLTW perhaps?


the , (comma) I'd say. in between vector elements or map pairs 😆

Ben Sless05:10:27

(symbol "now what?")


Harry lifts his bangs to reveal a new ScarBuilder().append(new ZigZag()).toScar()

James Pratt04:10:55

Why does this work: (.getPath(first(.listFiles ( "notebooks")))) ;; => "notebooks/notes_further_to_stat_rethink.clj" but not this: (map .getPath (.listFiles ( "notebooks"))) ; Syntax error compiling at (.calva/output-window/output.calva-repl:68:1). ; Unable to resolve symbol: .getPath in this context

James Pratt04:10:55

Or even this: (doseq [file (.listFiles ( "notebooks"))] .getPath file) ; Syntax error compiling at (.calva/output-window/output.calva-repl:111:1). ; Unable to resolve symbol: .getPath in this context


You need to wrap Java methods in a Clojure function to pass to map

🙌 1

map #(.getpath %) …

🙌 1

(doseq [file (.listFiles ( "notebooks"))]
  (.getPath file)) ; parens are important here to make a function call

🙌 1

Because (.instanceMember instance) is a macro that expands to (. instance instanceMember)

(macroexpand '(.getPath (first (.listFiles (cloju
(. (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.

🙌 1
James Pratt07:10:20

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)

👍 1

macroexpand is your friend:

user=> (macroexpand '(.getPath foo))
(. foo getPath)


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
    (let [subscribers (->> @sync-state
                           (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!")
          {: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 is definitely not the count

👍 1

it should be the identity hash code of the seq

Clojure 1.11.1
user=> (str (map inc (range 10)))


Ah, makes sense! Thanks @U0NCTKEV8


so something short like that would indicate logging output getting cut off


or I guess an empty seq really can have that identity hashcode

user=> (str (filter number? (repeat 10 "a")))


^ yes — TIL!


I am just surprised, liking printing out a pointer and seeing 1


-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?

😮 2

TIL: this bug behaves in the same way, since clojure 1.0.0