Ad
Code
Diff
  • (ns fibonacci)
    
    (defn get-fibonacci-number [index]
      (cond (< index 10) (nth '(0 1 1 2 3 5 8 13 21 34) index 0)
            (odd? index) (let [k   (quot index 2)
                               fk  (get-fibonacci-number k)
                               fk1 (get-fibonacci-number (inc k))]
                           (+' (*' fk fk) (*' fk1 fk1))
                         )
            :else        (let [k   (quot index 2)
                               fk  (get-fibonacci-number k)
                               fk1 (get-fibonacci-number (inc k))]
                           (*' fk (-' (*' 2 fk1) fk))
                         )
      )
    )
    • (ns fibonacci)
    • (defn get-fibonacci-number [index]
    • "Finds fibonacci number by given index using iteration"
    • (loop [x 0 y 1 z index]
    • (if (< z 1) x (recur y (+ x y) (dec z)))))
    • (cond (< index 10) (nth '(0 1 1 2 3 5 8 13 21 34) index 0)
    • (odd? index) (let [k (quot index 2)
    • fk (get-fibonacci-number k)
    • fk1 (get-fibonacci-number (inc k))]
    • (+' (*' fk fk) (*' fk1 fk1))
    • )
    • :else (let [k (quot index 2)
    • fk (get-fibonacci-number k)
    • fk1 (get-fibonacci-number (inc k))]
    • (*' fk (-' (*' 2 fk1) fk))
    • )
    • )
    • )
Code
Diff
  • (ns fizzbuzz.core)
    
    (defn fizz [_] "Fizz")
    
    (defn buzz [_] "Buzz")
    
    (defn fizz-buzz [_] "FizzBuzz")
    
    (defn self [x] x)
    
    (defn fizzbuzz [x]
      (map (comp eval list) 
        (cycle (list self self fizz self buzz fizz self self fizz buzz self fizz self self fizz-buzz)) 
        (range 1 (inc x))
      )
    )
    
    • (ns fizzbuzz.core)
    • (defn divisible-by? [divisor number]
    • (zero? (mod number divisor)))
    • (defn fizz [_] "Fizz")
    • (defn buzz [_] "Buzz")
    • (defn fizz-buzz [_] "FizzBuzz")
    • (defn self [x] x)
    • (defn fizzbuzz [x]
    • (map #(cond
    • (divisible-by? 15 %) "FizzBuzz"
    • (divisible-by? 5 %) "Buzz"
    • (divisible-by? 3 %) "Fizz"
    • :else %)
    • (range 1 (inc x))))
    • (map (comp eval list)
    • (cycle (list self self fizz self buzz fizz self self fizz buzz self fizz self self fizz-buzz))
    • (range 1 (inc x))
    • )
    • )