Wednesday, 22 January 2020

Lambda Expression and Lambda Function in Python, JavaScript (also Node.js)

Lambda expression and lambda function in programming are no-name (anonymous). Both Python and JavaScript (also Node.js) have this feature.

Example utility function:
f = x^2 + x + 1

Example lambda expression in Python:
F = lambda X: f(X);

Example lambda function in Python, note the commas and no 'return':
F = lambda X: (
  statement1,
  statement2,
  Result
);

Example lambda expression in JavaScript:
F = X => f(X);

Example lambda function in JavaScript, note the semicolons and 'return':
F = X => {
  statement1;
  statement2; 
  return Result;
};

Generate a function with constant C in Python:
F = (lambda T: lambda X: f(X)+C)(C);

Generate a function with constant C in JavaScript:
F = (C => X => f(X)+C)(C);

No comments:

Post a Comment