Fork me on GitHub
#beginners
<
2015-12-13
>
roelof12:12:16

Why do I get this error message : CompilerException java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IFn

roelof12:12:38

on this code

(defn number_to_digits
  [number]
  (loop
    [n number
     acc ()]
    (if (= ( number 0))
      acc
      (recur (conj acc (rem n 10)) (n mod 10)  )
    )
  )
)  

meikemertsch12:12:51

Because you use number as a function

roelof12:12:24

oke, you mean in the if

meikemertsch12:12:28

(number 0) is trying to do that. Don't get sloppy with your parens again 😉

roelof12:12:16

nice to see you, how do you do ?

meikemertsch12:12:40

= is a function. Great, it may stand as first item in a list. Number is not so it has no permissions there

meikemertsch12:12:12

I'm great. A lot to do, new company, new work, less clojure but still a lot of fun

roelof12:12:24

oke, I changed it and see this error : CompilerException java.lang.ClassCastException: clojure.lang.PersistentList cannot be cast to java.lang.Number

roelof12:12:46

defn number_to_digits
  [number]
  (loop
    [n number
     acc ()]
    (if (=  number 0)
      acc
      (recur (conj acc (rem n 10)) (mod n 10)  )
    )
  )
)  

meikemertsch12:12:30

Look at the arguments you give recyt

meikemertsch12:12:06

What are they when you define them and what are they when you actually give it to recur?

roelof12:12:06

(rem n 10) is a number I want to add to acc that holds the remainder of number / 10

roelof12:12:30

(mod n 10) is a number that schould be the outcome of number / 10

meikemertsch12:12:22

Tip: you're effectively calling (mod () 10) when the error occurs. I can understand why your repl is complaining

roelof12:12:58

he, why is n a empty list , acc schould be a empty list. N schould be a number

meikemertsch12:12:06

In which order do you give stuff to recur?

roelof12:12:08

I see it mixed up the variables

meikemertsch12:12:56

Need to fix lunch now. Have fun

roelof13:12:25

chips now a endless loop I think :

(defn number_to_digits
  [number]
  (loop
    [n number
     acc ()]
    (if (= number 0)
      acc
      (recur (mod n 10) (conj acc (rem n 10)) )
    )
  )
) 

roelof13:12:49

I already changed number in n in the if

roelof13:12:56

but still no luck

codonnell13:12:56

You could add (println "n: " n "acc: " acc) above your if so you can see what's going on.

roelof13:12:06

oke, it seems that the same number is added all the time

roelof13:12:00

n stays 1 and acc will become all bigger and bigger with only 1

roelof13:12:07

oke, i see rem and mod are the same

roelof13:12:42

back to the cheat sheet to see which function I need to calculate the outcome of a /

roelof13:12:41

@mccraigmccraig: thanks, this is also working

(defn number_to_digits
  [number]
  (loop
    [n number
     acc ()]
    (if (= n 0)
      acc
      (recur (quot n 10) (conj acc (rem n 10)) )
      )
    )
  ) 

roelof14:12:46

How can this one be refractored

roelof14:12:55

(defn number_to_digits
  [n1 n2]
  (let [outcome (* n1 n2)]
 (loop
    [n outcome
     acc ()]
    (if (= n 0)
      acc
      (recur (quot n 10) (conj acc (rem n 10)) )
      )
    )
  ))

val_waeselynck15:12:06

@roelof off the top of my head (->> n (iterate #(quot % 10)) (take-while #(not (zero? %))) (map #(rem % 10)) reverse)

val_waeselynck15:12:58

(well, only the loop part)

roelof15:12:47

@val_waeselynck: thanks, What does the ->> do, I have not learned that

val_waeselynck15:12:51

it's the thread-last macro. It basically rewrites the above code as (reverse (map #(rem % 10) (take-while #(not (zero? %) (iterate #(quot % 10) n)))))

val_waeselynck15:12:31

@roelof: maybe you have not learned about macros yet?

roelof15:12:32

a little , I have learned to make a macro which convert and apply things like this ( 2 + 3) to ( + 2 3) and outputs then 5

val_waeselynck15:12:35

The threading macros are among those I use most often, ->> for transforming collections and -> for transforming maps typically

val_waeselynck15:12:41

they let you write successive function calls left to right instead of inside out, which is quite nice.

roelof16:12:03

oke, thanks for the lesson

roelof18:12:02

if im right , the cartesian product of two sets is the first one from set one with the first one of set2

roelof18:12:23

the first one with the second one of set2 and so on ?

codonnell19:12:50

@roelof: yes, that's right. If you had two sets, A = {1,2} and B = {3,4}, then the cartesian product A x B would have {(1,3), (1,4), (2,3), (2,4)} as its elements.

roelof19:12:21

@codonnell: then I have to think hard how to do something like that Maybe use the idea of a earlier exercise simpler closures

roelof19:12:46

thanks for the help

codonnell19:12:31

@roelof: play around a bit with for