clojure cycles returning nil in map-indexed -
i trying write cycle based fizz buzz in clojure. seems work values not fizz or buzz, values fizz , buzz returns nil.
code:
(ns fizz-buzz.core (:gen-class)) (defn fizz-buzz [value] (let [fizz (cycle ["" "" "fizz"]) buzz (cycle ["" "" "" "" "buzz"]) fb (map str fizz buzz)] (nth (map-indexed (fn [i v] (if (clojure.string/blank? v) (str (+ 1) v))) fb) (- value 1)))
tests:
(ns fizz-buzz.core-test (:require [clojure.test :refer :all] [fizz-buzz.core :refer :all])) (deftest value-2-will-return-2 (testing "2 return string 2" (is (= "2" (fizz-buzz 2))))) (deftest value-4-will-return-4 (testing "4 return string 4" (is (= "4" (fizz-buzz 4))))) (deftest value-3-will-return-fizz (testing "3 return string fizz" (is (= "fizz" (fizz-buzz 3))))) (deftest value-5-will-return-buzz (testing "5 return string buzz" (is (= "buzz" (fizz-buzz 5))))
the first 2 tests work (2 , 4), fizz , buzz test not work. sure not understand how map-indexed works.
you have slight bracket misplacement in if
statement. means have no else
case if
(hence nil
result)
try rewriting:
(if (clojure.string/blank? v) (str (+ 1) v)))
as:
(if (clojure.string/blank? v) (str (+ 1)) v))
ps - looks you've understood map-indexed fine :)
Comments
Post a Comment