About code style, don't do "def"s inside of a function - this binds them inside the entire namespace, so your values are escaping and persisting when you just want locals. Use "let":

(defn averaged [x]
  (let [summed (reduce + x)
         counted (count x)]
    (/ summed counted)))

That function actually does work, by the way. I suspect that farther up in your lighttable/repl session you bound "avged" to a number, like (def avged 10).
Stephen Feyrer <mailto:stephen.fey...@gmail.com>
July 9, 2014 at 6:48 PM
Hi,

I tried to create the function below in a Lighttable instarepl. In lieu of any better idea for formatting, the < > statements below indicate instarepl output.

(defn avged ([x]
((def sumed (reduce + x)) < 10 >
(def counted (count x)) < 4 >
(def result (/ sumed counted)) < 5/2 >
result
)))

(avged [1 2 3 4]) < java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IFn
Var.java:392 clojure.lang.Var.fn
Var.java:423 clojure.lang.Var.invoke
(Unknown Source) user/avged >

The objective of this function is just a learning exercise.

I have three questions:

1. Why doesn't it work, return a value?

2. What does the error message mean? and seeing this or similar again, how do I investigate to get a meaningful Clojure solution?

3.  Code Style, what can I do to improve readability and form?


Thanks.

--
Stephen Feyrer.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com <mailto:clojure+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

--
Sam Ritchie (@sritchie)
Paddleguru Co-Founder
703.863.8561
www.paddleguru.com <http://www.paddleguru.com/>
Twitter <http://twitter.com/paddleguru>// Facebook <http://facebook.com/paddleguru>

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to