Fork me on GitHub
#beginners
<
2023-12-13
>
mister_m00:12:02

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?

seancorfield00:12:23

What do you mean by "same-keys added together"? Sounds like you want merge-with and some merging function for that...

mister_m00:12:14

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)

seancorfield00:12:56

(apply merge-with (fn ..) freq-list) -- no need for partial there.

Bob B00:12:03

and in this particular case, just + can work in place of the whole fn definition

Bob B00:12:05

(apply merge-with + [{:a 1 :b 2 :c 6} {:a 4 :b 5}])
=> {:a 5, :b 7, :c 6}

JI Lao02:12:01

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

dpsutton02:12:02

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

dpsutton02:12:48

(take 2 (map #(do (println %) (inc %)) (range 64)))

dpsutton02:12:27

(take 2 (map println (range 64))) etc

dpsutton02:12:41

i’ve never heard it called the inertia property though

JI Lao02:12:02

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?

JI Lao02:12:50

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

dpsutton02:12:00

(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

dpsutton02:12:01

This isn’t official but could offer some explanation: https://clojure-doc.org/articles/language/laziness/

JI Lao02:12:23

I should understand, I'll go back and look at the article in more detail, thanks for your patience!

seancorfield03:12:53

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.

🙌 1
dpsutton03:12:56

that’s what #C053AK3F9 is for. we’re happy to talk through things with you. love to answer questions

mister_m04:12:21

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.

mister_m04:12:05

I can probably just do this myself with re-seq and then using str/index-of for each match in the returned seq

Bob B04:12:40

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

👍 2
Can17:12:41

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!

Sam Ferrell17:12:57

I would just parse the input and return the original input if its nil or throws

Bob B17:12:13

I copied and pasted that code, and it seems to work as-is

Bob B17:12:44

that said, I agree with Sam

Sam Ferrell17:12:59

(let [x (parse-long input)] (if (nil? x) input x))

Sam Ferrell17:12:06

Or whichever function or Java constructor you prefer

Bob B17:12:03

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

Bob B17:12:00

perhaps something like:

(defn number-or-original [s]
  (if (string? s) 
    (or (parse-long s) s)
    s))

hiredman18:12:42

to go back to the original code posted, the example inputs and outputs don't match the actual behavior of the code

hiredman18:12:25

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

hiredman18:12:18

so the issue is actually not with your function, but with however you are getting those examples of its behavior

Can18:12:32

I tried some times and saw that result, after reading your messages, I restarted my computer and it started working. strange 🙂 Thank you so much for helping! My mistake 🙂

Can18:12:32

Sorry about that, I took your time 😞