This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-03-19
Channels
- # announcements (5)
- # babashka (49)
- # beginners (11)
- # biff (5)
- # calva (123)
- # clerk (9)
- # cljdoc (5)
- # cljs-dev (9)
- # clojure (62)
- # clojure-europe (32)
- # clojure-nl (1)
- # clojure-norway (54)
- # clojure-uk (3)
- # clojurescript (30)
- # community-development (5)
- # cursive (9)
- # devops (5)
- # events (1)
- # fulcro (35)
- # graalvm (10)
- # gratitude (3)
- # hyperfiddle (9)
- # jobs (3)
- # keechma (1)
- # lsp (10)
- # malli (14)
- # off-topic (42)
- # overtone (1)
- # releases (3)
- # shadow-cljs (66)
- # squint (153)
- # xtdb (19)
Hi, stupid regex problem, I'm not getting what I'm missing... Trying to match "ERROR"
in the string "[ERROR]: message"
I would think (str/split msg #"\[\w\]:")
would match it, but it doesn't seem to match. I feel I'm missing something really stupid.
not looking for re-matches or anything like that, my question is more what's wrong with my actual regex.
(I do use a str/replace
in my actual code with a matching group to extract that part out)
I'm doing https://exercism.org/tracks/clojure/exercises/log-levels/edit
and i'd think that code would just work fine:
(defn log-level
"Takes a string representing a log line
and returns its level in lower-case."
[s]
(str/lower-case (str/replace s #"\[(\w+)\]: .*" "$1"))
)
but I'm getting "error\r\n"
as a result.
Regex witt only a dot normally does not match new line or carriage return characters
What are you hoping to be returned from this function?
Are you sure your input doesn't have newlines in it? If you're slurping a file it might have a trailing newline
You are telling it to replace everything except any new lines after the right square bracket with $1, so the carriage return remains in the replaced string
Agreed. If you only want whatever matched the \w+ part, then replace is an odd choice
thanks valerauko, I'll do that next then. I was just eh replace all the string with the thing I matched... Just couldn't understand why it was adding the \r\n.
Yeah, it was not adding those characters. You were, without realizing it, telling it to leave them there
Would this be an idiomatic way to write it?
(str/lower-case (get (re-find #"^\[(\w+)\]:" s) 1))
Is there a clojure way to measure maximum memory consumption by the function call? (not the object allocation memory). Something like (max-memory-used (fib 300))
?
I recommend using an actual profiling tool, but if you want something with absolutely zero dependencies, then simply use this macro:
(import com.sun.management.ThreadMXBean)
(import java.lang.management.ManagementFactory)
(defn get-usage []
(let [thread-bean (ManagementFactory/getThreadMXBean)]
(System/gc)
(.getCurrentThreadAllocatedBytes ^ThreadMXBean thread-bean)))
(defmacro with-memory-statistics [& body]
`(let [start# (get-usage)
result# (do ~@body)
end# (get-usage)]
(array-map :result result# :diff (- end# start#) :start start# :end end#)))
Thanks a ton, @U0479UCF48H
My intention is indeed to do something like
(System/gc)
(measure)
(run the body)
(System/gc)
(measure)
ah yeah you’re right… I just took a look at the docs for getCurrentThreadAllocatedBytes, and this returns total # of bytes allocated in the thread (as in, a running total). So running GC is unnecessary at best and, at worst, only measures memory leaks.
so just deleting the (System/gc)
call should be good enough. the start
value is going to be strictly increasing, even if the java heap shrinks, since it measures how many bytes the JVM tried to allocate in total for that specific thread
Perhaps worth considering: (System/gc)
is advisory and is no guarantee that any GC will actually happen at that point.
@U04V70XH6 That explains why I got meaningful results for both variations of the script. Thanks, good to know!
I would really like a redis-alike, but with clojure refs and datastructures. Does such a thing exist?
https://github.com/taoensso/carmine translates clojure data structures iirc
https://github.com/tolitius/redclaw I think this may be similar to what you describe: creating redis-backed data structures that implement the expected interfaces.
and failing that, are there any examples of really fast networked clojure services? i.e. not naive HTTP
What does "really fast" mean? What does a "network service" mean? Are these really the only metrics?
With such a tacit description, I doubt there's anything better than java.net.ServerSocket
. :)
aleph supports raw tcp/udp services, and it's rather performant, but I suppose you know about it 🙂
You can also interop with Aeron's Java implementation, I think Onyx did that "back in the days".
Are there any features in the https://www.youtube.com/live/AjjAZsnRXtE?si=TmccD4O4q7irfVNR which Clojure can benefit from? Maybe https://openjdk.org/jeps/458 can simplify packaging and running Clojure programs? I wasn't even aware of the https://docs.oracle.com/en/java/javase/21/docs/specs/man/jwebserver.html command, which came with Java 18...
https://openjdk.org/jeps/457 is exciting
https://openjdk.org/jeps/461 feels a bit like transducers for streams...?
it is, and I absolutely think you could write something that took a transducer and implemented a gatherer, which is a very interesting idea to bring our whole transducers library into java streams
https://openjdk.org/jeps/456 is _
as an "unnamed" variable 🙂 Feels like they've been getting quite a bit of Clojure inspiration here...
I think most changes coming and that have come to Java strengthen Clojure - less so for Kotlin and Scala which kinda do seem sillier
well several of us in the Clojure core are friendly with Brian Goetz so we try to influence him :)
Java in general
I agree. The java stdlib is pretty huge as-is and I find it very useful. Outside of tweaks to the JVM and GC, I don’t see much things that interest me much
https://openjdk.org/jeps/454 seems like a big deal