Search This Blog

Monday, 1 November 2021

Arrow Functions or Lambdas - Language Agnostic expression

Lambdas in Javascript/ES6+ | Lambdas in C# |


Lambdas are functions that have the ability to behave in a language agnostic manner. This means that you could write a lambda or arrow function without referring to any specific language syntaxes

1.    var sum = (n1,n2) => n1+n2

The above line of code has no language specific syntax. Hence it could be used in a different language like C#, Java, Python etc to produce the same output

Lambdas are used in lexical parsers, language processors, complex calculations and pattern matching systems.

If the sum function had to be written in the normal way in javascript / es6+, this is how it would it would look like

1.  function Sum(n1,n2){
2.    return n1, n2
3.  
4.   
5.         OR
6.   
7.  var Sum = function(n1,n2){
8.     return n1+n2
9.  }
10. USING ARROW FUNCTIONS, the FUNCTION keyword is not required, 
the datatypes for the parameters are not required and the RETURN keyword can also be skipped
11.  
12. var Sum = (n1,n2) => n1+n2;

To read about how these lambda expressions are used in a different language like C#.Net, please refer to my blog link here: Lambdas in C#

Hope this helps.

No comments:

Post a Comment