Fork me on GitHub
#beginners
<
2016-01-15
>
ratnakar00:01:40

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

kamn00:01:32

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

kamn00:01:50

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

ratnakar00:01:39

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

kamn00:01:27

Reduce has a remainder and the element of the liat

ratnakar00:01:04

oh I see, got it thanks

kamn01:01:22

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

kopasetik05:01:15

How might I put this into thread macro form?

seancorfield06:01:23

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

agile_geek08:01:20

@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.

mbarbieri10:01:11

Hi, can you help me debugging a OutOfMemoryError Java heap space? I'm trying to move data from Mongodb to Postgres using Monger and clojure.java.jdbc. 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

ordnungswidrig10:01:43

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.

mbarbieri10:01:44

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

mbarbieri10:01:08

thanks a lot!

ordnungswidrig10:01:37

you’re welcome!

agile_geek11:01:07

@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 http://stuartsierra.com/2015/08/25/clojure-donts-lazy-effects

mbarbieri11:01:58

@agile_geek: bookmarked, thanks!

agile_geek11:01:46

@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.

mbarbieri11:01:12

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

agile_geek11:01:58

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

mfikes12:01:48

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

agile_geek12:01:20

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

meow12:01:37

would be nice to know

ordnungswidrig16:01:00

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

agile_geek16:01:51

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

alexmiller17:01:11

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

mfikes17:01:55

FWIW, the above discussion prompted me to write about this problem domain in ClojureScript http://blog.fikesfarm.com/posts/2016-01-15-clojurescript-head-holding.html

ratnakar17:01:28

Thanks @agile_geek , that helps.

agile_geek18:01:50

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

agile_geek18:01:29

@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.

agile_geek18:01:20

@ratnakar gr8 pleased it helped.

mfikes18:01:29

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

agile_geek18:01:12

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

seancorfield20:01:44

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

alexmiller20:01:57

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

mfikes20:01:42

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

alexmiller20:01:10

shouldn't be hard :)

jcomplex21:01:38

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

jonahbenton22:01:08

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

jonahbenton22:01:14

if the service provides responses in JSON format, cheshire is a good parsing tool: https://github.com/dakrone/cheshire

vanrysss22:01:33

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