Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Abstraction.js "Lite" (live-coded at BrazilJS 2012)
/*
* Abstraction.js "Lite"
*
* Copyright (c) 2012 "Cowboy" Ben Alman
* Licensed under the MIT license.
* http://benalman.com/about/license/
*/
var $elseif, $else;
var $if = function(state) {
var nop = function() {};
var exec = function(fn) { fn() };
if (state) {
$elseif = function() { return nop; };
$else = nop;
return exec;
} else {
$elseif = $if;
$else = exec;
return nop;
}
};
// Why log now when we can log later?
var console$log = function(arg) {
return function() {
console.log(arg);
};
};
// Valid JavaScript
$if (true) (
console$log("1")
)
$elseif (true) (
console$log("shouldn't log a")
)
$else (
console$log("shouldn't log b")
)
$if (false) (
console$log("shouldn't log c")
)
$elseif (true) (
console$log("2")
)
$elseif (true) (
console$log("shouldn't log d")
)
$elseif (false) (
console$log("shouldn't log e")
)
$else (
console$log("shouldn't log f")
)
$if (false) (
console$log("shouldn't log g")
)
$elseif (false) (
console$log("shouldn't log h")
)
$else (
console$log("3")
)
// Logs:
// 1
// 2
// 3
@cowboy

This comment has been minimized.

Copy link
Owner Author

@cowboy cowboy commented Aug 31, 2012

Also see the original, Abstraction.js.

@hkdobrev

This comment has been minimized.

Copy link

@hkdobrev hkdobrev commented Sep 2, 2012

OMG

@TheEskhaton

This comment has been minimized.

Copy link

@TheEskhaton TheEskhaton commented Oct 16, 2013

well this completely blew my mind

@ryansmith94

This comment has been minimized.

Copy link

@ryansmith94 ryansmith94 commented Oct 16, 2013

@cowboy if the whole language could be written this way and macros were implemented, I think this could be awesome. JavaScript could consist of a small set of functions and really simple syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.