Skip to content

Instantly share code, notes, and snippets.

@violet-athena
Last active October 2, 2015 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save violet-athena/2320261 to your computer and use it in GitHub Desktop.
Save violet-athena/2320261 to your computer and use it in GitHub Desktop.
Node.js non-blocking Fibonacci written on CoffeeScript
###
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