Fork me on GitHub
#beginners
<
2022-10-13
>
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])

walterl00:10:54

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

dpsutton01:10:47

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

nice 2
1
vinurs02:10:47

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

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

seancorfield02:10:22

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

vinurs02:10:59

@U04V70XH6 thanks ver much, thanks for your suggestion ^_^

manas_marthi03:10:28

what's the clojure equivalent of this

1
parens 1
teodorlu05:10:30

Ask in #C0459PCHLTW perhaps?

lispyclouds05:10:34

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

Ben Sless05:10:27

(symbol "now what?")

Eric15:10:46

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

dumrat04:10:51

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

🙌 1
dumrat04:10:53

map #(.getpath %) …

🙌 1
seancorfield05:10:47

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

🙌 1
skylize05:10:59

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

(macroexpand '(.getPath (first (.listFiles (cloju
 "notebooks")))))
(. (first (.listFiles ( "notebooks"))) getPath))

1
seancorfield05:10:11

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.

dumrat10:10:56

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

skylize13:10:43

@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
seancorfield16:10:10

macroexpand is your friend:

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

Casey09:10:17

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

rolt10:10:07

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)

Casey11:10:58

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

stopa16:10:54

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

stopa16:10:51

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

hiredman16:10:51

it is definitely not the count

👍 1
hiredman16:10:19

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

stopa16:10:49

Ah, makes sense! Thanks @U0NCTKEV8

hiredman16:10:55

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

hiredman16:10:47

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

user=> (str (filter number? (repeat 10 "a")))
"clojure.lang.LazySeq@1"
user=>

stopa16:10:19

^ yes — TIL!

hiredman16:10:25

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

souenzzo17:10:16

-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

souenzzo17:10:59

(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
souenzzo17:10:04

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