This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-01-15
Channels
- # admin-announcements (130)
- # alternate-reality (2)
- # aws (20)
- # beginners (49)
- # boot (1)
- # braid-chat (18)
- # cljsrn (54)
- # clojars (1)
- # clojure (70)
- # clojure-art (1)
- # clojure-japan (21)
- # clojure-miami (2)
- # clojure-my (7)
- # clojure-russia (60)
- # clojurescript (75)
- # community-development (12)
- # core-matrix (7)
- # cursive (23)
- # datomic (31)
- # dirac (2)
- # dunaj (3)
- # dysphemism (5)
- # editors-rus (1)
- # emacs (22)
- # events (9)
- # funcool (56)
- # hoplon (63)
- # human (1)
- # jobs (9)
- # ldnclj (7)
- # lein-figwheel (21)
- # leiningen (1)
- # off-topic (2)
- # om (61)
- # onyx (20)
- # other-lisps (2)
- # portland-or (1)
- # proton (26)
- # re-frame (27)
- # reagent (16)
- # ring-swagger (30)
- # spacemacs (6)
- # yada (5)
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
Thanks @kamn , I see, but in the above code, how does reduce get invoked with 2 params?
np. It still gets me because for whatever reason when I see reduce I think map first then I realize there is a remainder
(->> 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 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 exceptionI 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. ?
exactly
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 http://stuartsierra.com/2015/08/25/clojure-donts-lazy-effects
@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
Precisely!
This is the classic symptom of laziness over a side-effect producing fn
@mfikes: probably
as it runs reduce it would certainly realise the seq. Not sure what the advantages are over doseq
? Anyone 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
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 http://blog.fikesfarm.com/posts/2016-01-15-clojurescript-head-holding.html
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.
@seancorfield that's a bug - it's fixed in 1.8
shouldn't be hard :)
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
if the service provides responses in JSON format, cheshire is a good parsing tool: https://github.com/dakrone/cheshire
Ah yes, so it is… https://clojurians.slack.com/archives/beginners/p1452891177006026