Arrays |
array one = [1, "two"]
array two = [
3
"four"
]
|
var arrayOne, arrayTwo;
arrayOne = [ 1, "two" ];
arrayTwo = [ 3, "four" ];
|
Blocks Preserve Self |
person = {
name = 'Man Ray'
say hi later() =
set
console. log "my name is #(self.name)"
timeout 123
}
not using an arrow
means (self) is not always (this)
using an arrow =>
means (self) is always (this)
|
var person;
person = {
name: "Man Ray",
sayHiLater: function() {
var self = this;
return setTimeout(function() {
return console.log("my name is " + self.name);
}, 123);
}
};
notUsingAnArrow(function() {
return meansIsNotAlways(self, this);
});
usingAnArrow(function() {
var self = this;
return meansIsAlways(self, this);
});
|
Closures |
map each @(file) in (files) into @{ resolve (file) }
map each @(file) in (files) into
resolve (file)
|
mapEachInInto(files, function(file) {
return resolve(file);
});
mapEachInInto(files, function(file) {
return resolve(file);
});
|
Declaring Functions |
one lower than (n) = n - 1
one higher than (n) =
n + 1
either (thing) or (other) =
thing || other
end of the line () =
ok then()
|
var oneLowerThan, oneHigherThan, eitherOr, endOfTheLine;
oneLowerThan = function(n) {
return n - 1;
};
oneHigherThan = function(n) {
return n + 1;
};
eitherOr = function(thing, other) {
return thing || other;
};
endOfTheLine = function() {
return okThen();
};
|
Dsl |
describe 'a horse'
before
self.horse = new (Horse)
context 'that is dead'
before
self.horse.kill()
it 'should not move'
self.horse.should not move()
|
describe("a horse", function() {
before(function() {
return self.horse = new Horse();
});
return context("that is dead", function() {
before(function() {
return self.horse.kill();
});
return it("should not move", function() {
return self.horse.shouldNotMove();
});
});
});
|
Event Emitter |
EventEmitter = require('events').EventEmitter
shouter = new (EventEmitter)
shouter.on 'shout'
console.log "calm down"
shouter.emit 'shout'
|
var EventEmitter, shouter;
EventEmitter = require("events").EventEmitter;
shouter = new EventEmitter();
shouter.on("shout", function() {
return console.log("calm down");
});
shouter.emit("shout");
|
Factorial (Iterative) |
factorial (n) =
x = 1
for (i = 2, i <= n, i := i + 1)
x := x * i
x
|
var factorial;
factorial = function(n) {
var x, i;
x = 1;
for (i = 2; i <= n; i = i + 1) {
x = x * i;
}
return x;
};
|
Factorial (Recursive) |
factorial (n) =
if (n == 0)
1
else
n * factorial (n - 1)
|
var factorial;
factorial = function(n) {
if (n === 0) {
return 1;
} else {
return n * factorial(n - 1);
}
};
|
Fibonacci |
fib (i) =
if ((i == 0) || (i == 1))
1
else
fib (i - 1) + fib (i - 2)
|
var fib;
fib = function(i) {
if (i === 0 || i === 1) {
return 1;
} else {
return fib(i - 1) + fib(i - 2);
}
};
|
For |
for (n = 0, n < 10, n := n + 1)
console. log (n)
|
var n;
for (n = 0; n < 10; n = n + 1) {
console.log(n);
}
|
For Each |
for each @(fruit) in (basket)
gobble(fruit)
|
var gen1_items, gen2_i, fruit;
gen1_items = basket;
for (gen2_i = 0; gen2_i < gen1_items.length; ++gen2_i) {
fruit = gen1_items[gen2_i];
gobble(fruit);
}
|
Function Calls |
y = compute some value from (z) and return it
md5 hash (read all text from file "sample.txt")
|
var y;
y = computeSomeValueFromAndReturnIt(z);
md5Hash(readAllTextFromFile("sample.txt"));
|
Functions Without Arguments |
update page()
hungry = is lunch time()
|
var hungry;
updatePage();
hungry = isLunchTime();
|
If |
if (wind speed > 20)
console. log "gone kitesurfing!"
else
console. log "no wind, programming"
|
if (windSpeed > 20) {
console.log("gone kitesurfing!");
} else {
console.log("no wind, programming");
}
|
List Comprehensions |
mapped = [x * x, where: x <- [1, 2, 3]]
filtered = [x, where: x <- [1, 2, 3], x > 1]
both = [x * y, where: x <- [1, 2, 3], y <- [4, 5, 6]]
with no where = [
x <- [1, 2, 3]
x * x
]
cartesian products = [
x <- [1, 2, 3]
y <- [4, 5, 6]
[x, y]
]
async = [
country code <- country codes
country = http.get "/countries/#(country code)"!
city name <- country.cities
city = http.get "/cities/#(city name)"!
city.population > population
city
]
|
var mapped, filtered, both, withNoWhere, cartesianProducts;
mapped = function() {
var gen1_results, gen2_items, gen3_i, x;
gen1_results = [];
gen2_items = [ 1, 2, 3 ];
for (gen3_i = 0; gen3_i < gen2_items.length; ++gen3_i) {
x = gen2_items[gen3_i];
gen1_results.push(x * x);
}
return gen1_results;
}();
filtered = function() {
var gen4_results, gen5_items, gen6_i, x;
gen4_results = [];
gen5_items = [ 1, 2, 3 ];
for (gen6_i = 0; gen6_i < gen5_items.length; ++gen6_i) {
x = gen5_items[gen6_i];
if (x > 1) {
gen4_results.push(x);
}
}
return gen4_results;
}();
both = function() {
var gen7_results, gen8_items, gen9_i, x, gen10_items, gen11_i, y;
gen7_results = [];
gen8_items = [ 1, 2, 3 ];
for (gen9_i = 0; gen9_i < gen8_items.length; ++gen9_i) {
x = gen8_items[gen9_i];
gen10_items = [ 4, 5, 6 ];
for (gen11_i = 0; gen11_i < gen10_items.length; ++gen11_i) {
y = gen10_items[gen11_i];
gen7_results.push(x * y);
}
}
return gen7_results;
}();
withNoWhere = function() {
var gen12_results, gen13_items, gen14_i, x;
gen12_results = [];
gen13_items = [ 1, 2, 3 ];
for (gen14_i = 0; gen14_i < gen13_items.length; ++gen14_i) {
x = gen13_items[gen14_i];
(function(x) {
return gen12_results.push(x * x);
})(x);
}
return gen12_results;
}();
cartesianProducts = function() {
var gen15_results, gen16_items, gen17_i, x;
gen15_results = [];
gen16_items = [ 1, 2, 3 ];
for (gen17_i = 0; gen17_i < gen16_items.length; ++gen17_i) {
x = gen16_items[gen17_i];
(function(x) {
var gen18_items, gen19_i, y;
gen18_items = [ 4, 5, 6 ];
for (gen19_i = 0; gen19_i < gen18_items.length; ++gen19_i) {
y = gen18_items[gen19_i];
(function(y) {
return gen15_results.push([ x, y ]);
})(y);
}
return void 0;
})(x);
}
return gen15_results;
}();
gen20_listComprehension(countryCodes, true, function(gen21_index, countryCode, gen22_result, continuation) {
var gen23_a = gen24_continuationOrDefault(arguments);
continuation = gen23_a.continuation;
var gen25_arguments = gen23_a.arguments;
gen21_index = gen25_arguments[0];
countryCode = gen25_arguments[1];
gen22_result = gen25_arguments[2];
http.get("/countries/" + countryCode, gen26_rethrowErrors(continuation, function(gen27_asyncResult) {
var country;
country = gen27_asyncResult;
gen20_listComprehension(country.cities, false, function(gen28_index, cityName, gen29_result, continuation) {
var gen30_a = gen24_continuationOrDefault(arguments);
continuation = gen30_a.continuation;
var gen31_arguments = gen30_a.arguments;
gen28_index = gen31_arguments[0];
cityName = gen31_arguments[1];
gen29_result = gen31_arguments[2];
http.get("/cities/" + cityName, gen26_rethrowErrors(continuation, function(gen32_asyncResult) {
var city;
city = gen32_asyncResult;
if (city.population > population) {
return continuation(void 0, gen29_result(city, gen28_index));
} else {
continuation();
}
}));
}, gen26_rethrowErrors(continuation, function(gen33_asyncResult) {
return continuation(void 0, gen22_result(gen33_asyncResult, gen21_index));
}));
}));
}, gen26_rethrowErrors(continuation, function(gen34_asyncResult) {
var async;
return continuation(void 0, async = gen34_asyncResult);
}));
|
Method Calls |
file = open file "README.md"
file. read line()
open file "README.md". read line()
|
var file;
file = openFile("README.md");
file.readLine();
openFile("README.md").readLine();
|
Method Chains |
list. map @(i) into
i + 1
.select @(i) if
i < 5
.each @(i) do
console. log (i)
|
list.mapInto(function(i) {
return i + 1;
}).selectIf(function(i) {
return i < 5;
}).eachDo(function(i) {
return console.log(i);
});
|
Multiline Functions |
item = 0
do
print (item)
item := item + 1
while @{ item < 10 }
|
var item;
item = 0;
doWhile(function() {
print(item);
return item = item + 1;
}, function() {
return item < 10;
});
|
Objects |
address = {
street = "Fake Street"
post code = "123 ABC"
}
|
var address;
address = {
street: "Fake Street",
postCode: "123 ABC"
};
|
Operators |
total height = header + (item * count) + footer
definitely = true @and true
maybe = false @or true
no way = (definitely) not
(p1) plus (p2) = { x = p1.x + p2.x, y = p1.y + p2.y }
a = { x = 1, y = 2 }
b = { x = 3, y = 4 }
a @plus b
|
var totalHeight, definitely, maybe, noWay, plus, a, b;
totalHeight = header + item * count + footer;
definitely = true && true;
maybe = false || true;
noWay = !definitely;
plus = function(p1, p2) {
return {
x: p1.x + p2.x,
y: p1.y + p2.y
};
};
a = {
x: 1,
y: 2
};
b = {
x: 3,
y: 4
};
plus(a, b);
|
Optional Arguments |
connect to mysql database (address: "1.2.3.4", port: 3307)
connect to mysql database (readonly: true)
|
connectToMysqlDatabase({
address: "1.2.3.4",
port: 3307
});
connectToMysqlDatabase({
readonly: true
});
|
Options |
connect to (port: 3306, host: "127.0.0.1") =
console.log "connecting to #(host):#(port)"
|
var connectTo;
connectTo = function(gen1_options) {
var port, host;
port = gen1_options !== void 0 && Object.prototype.hasOwnProperty.call(gen1_options, "port") && gen1_options.port !== void 0 ? gen1_options.port : 3306;
host = gen1_options !== void 0 && Object.prototype.hasOwnProperty.call(gen1_options, "host") && gen1_options.host !== void 0 ? gen1_options.host : "127.0.0.1";
return console.log("connecting to " + host + ":" + port);
};
|
Regular Expressions |
"hot dog".replace r/d.g/ "potato"
"FOO".match r/foo/gi
|
"hot dog".replace(/d.g/, "potato");
"FOO".match(/foo/gi);
|
Splat Arguments |
foo (args, ...) =
print (args)
foo 1 [2, 3] ... [4, 5] ... 6
|
var foo;
foo = function() {
var args = Array.prototype.slice.call(arguments, 0, arguments.length);
return print(args);
};
foo.apply(null, [ 1 ].concat([ 2, 3 ]).concat([ 4, 5 ]).concat([ 6 ]));
|
Strings |
'meet you at Jayne''s place'
alan kay = "The best way to predict the future
is to invent it."
mood = "happy"
announce "Feeling #(mood) today"
|
var alanKay, mood;
"meet you at Jayne's place";
alanKay = "The best way to predict the future\nis to invent it.";
mood = "happy";
announce("Feeling " + mood + " today");
|
Try Catch |
try
something messy()
catch (ex)
console.log "it went horribly wrong"
finally
better clean up()
|
try {
somethingMessy();
} catch (ex) {
console.log("it went horribly wrong");
} finally {
betterCleanUp();
}
|
Variables |
height = 1025
height :=
n = 10
(n * n * n) + 25
|
var height;
height = 1025;
height = function() {
var n;
n = 10;
return n * n * n + 25;
}();
|
Web Server |
http = require 'http'
http. create server @(request, response)
response.end "Hello World\n"
.listen 1337
|
var http;
http = require("http");
http.createServer(function(request, response) {
return response.end("Hello World\n");
}).listen(1337);
|
When |
is (expected) (action) =
action if (actual) matched =
if (expected == actual)
action
otherwise (action) =
action if (actual) matched =
action
when (actual, cases) =
for each @(action if matched) in (cases)
action = action if (actual) matched
if (action)
return (action ())
print (args, ...) = console.log (args, ...)
x = 2
when (x) [
is 0
print "x is zero"
is 1
print "x is one"
otherwise
print "x is not zero or one"
]
|
var is, otherwise, when, print, x;
is = function(expected, action) {
var actionIfMatched;
return actionIfMatched = function(actual) {
if (expected === actual) {
return action;
}
};
};
otherwise = function(action) {
var actionIfMatched;
return actionIfMatched = function(actual) {
return action;
};
};
when = function(actual, cases) {
var gen1_items, gen2_i, gen3_forResult;
gen1_items = cases;
for (gen2_i = 0; gen2_i < gen1_items.length; ++gen2_i) {
gen3_forResult = void 0;
if (function(gen2_i) {
var actionIfMatched, action;
actionIfMatched = gen1_items[gen2_i];
action = actionIfMatched(actual);
if (action) {
gen3_forResult = action();
return true;
}
}(gen2_i)) {
return gen3_forResult;
}
}
return void 0;
};
print = function() {
var args = Array.prototype.slice.call(arguments, 0, arguments.length);
var gen4_o;
gen4_o = console;
return console.log.apply(console, args);
};
x = 2;
when(x, [ is(0, function() {
return print("x is zero");
}), is(1, function() {
return print("x is one");
}), otherwise(function() {
return print("x is not zero or one");
}) ]);
|
Async Calls |
web page = http.get! "http://www.interesting.com"
scrape facts from (web page)
|
http.get("http://www.interesting.com", gen1_rethrowErrors(continuation, function(gen2_asyncResult) {
var webPage;
webPage = gen2_asyncResult;
return continuation(void 0, scrapeFactsFrom(webPage));
}));
|
Async Continuations |
current continuation (callback) =
continuation () =
callback (nil, continuation)
callback (nil, continuation)
n = 0
cont = current continuation!
console.log (n)
n := n + 1
if (n < 10)
cont ()
|
var currentContinuation, n;
currentContinuation = function(callback) {
var continuation;
continuation = function() {
callback(void 0, continuation);
};
callback(void 0, continuation);
};
n = 0;
currentContinuation(gen1_rethrowErrors(continuation, function(gen2_asyncResult) {
var cont;
cont = gen2_asyncResult;
console.log(n);
n = n + 1;
if (n < 10) {
return continuation(void 0, cont());
} else {
continuation();
}
}));
|
Async Coroutines |
last yield continuation = nil
last result continuation = nil
yield (n, yield continuation) =
last yield continuation := yield continuation
last result continuation (nil, n)
coroutine (block) =
run (result continuation) =
last result continuation := result continuation
if (last yield continuation)
last yield continuation ()
else
block
last yield continuation := nil
last result continuation := nil
strings = coroutine
yield! 1
yield! 2
yield! 3
console.log (strings!)
console.log (strings!)
console.log (strings!)
console.log 'finished'
|
var lastYieldContinuation, lastResultContinuation, yield, coroutine, strings;
lastYieldContinuation = void 0;
lastResultContinuation = void 0;
yield = function(n, yieldContinuation) {
lastYieldContinuation = yieldContinuation;
return lastResultContinuation(void 0, n);
};
coroutine = function(block) {
var run;
return run = function(resultContinuation) {
lastResultContinuation = resultContinuation;
if (lastYieldContinuation) {
return lastYieldContinuation();
} else {
return block(function() {
lastYieldContinuation = void 0;
return lastResultContinuation = void 0;
});
}
};
};
strings = coroutine(function(continuation) {
var gen1_a = gen2_continuationOrDefault(arguments);
continuation = gen1_a.continuation;
var gen3_arguments = gen1_a.arguments;
yield(1, gen4_rethrowErrors(continuation, function(gen5_asyncResult) {
gen5_asyncResult;
yield(2, gen4_rethrowErrors(continuation, function(gen6_asyncResult) {
gen6_asyncResult;
yield(3, continuation);
}));
}));
});
strings(gen4_rethrowErrors(continuation, function(gen7_asyncResult) {
console.log(gen7_asyncResult);
strings(gen4_rethrowErrors(continuation, function(gen8_asyncResult) {
console.log(gen8_asyncResult);
strings(gen4_rethrowErrors(continuation, function(gen9_asyncResult) {
console.log(gen9_asyncResult);
return continuation(void 0, console.log("finished"));
}));
}));
}));
|
Async For |
for (n = 0, n < 3, n := n + 1)
send letter! (n)
all letters sent()
|
(function(continuation) {
var gen1_a = gen2_continuationOrDefault(arguments);
continuation = gen1_a.continuation;
var gen3_arguments = gen1_a.arguments;
var n;
n = 0;
gen4_asyncFor(function(continuation) {
var gen5_a = gen2_continuationOrDefault(arguments);
continuation = gen5_a.continuation;
var gen6_arguments = gen5_a.arguments;
return continuation(void 0, n < 3);
}, function(continuation) {
var gen7_a = gen2_continuationOrDefault(arguments);
continuation = gen7_a.continuation;
var gen8_arguments = gen7_a.arguments;
return continuation(void 0, n = n + 1);
}, function(continuation) {
var gen9_a = gen2_continuationOrDefault(arguments);
continuation = gen9_a.continuation;
var gen10_arguments = gen9_a.arguments;
sendLetter(n, continuation);
}, continuation);
})(gen11_rethrowErrors(continuation, function(gen12_asyncResult) {
gen12_asyncResult;
return continuation(void 0, allLettersSent());
}));
|
Async Fork |
fs = require 'fs'
fork (block) = block @{}
fork
console.log "reading contents of file #(__filename)"
contents = fs.read file (__filename, 'utf-8')!
console.log ("contents of #(__filename):")
process.stdout.write (contents)
console.log 'processing other stuff'
|
var fs, fork;
fs = require("fs");
fork = function(block) {
return block(function() {});
};
fork(function(continuation) {
var gen1_a = gen2_continuationOrDefault(arguments);
continuation = gen1_a.continuation;
var gen3_arguments = gen1_a.arguments;
console.log("reading contents of file " + __filename);
fs.readFile(__filename, "utf-8", gen4_rethrowErrors(continuation, function(gen5_asyncResult) {
var contents;
contents = gen5_asyncResult;
console.log("contents of " + __filename + ":");
return continuation(void 0, process.stdout.write(contents));
}));
});
console.log("processing other stuff");
|
Async Futures |
a = fs.stat? 'a.txt'
b = fs.stat? 'b.txt'
console.log "size of a.txt is #(a!.size)"
console.log "size of b.txt is #(b!.size)"
|
var a, b;
a = gen1_future(function(continuation) {
fs.stat("a.txt", continuation);
});
b = gen1_future(function(continuation) {
fs.stat("b.txt", continuation);
});
a(gen2_rethrowErrors(continuation, function(gen3_asyncResult) {
console.log("size of a.txt is " + gen3_asyncResult.size);
b(gen2_rethrowErrors(continuation, function(gen4_asyncResult) {
return continuation(void 0, console.log("size of b.txt is " + gen4_asyncResult.size));
}));
}));
|
Async If |
if ((intruders) are dangerous)
kill! (intruders)
else
call the cops about! (intruders)
keep watch!
|
gen1_asyncIfElse(areDangerous(intruders), function(continuation) {
var gen2_a = gen3_continuationOrDefault(arguments);
continuation = gen2_a.continuation;
var gen4_arguments = gen2_a.arguments;
kill(intruders, continuation);
}, function(continuation) {
var gen5_a = gen3_continuationOrDefault(arguments);
continuation = gen5_a.continuation;
var gen6_arguments = gen5_a.arguments;
callTheCopsAbout(intruders, continuation);
}, gen7_rethrowErrors(continuation, function(gen8_asyncResult) {
gen8_asyncResult;
keepWatch(continuation);
}));
|
Async Sleep |
sleep (ms, awake) = set timeout (awake, ms)
console.log "wait a second..."
sleep! 1000
console.log "ok, go!"
|
var sleep;
sleep = function(ms, awake) {
return setTimeout(awake, ms);
};
console.log("wait a second...");
sleep(1e3, gen1_rethrowErrors(continuation, function(gen2_asyncResult) {
gen2_asyncResult;
return continuation(void 0, console.log("ok, go!"));
}));
|
Async Try Catch |
record = nil
try
database = connect to the database!
record := database.query!
catch (error)
exit the program because of (error)!
finally
database.close!
console.log("RECORD FOUND: #(record)")
|
var record;
record = void 0;
gen1_asyncTry(function(continuation) {
var gen2_a = gen3_continuationOrDefault(arguments);
continuation = gen2_a.continuation;
var gen4_arguments = gen2_a.arguments;
connectToTheDatabase(gen5_rethrowErrors(continuation, function(gen6_asyncResult) {
var database;
database = gen6_asyncResult;
database.query(gen5_rethrowErrors(continuation, function(gen7_asyncResult) {
return continuation(void 0, record = gen7_asyncResult);
}));
}));
}, function(error, continuation) {
var gen8_a = gen3_continuationOrDefault(arguments);
continuation = gen8_a.continuation;
var gen9_arguments = gen8_a.arguments;
error = gen9_arguments[0];
exitTheProgramBecauseOf(error, continuation);
}, function(continuation) {
var gen10_a = gen3_continuationOrDefault(arguments);
continuation = gen10_a.continuation;
var gen11_arguments = gen10_a.arguments;
database.close(continuation);
}, gen5_rethrowErrors(continuation, function(gen12_asyncResult) {
gen12_asyncResult;
return continuation(void 0, console.log("RECORD FOUND: " + record));
}));
|