This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2015-11-22
Channels
- # beginners (122)
- # boot (217)
- # cider (14)
- # cljs-dev (74)
- # cljsrn (22)
- # clojure (101)
- # clojure-nl (4)
- # clojure-russia (22)
- # clojure-taiwan (5)
- # clojurescript (87)
- # cursive (4)
- # datavis (2)
- # editors (3)
- # hoplon (2)
- # jobs (2)
- # ldnclj (1)
- # lein-figwheel (1)
- # luminus (1)
- # off-topic (1)
- # om (105)
- # onyx (37)
- # reagent (2)
- # spacemacs (2)
what is wrong here : (= "longest" (reduce (fn [a b] (if (< a b) b a)) ["which" "word" "is" "longest"]))
@roelof: if you try to run that in a repl it should give you a reasonably helpful error message.
@roelof: it seems, from some of the issues you have, that you're not using Light Table's major selling point, which is the instarepl - it shows you values and errors inline, which should help with some of the problems you're facing.
@roelof: idk if you've solved it yet, but your fn parens are wrong. You need another close paren after the if
(< “a” “b”) => ClassCastException java.lang.String cannot be cast to java.lang.Number http://clojure.lang.Numbers.lt (Numbers.java:221)
"Multimethods allow more complex dispatching" (= "Bambi eats veggies." (diet {:species "deer" :name "Bambi" :age 1 :eater :herbivore}))
and then different function bodies are invoked depending on the return value of the dispatch-fn
(defmulti diet (fn [x] (:eater x))) (defmethod diet :herbivore [a] "veggies") (defmethod diet :carnivore [a] "animals") (defmethod diet :default [a] "I don't know what a eats")
when you call a multimethod, it’ll call the dispatch-fn
, and then call the method that matches the result
so it’ll call (:eater m)
, which’ll return :herbivore
, and will then call the version of diet
that corresponds to :herbivore
(defmulti diet (fn [x] (:eater x))) (defmethod diet :herbivore [a] (str ( (:name x) " " "eats veggies.")))
I have changed x in a but then I see this message : java.lang.ClassCastException: java.lang.String cannot be cast to clojure.lang.IFn