Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a lambda (function)?

For a person without a comp-sci background, what is a lambda in the world of Computer Science?

like image 318
Brian Warshaw Avatar asked Aug 19 '08 16:08

Brian Warshaw


People also ask

What is lambda function?

Simply put, a lambda function is just like any normal python function, except that it has no name when defining it, and it is contained in one line of code. A lambda function evaluates an expression for a given argument. You give the function a value (argument) and then provide the operation (expression).

What is lambda in simple terms?

00:05 A lambda function is a simple, short, throwaway function which is designed to be created inline in code. They're also known as lambda expressions, anonymous functions, lambda abstractions, lambda form, or function literals.

What is use of lambda function in Python?

We use lambda functions when we require a nameless function for a short period of time. In Python, we generally use it as an argument to a higher-order function (a function that takes in other functions as arguments). Lambda functions are used along with built-in functions like filter() , map() etc.

What is lambda function explain with an example?

What is Lambda Function in Python? A Lambda Function in Python programming is an anonymous function or a function having no name. It is a small and restricted function having no more than one line. Just like a normal function, a Lambda function can have multiple arguments with one expression.


2 Answers

A lambda is a type of function, defined inline. Along with a lambda you also usually have some kind of variable type that can hold a reference to a function, lambda or otherwise.

For instance, here's a C# piece of code that doesn't use a lambda:

public Int32 Add(Int32 a, Int32 b) {     return a + b; }  public Int32 Sub(Int32 a, Int32 b) {     return a - b; }  public delegate Int32 Op(Int32 a, Int32 b);  public void Calculator(Int32 a, Int32 b, Op op) {     Console.WriteLine("Calculator: op(" + a + ", " + b + ") = " + op(a, b)); }  public void Test() {     Calculator(10, 23, Add);     Calculator(10, 23, Sub); } 

This calls Calculator, passing along not just two numbers, but which method to call inside Calculator to obtain the results of the calculation.

In C# 2.0 we got anonymous methods, which shortens the above code to:

public delegate Int32 Op(Int32 a, Int32 b);  public void Calculator(Int32 a, Int32 b, Op op) {     Console.WriteLine("Calculator: op(" + a + ", " + b + ") = " + op(a, b)); }  public void Test() {     Calculator(10, 23, delegate(Int32 a, Int32 b)     {         return a + b;     });     Calculator(10, 23, delegate(Int32 a, Int32 b)     {         return a - b;     }); } 

And then in C# 3.0 we got lambdas which makes the code even shorter:

public delegate Int32 Op(Int32 a, Int32 b);  public void Calculator(Int32 a, Int32 b, Op op) {     Console.WriteLine("Calculator: op(" + a + ", " + b + ") = " + op(a, b)); }  public void Test() {     Calculator(10, 23, (a, b) => a + b);     Calculator(10, 23, (a, b) => a - b); } 
like image 26
Lasse V. Karlsen Avatar answered Oct 05 '22 09:10

Lasse V. Karlsen


Lambda comes from the Lambda Calculus and refers to anonymous functions in programming.

Why is this cool? It allows you to write quick throw away functions without naming them. It also provides a nice way to write closures. With that power you can do things like this.

Python

def adder(x):     return lambda y: x + y add5 = adder(5) add5(1) 6 

As you can see from the snippet of Python, the function adder takes in an argument x, and returns an anonymous function, or lambda, that takes another argument y. That anonymous function allows you to create functions from functions. This is a simple example, but it should convey the power lambdas and closures have.

Examples in other languages

Perl 5

sub adder {     my ($x) = @_;     return sub {         my ($y) = @_;         $x + $y     } }  my $add5 = adder(5); print &$add5(1) == 6 ? "ok\n" : "not ok\n"; 

JavaScript

var adder = function (x) {     return function (y) {         return x + y;     }; }; add5 = adder(5); add5(1) == 6 

JavaScript (ES6)

const adder = x => y => x + y; add5 = adder(5); add5(1) == 6 

Scheme

(define adder     (lambda (x)         (lambda (y)            (+ x y)))) (define add5     (adder 5)) (add5 1) 6 

C# 3.5 or higher

Func<int, Func<int, int>> adder =      (int x) => (int y) => x + y; // `int` declarations optional Func<int, int> add5 = adder(5); var add6 = adder(6); // Using implicit typing Debug.Assert(add5(1) == 6); Debug.Assert(add6(-1) == 5);  // Closure example int yEnclosed = 1; Func<int, int> addWithClosure =      (x) => x + yEnclosed; Debug.Assert(addWithClosure(2) == 3); 

Swift

func adder(x: Int) -> (Int) -> Int{    return { y in x + y } } let add5 = adder(5) add5(1) 6 

PHP

$a = 1; $b = 2;  $lambda = fn () => $a + $b;  echo $lambda(); 

Haskell

(\x y -> x + y)  

Java see this post

// The following is an example of Predicate :  // a functional interface that takes an argument  // and returns a boolean primitive type.  Predicate<Integer> pred = x -> x % 2 == 0; // Tests if the parameter is even. boolean result = pred.test(4); // true 

Lua

adder = function(x)     return function(y)         return x + y     end end add5 = adder(5) add5(1) == 6        -- true 

Kotlin

val pred = { x: Int -> x % 2 == 0 } val result = pred(4) // true 

Ruby

Ruby is slightly different in that you cannot call a lambda using the exact same syntax as calling a function, but it still has lambdas.

def adder(x)   lambda { |y| x + y } end add5 = adder(5) add5[1] == 6 

Ruby being Ruby, there is a shorthand for lambdas, so you can define adder this way:

def adder(x)   -> y { x + y } end 

R

adder <- function(x) {   function(y) x + y } add5 <- adder(5) add5(1) #> [1] 6 
like image 180
mk. Avatar answered Oct 05 '22 10:10

mk.