Fork me on GitHub
#beginners
<
2016-02-25
>
jack_liang05:02:06

question, is there a way to insert a hashmap other than to merge 2 hashmaps?

seancorfield05:02:37

Can you be a bit more specific about what you're trying to do @jack_liang ?

jack_liang05:02:23

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

jack_liang05:02:50

like have empty hashmap and insert key = "cat" value =4

jack_liang05:02:25

currently im doing it by merging hashmaps but i was wondering if there was a better way

seancorfield05:02:27

Remember that data structures in Clojure are immutable... (assoc my-map "cat" 4) will return a new hash map with that key / value added.

seancorfield05:02:00

I suspect you want to increment counts of words as you match them? So you probably want to look at update...

jack_liang05:02:51

would i have to bind (assoc my-map "cat" 4)`to my-map?

jack_liang05:02:18

sorry, immutable..of course i would

seancorfield05:02:33

In a loop / recur you mean?

jack_liang05:02:18

im prone to using while atm but its the same idea i guess

seancorfield05:02:20

while is intended for repeating side-effects... so it's very rarely used in Clojure.

jack_liang05:02:02

can you explain what you mean by repeating side-effects?

seancorfield05:02:46

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

seancorfield05:02:24

It doesn't rebind anything. It requires mutable state. Hence not used much in Clojure.

jack_liang05:02:46

and thats because most of clojure data structures are immutable, correct?

seancorfield05:02:15

In general, Clojure code will use map / filter / reduce for most things that you would use loops for in other languages.

seancorfield05:02:50

Since you have a sequence of words, and you want a hash map of words => counts, that sounds like a reduce operation to me.

jack_liang05:02:47

im actually having a hard time understanding how to use reduce

seancorfield05:02:51

For your problem, you'll start with an empty hash map (no word counts) and the sequence of words.

seancorfield05:02:06

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.

seancorfield05:02:44

(reduce (fn [v x] (if (even? x) (+ v x) v)) 0 [1 2 3 4 5])

seancorfield05:02:57

starts with 0 and the sequence 1 2 3 4 5

seancorfield05:02:13

the "step" function gets the current value and each element in turn

seancorfield05:02:51

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`)

seancorfield05:02:31

then it will be called with that result and 2 -- which is even so it will return (+ v x) which is 2

seancorfield05:02:45

then it will be called with 2 and 3 -- and return 2

seancorfield05:02:02

then with 2 and 4 -- and will return 6

seancorfield05:02:08

Does that help?

seancorfield05:02:55

(and finally with 6 and 5, returning 6)

jack_liang05:02:13

so in a sense, its almost like recursion ?

jack_liang05:02:54

or is that a bad way of saying it

jack_liang05:02:01

but i get the general explanation

seancorfield05:02:12

You could write it as a recursive function

seancorfield05:02:02

(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?

seancorfield05:02:21

(add-evens-to [1 2 3 4 5] 0)

jack_liang05:02:36

its alright, i get the general sense of how to use reduce

seancorfield05:02:56

(it takes me much longer to figure out the recursive function version than the reduce version)

polymeris19:02:50

Added it to lein dependencies and required it, but js/d3 seems not to exist, and the browser console says d3 is not defined.

polymeris19:02:51

Am I missing something?

polymeris20:02:47

oh, I see now there is an SyntaxError: illegal character error when loading d3.inc.js

polymeris20:02:04

adding <meta charset="utf-8"/> solved it simple_smile

jonahbenton22:02:48

hey @camille- what error are you getting?

camille22:02:08

well codewars errors are not very verbose

camille22:02:15

it’s just like “Wrong” or “correct”

jonahbenton22:02:17

gotcha. well, the error is likely around the use of this on line 9. you want to use a different symbol there.

camille22:02:31

but when i put it in the repl i seejava.lang.SecurityException: You tripped the alarm! class clojure.lang.Compiler is bad!

camille22:02:43

you mean using Str?

camille22:02:54

@shaun-mahood: yeah that’s the book i’ve been working my way through

camille22:02:11

@jonahbenton: hmm now i am getting a server error

camille22:02:21

meh, can’t make heads or tails of it

camille22:02:00

nevermind, i looked again and codewars is set up to keep the rookies out!