JavaScript has code that resembles lambda expression programming capabilities (improved version) _javascript tips

Source: Internet
Author: User
After posting, I spent some time trying to solve the problem ... After several attempts, I found another pattern, and the brackets were no longer necessary:
Copy Code code as follows:

Eval (function () {
var s = ', WW = [V] > (s + = v);
var ws = [n] > ww (' <a href= "#" > (' + n + ') </A> ');
PNVIEW3 ([n] > ww (' [' + n + '] '),
1, 37,
WS, [] > ww (' ... '),
2, 1
);
document.write (s);
}. lamda0 ());

However, because of the relationship of operator precedence, comparisons, gates, assignments, and so on are still not written directly in the (pseudo) lambda expression.
Other words
Copy Code code as follows:

function (A, b) {a = = B}

Still need to write
Copy Code code as follows:

[A, B] > (a = = b)

In addition, the selected pattern itself is practical--when comparing an array to something else, the script engine tries to convert both sides into numeric values, which are then compared to strings if they are unsuccessful.
But I think it's very rare for people to get arrays that much more than anything else--so you don't even have to take the initiative to avoid it, as long as you don't use the (pseudo) lambda expression to use it.

The new implementation code is as follows:
Copy Code code as follows:

/*!
L-amda "A-lambda", a module provides alternate "LAMBDA" style programming ability for JavaScript.
Created by Nanalich. 2010-09-10
This module is published under WTFPL v2 and so you just do WHAT the Fxxx to and it.
*/
!function () {
function Attachentry (o, A, m) {
var i, J, N;
o = [].concat (o);
while (i = O.shift ()) {
For (J in a) {
if (!i[n = A[j]]) i[n] = m;
}
}
}
var xx =/"(?: \ \[\s\s]| [^\x22]) *"|' (?:\ \[\s\s]| [^\x27]) *'| ([^\s\w]\s*) \[(\s*|\s*[a-z$_][\w$]*\s* (?:, \s*[a-z$_][\w$]*\s*) *) \]\s* (>) \s* (\?) /gi;
var xy =/[\n\r), \]}]|$/.source;
function Rxclone (RX) {
Return to New RegExp (Rx.source, Rx.global? ' G ': ' + (rx.ignorecase? ' I ': ' + (rx.multiline? ' m ': '));
}
Attachentry (REGEXP, [' Clone '], Rxclone);
Attachentry (Regexp.prototype, [' Clone '], function () {return rxclone (this);});
function Translatelambda (s) {
var m, l = 0, R = ', x = Xx.clone (); Because browsers such as Firefox and Safari are overly optimized for global matching regular expressions, there is a roundabout way to create an instance of a regular expression that is not repeated.
while (M = x.exec (s)) {
var h = m[0];
Switch (H.charat (0)) {//Judge expected grammatical composition
Case ' $ '://Function Reference
Case ') ':
Case '] ':
Case ' "'://Matched to a string
Case "'":
Continue Skip all of the above
}
var p, q, t, k = m[4].length, y = new RegExp (k?) ' \] ': XY, ' G ');
R + = s.substring (l, p = m.index); Append the remaining content to the result string
Y.lastindex = L = p + h.length; Start looking for a closing parenthesis or other symbol after the pseudo operator
while (q = y.exec (s)) {
Q = q.index;
try {
t = ' return (' + s.substring (l, Q) + ');
New Function (t); Grammar test
R + = m[1] + ' function (' + m[2] + ') {' + TRANSLATELAMBDA (t) + '} '; The contents of the translation
X.lastindex = L = q + k; The next lookup starts after the current boundary
Break
catch (ex) {}
}
if (!q) L = p; Note You cannot find a closing parenthesis or a valid code to append all the matching content directly
}
try {
R + + S.SUBSTR (l);
New Function (R); Grammar test
return R;
catch (ex) {//failed, return original
return s;
}
};
var lamdaaliases = ["Translatelambda", "Lambda", "Lamda"];
Attachentry (String, lamdaaliases, TRANSLATELAMBDA);
Attachentry (String.prototype, lamdaaliases, function () {return translatelambda (this);});
var funprototype = Function.prototype;
Attachentry (function, lamdaaliases, function (func) {return Translatelambda (' 0, ' + func);});
Attachentry (Funprototype, lamdaaliases, function () {return Translatelambda (' 0, ' + this);});
var lamda0aliases = [' Lambdainit ', ' lambda0 ', ' lamda0 '];
Attachentry (function, lamda0aliases, function (func) {return Translatelambda ('! ' + func + ' () ');});
Attachentry (Funprototype, lamda0aliases, function () {return Translatelambda ('! ' + This + ' () ');});
} ();

This time adds a special method to the function, removes the judgment before the stand eaves foot, and adds a new method to simplify the calling process slightly;
Fixed a bug in which the expected grammatical composition could not be judged in the case of a space grid.

In addition, because CodePlex again to cramp, this time still did not download.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.