Fork me on GitHub
#beginners
<
2016-11-19
>
agile_geek13:11:19

@roelofw how did you get on with that problem? There are number of ways of thinking about it but remember you are consuming a vector and producing a map so think about what assoc'ing into a vector might do? Two ways to solve this might be (without giving away the answers) 1. Start with an empty map and add key-value pairs to it - i.e. accumulate values in a map that starts empty- think about what fn might accumulate values 2. Create a sequence of key-value pair from the vector of keys and then change the sequence into a map

agile_geek14:11:48

There's several variations on 2. as well including a handy fn that creates a map from a sequence of keys and vals.

roelofw16:11:08

@agile_geek I solved that problem with zipmap after some trying

agile_geek16:11:54

Yep zipmap is that handy fn I mentioned.

roelofw17:11:09

IM now trying to count a seq and Im not allowed to use count

roelofw17:11:45

I thinking about reduce and a anymous function

roelofw17:11:59

@agile_geek Am I right that it is reduce <beginvalue> <function> ?

agile_geek17:11:07

(reduce <fn> <initial-value> <collection>)

agile_geek17:11:35

Where fn takes two args [acc value]

agile_geek17:11:30

acc is the accumulated value and value is the latest value from the collection

agile_geek17:11:55

acc starts as the initial-value and if the initial-value is not supplied it's the first value of the collection

roelofw17:11:05

I think I can make a sort of count now without using count

roelofw17:11:19

@agile_geek thanks

drewverlee18:11:04

this seems like an easy one: {:a [1 2 ]} => [[:a 1] [:a 2]]

roelofw18:11:10

Yep, it is

roelofw18:11:20

I have solved a simular question

roelofw18:11:44

@agile_geek : this works :

(fn [s] (reduce (fn [n _] (inc n)) 0 s)) 

roelofw18:11:03

@drewverlee do you want a hint ?

drewverlee18:11:21

@roelofw im leaning towards needing to map twice. Once for each key and then again for each value. But that is breaking my circuit somehow.

roelofw18:11:19

As hint : look at : zipmap and repeat

agile_geek18:11:21

You can do that one with reduce too

agile_geek18:11:34

Although it's probably a bit convoluted

drewverlee18:11:29

are keys and vals ordered (they always return the same thing?)

roelofw18:11:59

@drewverlee already solved the puzzle?

drewverlee18:11:37

not yet, im getting close though. I had to draw out the intermediate states.

roelofw18:11:43

oke, I hope you see the answer

drewverlee19:11:30

