Fork me on GitHub

Hello, I am doing iloveponies course on clojure. So far I am enjoying it. I am stuck with an exericse, and unable to understand the soultion @akiva, maybe you can help (reduce #(str %1 " " %2) a-seq) , can somebody explain how would this work?. What does # signify and how does %1 %2 get assigned values


@ratnakar #(...) is syntax sugar for (fn [] ...)


%1 is the first argument, %2 is the second


Thanks @kamn , I see, but in the above code, how does reduce get invoked with 2 params?


Reduce has a remainder and the element of the liat


oh I see, got it thanks


np. It still gets me because for whatever reason when I see reduce I think map first then I realize there is a remainder


How might I put this into thread macro form?


(->> array1 (filter #(belongs? % array2)) sort vec)


@ratnakar: @kamn: What worked for me in groking reduce was to think it calls a fn over each element of a seq where the first arg in fn is the accumulating value and the second arg is the current value from the seq. Use reduce when you want to consume a sequence and produce a single value (or combine a number of values in some way to produce a smaller sequence). Whenever you think of using recursion (or a for) to consume a sequence and produce a value consider reduce instead.


Hi, can you help me debugging a OutOfMemoryError Java heap space? I'm trying to move data from Mongodb to Postgres using Monger and After some millions rows I'm running out of heap space... The relevant part of the code is

(defn translate-list [mongo-rows]
  (map translate mongo-rows))

(defn insert-list! [db-spec table coll]
  (apply (partial j/insert! db-spec table) coll))

(defn go []
  (let [mongo-rows (mc/find-maps mongo "events")
        batches (partition-all 100000 mongo-rows)]
    (map #(insert-list! db :events (translate-list %)) batches)))
from what I know everything except for my translate function should be lazy. If I decrease the partition size I'm able to load more rows but eventually I get the exception


I guess that the return value of the map call is realized and thus you’ll end up with all the values in memory. (doseq [b batches] (insert-list! db :events (translate-list b)) should do it.


@ordnungswidrig: is it because (doc doseq) Does not retain the head of the sequence. ?


thanks a lot!


you’re welcome!


@mbarbieri: the other reason to use doseq rather than map is that you are mapping a function over a sequence only for a side effect (i.e. a db insert). See this from @stuartsierra


@agile_geek: bookmarked, thanks!


@mbarbieri: no problem. I've been bitten by this a few times where code running in my repl works (as the repl print phase forces realisation of the seq) but the code does nothing when I run it from an uberjar.


@agile_geek: ah that's why from uberjar it was not doing anything!!.... I was running it from repl for the same reason


This is the classic symptom of laziness over a side-effect producing fn


I'm curious if map could be replaced with run! in this case (instead of using doseq)


as it runs reduce it would certainly realise the seq. Not sure what the advantages are over doseq? Anyone know?


would be nice to know


@agile_geek: the difference between doseq/map and run! it might be related to chunking and transducers.


Hmm, not sure. run! just calls reduce and I haven't look at source for doseq

Alex Miller (Clojure team)17:01:11

run! uses a reducing function so can be use to pass data down the line (vs doseq)


FWIW, the above discussion prompted me to write about this problem domain in ClojureScript


Thanks @agile_geek , that helps.


@mfikes yep, just read your post. Very informative as my cljs knowledge is limited.


@alexmiller thx. Confirms what I thought. @mbarbieri use case just needs to call a fn for a side effect over every element of a seq so either would work.


@ratnakar gr8 pleased it helped.


We are all beginners, especially with ClojureScript. Hope there aren't silly job postings requiring 5 years experience with it. :)


@mfikes I saw a job ad for 10 yrs Java experience in 1999! Even James Gosling couldn't have applied.


@alexmiller: technically run! only returns nil if the proc function passed in returns nil(run! identity (range 5)) produces 4, not nil, for example.

Alex Miller (Clojure team)20:01:57

@seancorfield that's a bug - it's fixed in 1.8


I’m gonna work up a patch for the same for ClojureScript (CLJS-1546)


has anyone tried interacting with a C# service through REST?


hey @jcomplex- the language the REST service is written in shouldn't matter. Take a look at clj-http:, very easy to initiate REST service requests


if the service provides responses in JSON format, cheshire is a good parsing tool:


I have kind of a weird issue where hikari-cp is trying to make a connection to my database at compile time?