Last active
July 15, 2016 17:33
-
-
Save xhinking/a933410848b7f2d986a570c3426dd48a to your computer and use it in GitHub Desktop.
Javascript Lambda Expressions - http://www.paulfree.com/11/javascript-lambda-expressions/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function lambda( l ) { | |
var fn = l.match(/\((.*)\)\s*=>\s*(.*)/) ; | |
var p = [] ; | |
var b = "" ; | |
if ( fn.length > 0 ) fn.shift() ; | |
if ( fn.length > 0 ) b = fn.pop() ; | |
if ( fn.length > 0 ) p = fn.pop() | |
.replace(/^\s*|\s(?=\s)|\s*$|,/g, '').split(' ') ; | |
// prepend a return if not already there. | |
fn = ( ( ! /\s*return\s+/.test( b ) ) ? "return " : "" ) + b ; | |
p.push( fn ) ; | |
try { | |
return Function.apply( {}, p ) ; | |
} | |
catch(e) { | |
return null ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment