This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
- # aatree (10)
- # beginners (59)
- # boot (314)
- # braveandtrue (4)
- # cider (50)
- # cljs-dev (12)
- # cljsrn (6)
- # clojure (206)
- # clojure-austin (2)
- # clojure-gamedev (90)
- # clojure-japan (1)
- # clojure-poland (12)
- # clojure-russia (10)
- # clojure-sg (1)
- # clojurescript (86)
- # core-async (2)
- # core-matrix (3)
- # cursive (40)
- # datomic (2)
- # dirac (13)
- # editors (25)
- # emacs (7)
- # hoplon (2)
- # immutant (10)
- # jobs (24)
- # jobs-discuss (1)
- # ldnclj (8)
- # lein-figwheel (19)
- # leiningen (1)
- # mount (7)
- # off-topic (34)
- # om (147)
- # onyx (11)
- # parinfer (151)
- # pedestal (2)
- # re-frame (31)
- # reagent (13)
- # ring-swagger (7)
- # spacemacs (1)
- # yada (11)
oh sorry, i mean insert into a hashmap, well what i am doing is creating a hashmap to store words and how frequently they appear in a text
currently im doing it by merging hashmaps but i was wondering if there was a better way
Remember that data structures in Clojure are immutable...
(assoc my-map "cat" 4) will return a new hash map with that key / value added.
I suspect you want to increment counts of words as you match them? So you probably want to look at
while is intended for repeating side-effects... so it's very rarely used in Clojure.
The docstring for
while -- if you type
(doc while) into the REPL -- says
Repeatedly executes body while test expression is true. Presumes some side-effect will cause test to become false/nil. Returns nil
It doesn't rebind anything. It requires mutable state. Hence not used much in Clojure.
In general, Clojure code will use
reduce for most things that you would use loops for in other languages.
Since you have a sequence of words, and you want a hash map of words => counts, that sounds like a
reduce operation to me.
For your problem, you'll start with an empty hash map (no word counts) and the sequence of words.
reduce works by taking an initial value and a sequence of things, and repeats a "step" function using each value in the sequence to produce new versions of the initial value.
so it will have
1 for the first step...
x isn't even so it'll just return
then it will be called with that result and
2 -- which is even so it will return
(+ v x) which is
(defn add-evens-to [s n] (if (empty? s) n (add-evens-to (rest s) (if (even? (first s)) (+ n (first s)) n)))) ;; something like that?
(it takes me much longer to figure out the recursive function version than the
Added it to lein dependencies and required it, but
js/d3 seems not to exist, and the browser console says d3 is not defined.
oh, I see now there is an
SyntaxError: illegal character error when loading d3.inc.js
gotcha. well, the error is likely around the use of this on line 9. you want to use a different symbol there.
but when i put it in the repl i seejava.lang.SecurityException: You tripped the alarm! class clojure.lang.Compiler is bad!
@camille: Fun background reading on protocols and records - http://www.braveclojure.com/multimethods-records-protocols/