Last active
October 2, 2015 20:58
-
-
Save violet-athena/2320261 to your computer and use it in GitHub Desktop.
Node.js non-blocking Fibonacci written on CoffeeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
Original project here: https://github.com/glenjamin/node-fib , | |
this is CoffeeScript rewrite of the app. Nothing more, nothing less. | |
### | |
app = require("express").createServer() | |
port = "3000" | |
fibonacci = (n, callback) -> | |
inner = (n1, n2, i) -> | |
callback null, n2 if i > n | |
func = (if i % 100 then inner else inner_tick) | |
func n2, n1 + n2, i + 1 | |
inner_tick = (n1, n2, i) -> | |
process.nextTick -> | |
inner n1, n2, i | |
if n is 1 or n is 2 | |
callback null, 1 | |
else | |
inner 1, 1, 3 | |
app.get "/:n", (req, res) -> | |
fibonacci req.params.n, (err, number) -> | |
res.send "#{number}" | |
app.listen port | |
console.log "server listens on localhost #{port}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment