Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HACK Lambda Example -- what?

Can someone explain how this works?

http://docs.hhvm.com/manual/en/hack.lambda.php

Variables are captured automatically and transitively (including $this):

<?hh
$z = 11;
$foo = $x ==> $y ==> $x * $z + $y;
$bar = $foo(5);
var_dump($bar(4)); // outputs 59
like image 204
Bob Brunius Avatar asked Apr 09 '14 17:04

Bob Brunius


People also ask

What is the function of hack?

Definition: Hacking is an attempt to exploit a computer system or a private network inside a computer. Simply put, it is the unauthorised access to or control over computer network security systems for some illicit purpose.

What are lambdas used for?

Lambda functions are intended as a shorthand for defining functions that can come in handy to write concise code without wasting multiple lines defining a function. They are also known as anonymous functions, since they do not have a name unless assigned one.

What is lambda function in python 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.

Are there lambda functions in C?

Significance of Lambda Function in C/C++ Lambda Function − Lambda are functions is an inline function that doesn't require any implementation outside the scope of the main program. Lambda Functions can also be used as a value by the variable to store.


1 Answers

Conceptually, $foo is like a function with 2 inputs, x and y. Calling $foo is like a partial function evaluation that sets x=5. The $bar call then evaluates the function with y=4. So you end up with x * z + y = 5 * 11 + 4 = 59.

To put it another way, $foo is a lambda that evaluates to another lambda. So $bar becomes a lambda that evaluates to a number.

$z = 11

$foo = $x ==> $y ==> $x * $z + $y;
// foo = f(x,y,z) = x * z + y

$bar = $foo(5);                 // $bar is now $y ==> 5 * $z + $y
// bar = f(y,z) = 5 * z + y

$result = $bar(4);              // this gives you 5 * $z + 4 = 59
// result = f(z) = 5 * z + 4

var_dump(result);               // outputs 59
like image 146
McGarnagle Avatar answered Sep 22 '22 02:09

McGarnagle