Code Monkey home page Code Monkey logo

lambdajs's Introduction

Lambda.js

支持dojo AMD加载规范,实现动态编译Lambda规范字符串表达式为匿名函数,支持闭包函数体,具体请查看博客

  • 表达式应用
var add = function(x,y){return x+y;}               //通常写法
var add = lambda("(x,y)=>x+y");                   //转换为lambda
console.log(add.toString());                          //输出lambda编译后的源代码

Lambda编译后源代码如下:

function anonymous(x, y) {
return x+y ;
}

JSLINQ使用中例子:

var fromq= JSLINQ([1, 2, 3]).Where(lambda("o=>o>2"));

  • 函数体,即闭包(Closure)
var each = function(callback){
    for(var index=0;index<this.items.length;index++)
         if(callback(item,index))break;
}
var callback = function(item,index){            //通常写法
      if(index>100)return true; 
      console.log(item);
      };
//转换为lambda
var callback = lambda("(item,index)=>if(index>100)return true;console.log(item)",true);      
each(callback);                                        //只输出前100个元素
console.log(callback.toString());                 //输出lambda编译后的源代码

Lambda编译后源代码如下:

function anonymous(item, index) {
  return  (function(item,index){
        if(index>100)
           return true;
        console.log(item)
   }).call(this,item,index) ;
}

JSLINQ使用中例子:

var People =  
    [
        { ID: 1, FirstName: "Chris", LastName: "Pearson" },
        { ID: 2, FirstName: "Kate", LastName: "Johnson" },
        { ID: 3, FirstName: "Josh", LastName: "Sutherland" }
    ];
var fromq= JSLINQ(People).Each(lambda("o=>o.Name = o.FirstName+' '+o.LastName",true));
fromq = fromq.Select(lambda("o=>{Name:o.FirstName+' '+o.LastName}"));

  • 缓存功能 Lambda增加了Cache功能,所有经Lambda编译后的表达式都会在后台缓存,如有相同的表达式时,Lambda只进行分析后找到缓存的匿名方法实现并返回,这样减少了Function的调用,使速度更快,另还针对缓存的Lambda表达式进行了使用次数的统计,这样可以查看某个表达式的使用情况。
var src = "(o,i)=>console.log(o)";
var afn = lambda(src), bfn = lambda(src);
console.log(afn === bfn); //true
var cache = lambda.getCache();
for (var i = 0; i < cache.length; i++) {
    console.log("lambda:", cache[i].lambda, ", useCount:", cache[i].num);
}

lambdajs's People

Contributors

bonashen avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.