1
0
mirror of https://github.com/dschmenk/PLASMA.git synced 2024-09-27 04:54:37 +00:00

more better lambda example

This commit is contained in:
David Schmenk 2017-08-03 11:48:01 -07:00 committed by GitHub
parent 76038502c8
commit f939aef691

View File

@ -67,7 +67,7 @@ Different projects have led to the architecture of PLASMA, most notably Apple Pa
- [Assignment](#assignment) - [Assignment](#assignment)
- [Empty Assignments](#empty-assignments) - [Empty Assignments](#empty-assignments)
- [Increment and Decrement](#increment-and-decrement) - [Increment and Decrement](#increment-and-decrement)
- [Lambda Functions](#lambda-functions) - [Lambda (Anonymous) Functions](#lambda-functions)
- [Control Flow](#control-flow) - [Control Flow](#control-flow)
- [CALL](#call) - [CALL](#call)
- [RETURN](#return) - [RETURN](#return)
@ -477,7 +477,7 @@ end
def keyin(x, y, key) def keyin(x, y, key)
if key == '+' if key == '+'
eval(x, y, &(x, y) x + y) eval_op(x, y, &(x, y) x + y)
fin fin
end end
```` ````
@ -1037,11 +1037,11 @@ puti(i) // print 4
### Lambda Functions ### Lambda Functions
A Lambda function is a simple, anonymous function that can be passed to a function or assigned to a variable. It is called as a function pointer. The function can take a number of parameters and return a value based on the parameters and global values. By enclosing the expression of the lambda function in paranthesis, multiple values can be returned. A Lambda function is a simple, anonymous function that can be passed to a function or assigned to a variable. It is called as a function pointer. The function can take a number of parameters and return a value based on the parameters and/or global values. By enclosing the expression of the lambda function in paranthesis, multiple values can be returned.
``` ```
def whatop(a, b, op) def what_op(a, b, op)
puti(a) puti(a)
puts(" ? ") puts(" ? ")
puti(b) puti(b)
@ -1057,10 +1057,10 @@ def lambdas
lambda1 = &(a, b) a + b lambda1 = &(a, b) a + b
lambda2 = &(a, b) (a + b, a - b) lambda2 = &(a, b) (a + b, a - b)
x = lambda1(1, 2)#1 // This will return 3 x = lambda1(1, 2) // This will return 3
x, y = lambda2(3, 4)#2 // This will return 7, -1 x, y = lambda2(3, 4)#2 // This will return 7, -1 (the #2 denotes two returned values instead of the default of one)
whatop(10, 20, &(a, b) a * b) // This will print 10 ? 20 = 200 what_op(10, 20, &(a, b) a * b) // This will print 10 ? 20 = 200
return &(x, y, z) x * z / y // You can even return lambdas from definitions return &(x, y, z) x * z / y // You can even return lambdas from definitions (these x, y are different from the locally defined x, y)
end end
```` ````