Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Search Gists
Sign in Sign up

Instantly share code, notes, and snippets.

@cowboy
CreatedAugust 31, 2012 20:57
    • Star(5)You must be signed in to star a gist
    • Fork(0)You must be signed in to fork a gist
    Save cowboy/3558882 to your computer and use it in GitHub Desktop.
    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){
    varnop=function(){};
    varexec=function(fn){fn()};
    if(state){
    $elseif=function(){returnnop;};
    $else=nop;
    returnexec;
    }else{
    $elseif=$if;
    $else=exec;
    returnnop;
    }
    };
    // Why log now when we can log later?
    varconsole$log=function(arg){
    returnfunction(){
    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
    Copy link
    Author

    Also see the original,Abstraction.js.

    @hkdobrev
    Copy link

    OMG

    @TheEskhaton
    Copy link

    well this completely blew my mind

    @ryasmi
    Copy link

    @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 freeto join this conversation on GitHub. Already have an account?Sign in to comment

    [8]ページ先頭

    ©2009-2025 Movatter.jp