-
Notifications
You must be signed in to change notification settings - Fork 10
Comprehensions
Comprehensions are pretty handy at creating very tight and almost incomprehensable (pun intended?) algorithmic code. Used by Haskell hackers globally, they must be a good idea.
We'll introduce the where
macro, that has the following syntax:
(x * y) where (x = [1..2], (x) is even, y = [4, 5, 6], x * y > 10)
And would expand to:
function () {
var results = [];
var list = [1, 2];
for (var x = 0; x < list.length; x++) {
if (isEven(x)) {
var pogo$1$y = [4, 5, 6];
for (var y = 0; y < pogo$1$y.length; y++) {
if (x * y > 10) {
results.push(x * y);
}
}
}
}
return results;
}();
Comprehensions can take on most list processing use-cases:
-
each
(console: log (x)) where (x = [1, 2])
-
map
(y) where (x = [1, 2], y = x + 1)
Or
(x + 1) where (x = [1, 2])
-
filter
(x) where (x = [1, 2], (x) is even)
We'll also introduce the given
macro, which is identical to the where
macro, but the body is in a block, for example:
given (x = [1, 2], (x) is even)
console: log (x)
We can also allow ranges to be defined with the =
operator:
(x) where (x = 5..10)
According to the documentation, you can have Closure Compiler add the sourceMappingURL to the bottom of the script with something like this:
--output_wrapper "%output%
//# sourceMappingURL=output.js.map"
being added to your call. Not that you cannot use "\n" here, and you need a newline literal. On a Linux shell this works just fine (if you're inside of quotes when you press enter, the command doesn't get executed).