Clojurians
#beginners
<
2016-02-28
>

This page is not created by, affiliated with, or supported by Slack Technologies, Inc.

danlucraft10:02:45

What's going on here on 4ojure? I tried this code in Replete so it should be working...

mfikes12:02:29

@danlucraft: I’d try that in a Clojure REPL. ClojureScript can let arity errors slip by. A small example: (inc 2 3) in ClojureScript vs. Clojure.

danlucraft13:02:09

@mfikes: interesting, thanks. Can't see where I might be messing up though

mfikes13:02:29

@danlucraft: Paste code or a gist. :simple_smile: Too hard to experiment with an image of code :simple_smile:

danlucraft14:02:49

@mfikes yep sorry! I think trying to do this on iOS is a fools errand really...

danlucraft14:02:55

Replete is nice.

danlucraft14:02:11

If it had a persistent history I could go and grab that code right now :wink:

mfikes14:02:14

Hah, yeah, that’s a tough one to solve because of JavaScriptCore state :simple_smile:

mfikes14:02:34

@danlucraft: I think this is the same code

(fn [n]
  (->> (range 1 n)
    (reduce #(let [[a b r] %1
                   c (+ a b)]
              [b c (conj r c)])
      [0 1 [1]])
    (last)
    (into '())
    (reverse)))

mfikes14:02:08

Ahh, so your reduce function takes one argument.

mfikes14:02:38

@danlucraft: ClojureScript isn’t telling you this, while Clojure is.

mfikes14:02:50

@danlucraft: using fn and saving some keystrokes elsewhere in the form:

(fn [n]
  (->> (range 1 n)
    (reduce (fn [x _]
              (let [[a b r] x
                    c (+ a b)]
                [b c (conj r c)]))
      [0 1 [1]])
    last
    (into ())
    reverse))
This way, the reduce function has arity 2

danlucraft15:02:22

Ahh so if I'd used %1 and %2 it would have defined an arity 2 function and worked?

danlucraft15:02:09

So in ClojureScript the arity must not be checked then, because you can pass extra arts to any JS function and they are ignored

mfikes15:02:01

@danlucraft: In ClojureScript, sometimes arity causes an error, sometimes it causes a warning. In some bootstrapped implementations, which are new with respect to everything, sometimes you may not get a warning. But, yes, oftentimes things will just work anyway owing to the JavaScript being a bit forgiving.

mfikes15:02:56

@danlucraft: It is definitely not the case that ClojureScript has no arity checking.

mfikes15:02:43

@danlucraft: Sorry, yes, adding a %2 as in the following is also a valid way to address the issue. No need to fn.

(fn [n]
  (->> (range 1 n)
    (reduce #(let [[a b r] %1
                   c (+ a b)
                   _ %2]
              [b c (conj r c)])
      [0 1 [1]])
    (last)
    (into '())
    (reverse)))

danlucraft16:02:02

got it. Thanks for your help @mfikes I'll do this with more sensible tools next time :blush: