Skip to content

Instantly share code, notes, and snippets.

@r00k
Created January 28, 2013 18:50
Show Gist options
  • Save r00k/4658025 to your computer and use it in GitHub Desktop.
Save r00k/4658025 to your computer and use it in GitHub Desktop.
Kinda mind-blowing fibonacci-sequence-producing function.
(defn fib [n]
(take n (map first (iterate (fn [[a b]] [b (+ a b)]) [1 1]))))
(fib 5) => (1 1 2 3 5)
@mollymorphic
Copy link

You can do something similar in JS though it's not as pretty:

function allfibs(end) {
    var x = 0, y = 1, count = 1;
    yield 1;
    while (count++ < end) {
        [x,y] = [y,x + y];
        yield y;
    }
}

function fibs(x) {
    return [ i for (i in allfibs(x)) ]
}

fibs(5) // [1,1,2,3,5]

Wouldn't be hard to write some library functions like take() to get the nth element etc.

@r00k
Copy link
Author

r00k commented Jan 29, 2013

@lmarinho - I'd been playing there all morning actually. Thanks for the rec though!

@fogus - Totally agreed. As you covered in your book (which I'm loving), there's something beautifully declarative about saying "this is how you calculate ALL fibs", and then just taking what you need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment