Fork me on GitHub
Abhi Saxena04:12:21

Hi All, I have a postgres function returning below result : {"title" : "ABC", "currentDate": "11/21/2022" , "data": [{"makeOver":"8","Parallel":"9"}]} {"title" : "XYZ", "currentDate": "12/21/2022" , "data": [{"makeOver":"9","Parallel":"12"}]} {"title" : "XXX", "currentDate": "01/20/2022" , "data": [{"makeOver":"10","Parallel":"9"}]} (all are individual maps) How can I iterate through each one of them if I have to change the Date format. Do I need to first convert it into a single collection or is there another way to achieve this where I can modify them with as-is structure.


Assuming xs is your data (sequence of maps), and f is a function that takes a date and returns it in the desired-format, you can do (map #(update % "currentDate" f) xs).


When you have a problem that involves many things, solve it for one and then map the solution over the many things. Not necessarily limited to programming problems 😛

👍 2
Duco Ergo Sum09:12:18

Hi Everyone, I have a list, (["1000" "2000"] ["3000" "4000" "5000"]) but it could be (["1000" "2000"] ["3000" "4000" "5000"] ["6000" "7000" "8000" "9000"]) . This is a two part problem but I suspect the answer might be the same or very different for each part. I want to convert each string into a number and then sum the numbers in each vector. If I do something like:

(let [[numb1 numb2] (["1000" "2000"] ["3000" "4000" "5000"])]
[(reduce + (map #(Integer. %) numb1))
(reduce + (map #(Integer. %) numb2))])
Then I can get [3000 12000] but that only works when the vector has a known size. Is there a more general way to do this?


map (or mapv) will iterate over the collection. For each of those iterations, you have the inner collection where you also want to map to parse the integer and then reduce to get the sum:

(mapv (fn [coll]
        (reduce + 0 (map parse-long coll)))
  [["1000" "2000"] ["3000" "4000" "5000"]])


parse-long is part of Clojure 1.11; otherwise you can replace with #(Integer/parseInt %)


as an alternative to (reduce + 0 numbers) you could also use (apply + numbers)

Duco Ergo Sum09:12:04

For your first solution I get an "Execution error" ... "Expected string, got clojure.lang.PersistenVector" and then variations on a theme when I poke it.


are you sure?

user=> (mapv (fn [coll]
        (reduce + 0 (map parse-long coll)))
  [["1000" "2000"] ["3000" "4000" "5000"]])
;;=> [3000 12000]

Duco Ergo Sum09:12:49

Yes but it was the classic interface between the keyboard and chair bug. I made a typo in (map parse-long coll)... I typed (amp . I apologise, sorry.


heh, no apology necessary! I wasn't sure I didn't make a typo myself, but that's why a REPL is really useful - you can quickly remove parts of a complex operation and re-evaluate until you find the source of the bug :)

Duco Ergo Sum09:12:36

I will play with your solution and try to understand it. Thank you!

Gagan Gayari10:12:04

How do I connect clojure to a neo4j db? Any good reference. I want to apply core.logic in my database.

delaguardo10:12:12 there are few connectors mentioned in neo4j documentation


is there a wrapper around jvm loom?

Ben Sless13:12:13

funcool/promesa and a few others already have stuff to work with loom

Ted Ciafardini15:12:22

Reading SICP with Clojure in mind - I have a question regarding iterative processes vs. recursive processes: When used - loop recur is an example of an iterative process - yeah?


tail recursion == iteration (== isomorphic)

Ted Ciafardini19:12:30

hey no spoilers

Ted Ciafardini19:12:11

just kidding its ok- also thank you clojure-spin

Jon Olick21:12:17

Most of clojure could theoretically be implementing using iteration instead of recursion - but the JVM makes that difficult for some reason I know not