this post was submitted on 16 Feb 2022
40 points (93.5% liked)

Programmer Humor

32054 readers
1986 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
top 5 comments
sorted by: hot top controversial new old
[–] MontyVirus@sh.itjust.works 8 points 1 year ago

This has no business looking this satisfying

[–] cloudy1999@sh.itjust.works 4 points 1 year ago (1 children)

Goofy but aesthetically pleasing impl

[–] nychtelios@rlyeh.icu 1 points 1 year ago* (last edited 1 year ago)

This isn't goofy, this is an extremely diffused "pattern" when you need to use non dynamic number sequences.

[–] exapsy@sh.itjust.works 3 points 1 year ago

Dynamic programming /s

[–] yogthos@lemmy.ml 0 points 2 years ago

best of both world with Lisp macros :)

(defmacro gen-fib [size]
  (let [values (->> (iterate (fn [[a b]] [b (+ a b)]) [0 1])
                    (map first)
                    (take size)
                    (vec))]
    `(defn ~'fib [~'n] (~values ~'n))))

(macroexpand-1 '(gen-fib 10))
;=> (clojure.core/defn fib [n] ([0 1 1 2 3 5 8 13 21 34] n))

(gen-fib 10)

(fib 9)
;=> 34
load more comments
view more: next ›