Rebel readline will highlight namespace and function names (also has completion, built-in docs, etc) I find rebel readline especially useful to run webapps and other longer running processes (or just want to quick experiment) There is a demo on this page https://practical.li/clojure/clojure-cli/repl/
rebel-readline is a crazy good project!
I just implemented fibbonanci in the weirdest way lol
(defn add-prev-next [array]
(def value-end (reduce + (
if (=(count array) 1)
(take-last 1 array)
(take-last 2 array))))
(concat array (list value-end) )
)
(
defn generate-numbers [collection numbers-to-generate]
(loop [x numbers-to-generate collection collection]
(if (= x 0)
collection
(recur (- x 1) (add-prev-next collection)))
)
)
(defn fibonnaci-sequence [x]
(if (= x 0)
'()
(generate-numbers '(1) x)
)
)
(fibonnaci-sequence 8)def inside of defn is not really idiomatic, I like your idea though! 🙂
---
Boils down to something like:
(-> (iterate #(conj % (apply + (take-last 2 %))) [1 1])
(nth 8))to be specific: even inside a function, def creates a namespace level value, which means if you used threads the function could misbehave badly
@noisesmith I do not get what you mean. whats the story
def creates a value at namespace scope. if the function runs in two threads, the value of value-end can be over-written
(ins)user=> (defn racer [x] (def y x) (if-not (= x y) (println :race)))
#'user/racer
(ins)user=> (dotimes [i 1000] (racer i))
nil
(ins)user=> (dotimes [i 1000] (future (racer i)))
:race
:race
:race
:race
:race
nilSince you seem like a fun-loving person, here's another fun one to think about. What should the value of this infinite form be?
(+ 1 (/ 1 (+ 1 (/ 1 (+ (/ 1 (+ 1 (/ 1 (+ 1 (/ 1 (+ 1 (/ 1 (+ 1 (/ 1 (+ 1 ...)))))))))))))))
---
Also, heh:
user> (deftype Fiberator [^:unsynchronized-mutable ^clojure.lang.BigInt a
^:unsynchronized-mutable ^clojure.lang.BigInt b]
java.util.Iterator
(hasNext [_] true)
(next [this]
(let [res a]
(set! a b)
(set! b (+ res b))
res)))
user.Fiberator
user> (take 11 (iterator-seq (Fiberator. 0N 1N)))
(0N 1N 1N 2N 3N 5N 8N 13N 21N 34N 55N)
user> (nth (iterator-seq (Fiberator. 0N 1N)) 100000)
259740693472217241661550340212759154148804853865176965847247707039525345435112736862655567728367167447546375872230744321116383994738750910309656973821883044930522876385313349213530267927895670105127657827163560807305053220024323311438398651613782723812477745377833729991621463405005466986039086275099663936640921189012527196017210506030035058689402855810367511765825136837743...
user> (count (str (nth (iterator-seq (Fiberator. 0N 1N)) 100000)))
20899Looks good. :-) Keep practicing! Only 3 comments: Def is a no go. The parens are misplaced. Please try to always ask yourself if you are on the right path. Here it might be obvious for you that this is not the right way. But often you don't realise this. This is why these exercises are so important,
> (+ 1 (/ 1 (+ 1 (/ 1 (+ (/ 1 (+ 1 (/ 1 (+ 1 (/ 1 (+ 1 (/ 1 (+ 1 (/ 1 (+ 1 ...))))))))))))))) Golden post