This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-02-25
Channels
- # 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)
question, is there a way to insert a hashmap other than to merge 2 hashmaps?
Can you be a bit more specific about what you're trying to do @jack_liang ?
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
like have empty hashmap and insert key = "cat" value =4
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 update
...
would i have to bind (assoc my-map "cat" 4)`to my-map?
sorry, immutable..of course i would
In a loop
/ recur
you mean?
im prone to using while atm but its the same idea i guess
while
is intended for repeating side-effects... so it's very rarely used in Clojure.
can you explain what you mean by repeating side-effects?
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.
and thats because most of clojure data structures are immutable, correct?
Correct.
In general, Clojure code will use map
/ filter
/ 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.
im actually having a hard time understanding how to use reduce
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.
(reduce (fn [v x] (if (even? x) (+ v x) v)) 0 [1 2 3 4 5])
starts with 0
and the sequence 1 2 3 4 5
the "step" function gets the current value and each element in turn
so it will have v
as 0
and x
as 1
for the first step... x
isn't even so it'll just return v
(`0`)
then it will be called with that result and 2
-- which is even so it will return (+ v x)
which is 2
then it will be called with 2
and 3
-- and return 2
then with 2
and 4
-- and will return 6
Does that help?
(and finally with 6
and 5
, returning 6
)
so in a sense, its almost like recursion ?
or is that a bad way of saying it
but i get the general explanation
You could write it as a recursive function
(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?
(add-evens-to [1 2 3 4 5] 0)
its alright, i get the general sense of how to use reduce
(it takes me much longer to figure out the recursive function version than the reduce
version)
Hi. Trying to use https://github.com/cljsjs/packages/tree/master/d3
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
hey @camille- what error are you getting?
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/
@shaun-mahood: yeah that’s the book i’ve been working my way through
@jonahbenton: hmm now i am getting a server error