This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-12-13
Channels
- # adventofcode (37)
- # announcements (11)
- # babashka (46)
- # beginners (35)
- # biff (1)
- # clojure (44)
- # clojure-austin (1)
- # clojure-europe (23)
- # clojure-nl (2)
- # clojure-norway (8)
- # clojure-uk (5)
- # conjure (3)
- # cursive (22)
- # data-science (13)
- # docker (11)
- # events (8)
- # hyperfiddle (7)
- # joyride (1)
- # juxt (9)
- # malli (7)
- # matrix (4)
- # pedestal (3)
- # podcasts-discuss (1)
- # portal (1)
- # re-frame (62)
- # reitit (2)
- # releases (1)
- # schema (3)
- # sql (14)
- # squint (3)
- # xtdb (6)
- # yamlscript (4)
I'm drawing a big blank - how can I take a list of maps and "reduce" them into a single map with same-keys added together?
What do you mean by "same-keys added together"? Sounds like you want merge-with
and some merging function for that...
okay got it. I had a list of maps and had to do something like
(apply (partial merge-with (fn [v1 v2] (+ v1 v2)))
freq-list)
(apply merge-with (fn ..) freq-list)
-- no need for partial
there.
(take 1 (map println '(1 2 3 4 5))) vs. (take 1 (map println [1 2 3 4 5])) Why is the result different, in other words why does map on a vector cause the inertia property to fail?
Clojure makes no promises about “how lazy” the lazy data structures are. There are optimizations around chunking and type coercion that can lead to more laziness than you might expect
Sorry for my bad English, I'm not a native English speaker, I'm still a bit confused about your explanation above, is it saying that certain operations on the list will make it lose its LAZY, and if you use the literal `(1 2 3 4) it will keep it LAZY is it?
Is there an article or documentation on this, I'm confused about this, and also why '(1 2 3 4) and [1 2 3 4] work differently on this thread
(no apologies needed about your english. you are very clear and understandable)
The literal (1 2 3 4)
is not lazy at all. But map
will return a lazy list. But the “amount” of laziness isn’t something that you can easily control. Sometimes Clojure will do an optimization where it does “batches” or chunks of the map
in order to increase performance
This isn’t official but could offer some explanation: https://clojure-doc.org/articles/language/laziness/
I should understand, I'll go back and look at the article in more detail, thanks for your patience!
Let me know if you have questions about that laziness guide -- I didn't write it but I am maintaining that whole site now, and always interested in feedback. It's nice that many of the examples are interactive, so you can try updating the code and you'll see the result.
that’s what #C053AK3F9 is for. we’re happy to talk through things with you. love to answer questions
This could be more of a java question but how can I create a regular expression pattern, apply it to a string, and get the start/end positions of all the matches along with the matched text? For example I want to pull a number out of a line and note its start/end pos in the string.
I can probably just do this myself with re-seq and then using str/index-of for each match in the returned seq
it'd be possible to make a matcher using re-matcher, and then loop while .find returns true and use start, end, and group to get the desired attributes
Hello everyone! I'm currently developing a Clojure function that needs to parse mixed string inputs, handling various types of input such as integers and mixed strings containing both numbers and characters. Here's the function I've written so far:
(defn parse-number-from-string [param]
(if (string? param) ;my param can be string or number, if number I just want to return that as it is.
(if (re-matches #"\d+" param) ;Isn't re-matches truthy in Clojure?
(read-string param)
param)
param)
)
The goal is to have the function return numbers for strings that represent integers (e.g., "23423" should return 23423), but it should also handle cases where the input is just characters or a combination of characters and numbers.
Here are some examples of the function's behavior:
(parse-number-from-string "user5") ; => "user5" (expected: "user5")
(parse-number-from-string "foo") ; => "foo" (expected: "foo")
(parse-number-from-string ".,!134") ; => ".,!134" (expected: ".,!134")
(parse-number-from-string "23423") ; => "23423" (expected: 23423)
(parse-number-from-string "1") ; => "1" (expected: 1)
I expected the function to return numbers for strings that are valid integers, but it currently returns strings in those cases.
How can I modify the parse-number-from-string
function to correctly can anyone take a look please?
Thank you for separating time!I would just parse the input and return the original input if its nil or throws
(let [x (parse-long input)] (if (nil? x) input x))
Or whichever function or Java constructor you prefer
with that said, parse-long passed something that isn't a string (at least in clj) will throw, so given the statement that the input might be a number, probably want a guard in there
perhaps something like:
(defn number-or-original [s]
(if (string? s)
(or (parse-long s) s)
s))
to go back to the original code posted, the example inputs and outputs don't match the actual behavior of the code
(defn parse-number-from-string [param]
(if (string? param) ;my param can be string or number, if number I just want to return that as it is.
(if (re-matches #"\d+" param) ;Isn't re-matches truthy in Clojure?
(read-string param)
param)
param)
)
#'user/parse-number-from-string
user=> (parse-number-from-string "1")
1
user=>
so the issue is actually not with your function, but with however you are getting those examples of its behavior