(defn foo [x]
  (map  (comp #(apply zipmap %) #(conj [(repeat (count (val %)) (key %) )] (val %))))
  x)

drewverlee19:11:54

{:a [1 2], :b [3 4]}

drewverlee19:11:33

maybe my i outlined the wrong problem. What i actual need is: {:a [{:x 1 :z 2}] => [{:x 1 :z 2 :new :a}]

roelofw19:11:08

oke, and where do the :new come from ?

roelofw19:11:38

@drewverlee im still confused what you are trying to do

drewverlee19:11:09

new is a key ill have to supply to the function.

drewverlee19:11:34

i realized i hadn’t simplified the problem correctly.

roelofw19:11:59

Can happen

drewverlee20:11:36

(let [ds {:x [{:e 1} {:e 2}]
          :y [{:e 5} {:e 5}]
          }
      m (fn [nk k a] (map #(assoc % nk k) a))
      ]
  (map #(m :kind (first %) (last %))
       ds))


(({:e 1, :kind :x} {:e 2, :kind :x}) ({:e 5, :kind :y} {:e 5, :kind :y}))

drewverlee20:11:44

that seems awfuly convoluted

roelofw20:11:44

sorry, this is out of my lequa .

roelofw20:11:44

When I do this in 4clojure (fn [s] (filter odd? s)) I see this error message

Unable to resolve symbol: fn  in this context, compiling:(NO_SOURCE_PATH:0) 

drewverlee20:11:19

4clojure might have some restrictions on using anonymous functions? maybe try #(filter odd? %)

roelofw21:11:09

Sorry I do not work. Now I see a message that # cannot be resolved

jswart21:11:42

@drewverlee

(for [k (keys data)] 
  (map (fn [datum] (assoc datum :kind k)) 
           (get data k)))

roelofw21:11:42

The challenge looks like this :

(= (__ #{1 2 3 4 5}) '(1 3 5)) 

jswart21:11:34

could be done with a single pass through the data with reduce if you are doing a very big set of data

roelofw21:11:17

at some way I have to use filter odd? I think

jswart21:11:33

@roelofw here is a hint: partial

jswart21:11:14

of course thats only one way, 4clojure is like anything… more than one answer

roelofw21:11:41

oke, I have to think. I thought partial is used for functions that a partially executed

roelofw21:11:50

and mine is not I think

jswart21:11:04

I think you should read up on what partial does

jswart21:11:13

(doc partial) in your repl

jswart21:11:44

try ((partial + 1) 4)

jswart21:11:48

what does that give you?

jswart21:11:03

remember that + is a function in clojure

roelofw21:11:54

@jswart I will do ((partial + 1) 4) gives 5

jswart21:11:30

now think about your problem and what you know about partial now

roelofw21:11:13

I will do thanks

roelofw21:11:24

wierd, I now do the same

(fn [s] (filter odd? s))  
and now it works

jswart21:11:56

Instead of wrapping it in a function, use partial is what I was hinting at.

jswart21:11:57

((partial + 1) 4) == ((fn [x] (+ 1 x) 4)

roelofw21:11:01

so something like

(partial  filter odd ? %) 
?

jswart21:11:18

% is only used when using anonymous functions

jswart21:11:31

(fn [x] x) == #(%)

jswart21:11:01

only that doesn’t really work

roelofw21:11:02

oke, second attempt ((partial filter odd?) s)

jswart21:11:12

why do you need s?

jswart21:11:15

what is s?

jswart21:11:29

Do you know javascript?

roelofw21:11:12

s is the seq that the user can given in

jswart21:11:22

ah okay, so yes that would work

roelofw21:11:29

I know a very little javascript

jswart21:11:46

What language do you most use?

jswart21:11:15

I was going to say if you did, then a very simplified and not totally correct version of partial is:

jswart21:11:33

function partial(fn, arg) {
	return function(arg2) {
		return fn(arg, arg2);
	}
}

jswart21:11:50

only partial actually works with any arity, but this is just to demonstrate the intuitive idea

roelofw21:11:10

I m coming from ruby and did a very little haskell

jswart21:11:45

I see, well I don’t really know any ruby but the above is fairly simple I think.

jswart21:11:58

So you can use what you did above with partial to solve your 4clojure problem

jswart21:11:43

(partial filter odd?) is the answer I would have used

roelofw21:11:47

is using partial better then using anymous function with (fn )

jswart21:11:53

There is no “better”, but partial has meaning where an anonymous function doesn’t. Using partial is telling the reader of the code that: I’m going to partially apply this function to create a new function that will take the rest of the information.

jswart21:11:35

Partial can be used to make a very general function more specific. So you can use partial to create additional functions with some of the values already specified.

jswart22:11:14

(let [add5 (partial + 5), add10 (partial + 10)] (+ (add5 5) (add10 10)))

jswart22:11:06

These examples are contrived, but it is useful in practice too.

roelofw22:11:56

oke, thanks

roelofw22:11:03

Time to sleep now

drewverlee23:11:46

man this transformation should be easier then i’m making it: (??? [{:a 1} {:b 2}] [:new-key1 :new-key2])=>` [{:new-key :a :new-key2 1} {:new-key :b :new-key2 2}]`

dorianc.b23:11:16

Quick question, how do I get the repl to recognize functions declared with defn? I’m currently doing the clojure koans and I keep getting the cannot resolve symbol error