Fork me on GitHub
#beginners
<
2015-12-15
>
roelof06:12:46

someone who give me a tip for this challenge : http://www.4clojure.com/problem/63

seancorfield07:12:42

@roelof: Off the top of my head I’d say you want a reduce with an update-in and fnil...

seancorfield07:12:17

reduce because you’re transforming a sequence to a potentially shorter sequence, update-in because you’ll be updating entries in a map data structure as you reduce the sequence, and fnil because if the map entry is nil, you’ll want to substitute [] before you conj onto it.

seancorfield07:12:32

Is that enough of a tip? Too much?

roelof07:12:47

I think thats enough

roelof07:12:33

The part I do not see is one the first one how I can make a true / false part and on a calculating I see a 1/2 2/3

roelof07:12:24

its looks to me I have to find a way I can check what the functions returns a boolean or a number

seancorfield07:12:35

What is the function provided in that first problem? How does it apply to elements of the sequence provided?

seancorfield07:12:27

(there is also the hint that you cannot use group-by therefore you might assume you are being asked to implement group-by simple_smile )

roelof07:12:34

the first one the function is #(> % 5)

roelof07:12:53

the second one is #(apply / %)

seancorfield07:12:09

So that first function is true if its argument is greater than five, false if its argument is five or less.

roelof07:12:10

the 3rd one is just count

seancorfield07:12:22

The second function returns a long, given a long.

seancorfield07:12:31

The third function return a long, given a sequence.

roelof07:12:54

Yes, I know but if I use a if then and only check for true/false then the others will not work

seancorfield07:12:03

You’re overthinking.

seancorfield07:12:19

Just apply the function to each element of the sequence. What do you get in each case?

roelof07:12:40

That Always my problem , Overthinking a problem

roelof07:12:54

first case true/ false . Second one I doubt. I do not fully understand what #(apply / %) does but I think a number and the 3rd one a number

seancorfield07:12:02

This is one of those cases where types would be very, very helpful. You’re asked to write a function that takes (A->B) and [A] and returns {B : [A]}

seancorfield07:12:47

(apply / [2 3 4]) is (/ 2 3 4) which is 2/3/4

roelof07:12:03

oke, thanks

roelof07:12:25

but clojure has no types

seancorfield07:12:49

I know. But the types give you the generic shape of the result — for any input types.

seancorfield07:12:11

In the 1st case you have (long -> bool) and [long] so you get {bool : [long]}

roelof07:12:33

I know types a little when I tried Haskell for some days . I fight all the times against them

seancorfield07:12:11

In the 2nd case you have ([num] -> num) and [[num]] so you get {num : [[num]]}

roelof07:12:15

oke, so I have a function which inputs is a number. The functions output is a boolean and I want to represent it as a boolean and a list of numbers

seancorfield07:12:40

In the 3rd case you have ([A] -> long) and [[A]] so you get {long : [[A]]}

roelof07:12:59

oke, that way

roelof07:12:26

In the second case you put all the numbers with 0.5 and 0.6666 on one []

roelof07:12:46

so the first part is the outcome of the function

seancorfield07:12:36

Although they are ratios 1/2 and 2/3

seancorfield07:12:19

But the types give you the hint: the results of the function you are passed (applied to each element of the sequence you are passed) are the keys of the map you produce.

roelof07:12:43

oke, I will think about this and do some experiments

roelof07:12:30

That was what I meant to say with "the first part is the outcome of the function"

roelof07:12:35

thanks as I said earlier. Time to do some experiments and small steps to solve this one

seancorfield07:12:50

When you solve it and look at the solutions, amalloy’s is very nice!

roelof07:12:33

okle, I will add him/her to my list

seancorfield07:12:33

Bed time here. Good luck!

roelof07:12:25

Good night. Here the day just begin. Its here 8:41 in the morning

roelof07:12:34

and thanks for the tips

polymeris19:12:46

Hi... any suggestions on how to "transpose" a list of maps? I.e convert this '({:a 10 :b 20} {:a 50 :b 75}) into this {:a '(10 50) :b '(20 75)}

polymeris19:12:01

oh... merge-with does the trick, and even the next step I needed (to sum the lists), all in one step

polymeris19:12:19

(apply merge-with + '({:a 10 :b 20} {:a 50 :b 75}))

mostr19:12:39

@polymeris: I’m just curious, why quoted list and not vector? is there any difference here?

mostr19:12:46

REPL gives same results for both

polymeris19:12:23

any sequence should work, I think

polymeris19:12:09

I just had a list to work with