Is there lambda syntax in C # in JavaScript?

Is there a framework or post processor for JavaScript that supports lambda syntax like in C #?

Function definitions in CoffeeScript seem like lambdas, but I have not fully studied them.

Can someone tell me if I can use lambda syntax in JavaScript?

+61
javascript
Aug 25 '11 at 12:35
source share
6 answers

Lambda functions with similar syntax are included in ECMAscript 6 , they are known as arrow functions. Example:

["duck", "cat", "goat"].filter(el => el.length > 3); returns ["duck", "goat"]

Currently supports the latest versions of Firefox and Chrome .

To use this syntax in JavaScript targeting older browsers, there are tools that can compile ES 6 for a more widely supported version β€” for example, Babel or Traceur tools .

+65
Mar 11 '14 at 17:43
source share

You can use typescript ( www.typescriptlang.org/) :

 function (d, i) { return "translate(" + dx + "," + dy + ")"; } 

will become

 (d, i) => "translate(" + dx + "," + dy + ")" 

and much more cool stuff, like typing: ... what if you go into it

+12
Mar 15 '13 at 6:24
source share

I started creating an expander for jQuery that does exactly what you ask. Js-lambda

For example, in C #:

  coll.TakeWhile(x => x.index < 3); 

Looks like:

  $(coll).TakeWhile("x => x.index < 3"); 
+5
Aug 28 '13 at 13:16
source share

Javascript has some very nice anonymous functions that let you create anyWhere lambdas as a function, but the implementation is accompanied by the repetition of a few words and areas:

 function(name){ return "Hello, " + name; } 

Thanks to EcmaScript 2015, lambda expresion is now available for javaScript with a simpler syntax: Arrow function

 (name) => "Hello, " + name 
+3
Nov 10 '15 at 8:20
source share

using Typescript, I created a List<T> class that extends Array<T>

using:

 var list = new List<ListView>(); ... var selected = list.Items.Where(x => x.Selected); 

implementation:

  export class List<T> extends Array<T> { public Where(callback: (value: T) => boolean, thisArg?: any): List<T> { try { const list = new List<T>(); if (this.length <= 0) return list; if (typeof callback !== "function") throw new TypeError(callback + ' is not a function'); for (let i = 0; i < this.length; i++) { const c = this[i]; if (c === null) continue; if (callback.call(thisArg, c)) list.Add(c); } return list; } catch (ex) { return new List<T>(); } } ... } 

EDIT: I created the github repository: https://github.com/sevensc/linqscript

+3
Aug 09 '16 at 8:29
source share

Exact Matthew McVeigh!

I want to contribute also to this example.

 "use strict"; /* Lambda Helper to print */ function PrintLineForElement(element, index, array){ document.write("a[" + index + "] = " + JSON.stringify(element)+"<br>"); } /* function to print array */ function Print(a){ a.forEach(PrintLineForElement) document.write("<br>"); } /* user model */ class User{ constructor(_id,_name,_age,_job){ this.id = _id; this.name = _name; this.age = _age; this.job = _job; } } /* Provaider users */ class UserService{ constructor(){ } GetUsers(){ //fake ajax response let data = []; data.push(new User(1,"Juan",20,"AM")); data.push(new User(2,"Antonio",20,"AM")); data.push(new User(3,"Miguel Angel",30,"Developer")); data.push(new User(4,"Bea",26,"AM")); data.push(new User(5,"David",24,"Developer")); data.push(new User(6,"Angel",24,"Developer")); data.push(new User(7,"David",34,"TeamLead")); data.push(new User(8,"Robert",35,"TeamLead")); data.push(new User(9,"Carlos",35,"TeamLead")); return data; } } const userService = new UserService(); const users = userService.GetUsers(); //get user order by name document.write("All users </br>"); Print(users); //get user order by name document.write("Order users by name ASC : users.sort((a,b)=>{return a.name > b.name })</br>"); Print(users.sort((a,b)=>{return a.name > b.name; })); document.write("Order users by name DESC : users.sort((a,b)=>{return a.name < b.name })</br>"); Print(users.sort((a,b)=>{return a.name < b.name; })); //get user filter by age document.write("Only users oldest or equal than 25 : users.filter( (w) => {return w.age >= 25;} )</br>"); Print(users.filter( (w) => {return w.age >= 25;} )); document.write("Only users smallest or equal than 25 : users.filter( (w) => {return w.age <= 25;} )</br>"); Print(users.filter( (w) => {return w.age <= 25;} )); //GroupBY /** * custom group by with Array.prototype.keys **/ Array.prototype.groupBy = function(f) { let groups = []; this.forEach( function( o ) { let group = f(o) ; groups[group] = groups[group] || []; groups[group].push( o ); }); return Object.keys(groups).map( function( group ) { return groups[group]; }) } document.write("Group user by age </br>"); Print(users.groupBy( (w) => {return w.age } )); document.write("Group user by job </br>"); Print(users.groupBy( (w) => {return w.job } )); document.write("Group user by job</br>"); Print(users.groupBy( (g) => {return [g.job] } )); document.write("Group user by job and age then order by name </br>"); Print(users.sort((a,b)=>{return a.job > b.job}).groupBy( (g) => {return [g.job+g.age] } )); document.write("Group user by job and age then order by name and filter where age < 25 and job equal AM </br>"); Print(users.sort((a,b)=>{return a.job > b.job}).filter((w)=>{ return w.age<25 || w.job == "AM" }).groupBy( (g) => {return [g.job+g.age] } )); 

https://jsfiddle.net/angelfragap/0b5epjw3/29/

+3
Jan 18 '17 at 10:27
source share



All Articles