Skip to content

Instantly share code, notes, and snippets.

@creationix
Created August 31, 2012 02:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creationix/1b47cba9f3a066147966 to your computer and use it in GitHub Desktop.
Save creationix/1b47cba9f3a066147966 to your computer and use it in GitHub Desktop.
Feeling out continuable syntax in coffeescript, javascript, and lua
fs = require('fs')
open = (path, flags, mode) -> (callback) ->
fs.open(path, flags, mode or 438, callback)
close = (fd) -> (callback) ->
fs.close(fd, callback)
open("home.txt", "r") (err, fd) ->
throw err if err
console.log("Opened")
close(fd) (err) ->
throw err if err
console.log("Closed")
var fs = require('fs');
function open(path, flags, mode) {
return function (callback) {
fs.open(path, flags, mode || 0666, callback);
};
}
function close(fd) {
return function (callback) {
fs.close(fd, callback);
};
}
open("home.txt", "r")(function(err, fd) {
if (err) throw err;
console.log("Opened");
close(fd)(function(err) {
if (err) throw err;
console.log("Closed");
});
});
local fs = require "fs"
local function open(path, flags, mode)
return function (callback)
fs.open(path, flags, mode or "0666", callback)
end
end
local function close(fd)
return function (callback)
fs.close(fd, callback)
end
end
open("home.txt", "r")(function (err, fd)
if err then error(err) end
print "Opened"
close(fd)(function (err)
if err then error(err) end
print "Closed"
end)
end)
@michaelficarra
Copy link

LiveScript:

fs = require 'fs'

open = (path, flags, mode, cb) ->
  fs.open path, flags, mode ? 438, cb
close = fs.close

err, fd <-! open 'home.txt', 'r', null
throw err if err
console.log 'Opened'
err <-! close fd
throw err if err
console.log 'Closed'

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