HadesLang Doc
Search…
Declaring lambdas

Declaring a simple lambda

The simple lambda consists of only a single statement. It will automatically return the result of said statement without the need of the put keyword.

Example

1
var add = {x,y => x + y}
2
add(2,2) //this would return 4
Copied!

Declaring a complex lambda

A complex lambda contains multiple LOC and does require the use of the put keyword to return things.

Example

1
var pow = { x,y =>
2
var result = 1
3
4
while(y not 0)
5
result *= x
6
y--
7
end
8
9
put result //complex lambda needs a put statement
10
}
11
12
pow(4,2) //this would return 16
Copied!

Lambda return types

Lambdas can state return and input types like so: lambda::(input types)->return type.
1
with console from std:io
2
3
var bye lambda::(string)->string = { a string => put "Bye " + a }
4
bye("Foo")
5
6
var cheers lambda::(string)->none = { a => console.out("Cheers " + a) }
7
cheers("Bar")
Copied!

Declaring a lambda without parameters

If no parameter is needed, an _ is used as the parameter name, instead.

Example

1
with console from std:io
2
var hello = { _ => console.out:"Hello, world!" }
3
hello()
Copied!

Assigning a function to a lambda variable

One can assign a function to a lambda. If the function has function guards, they are being rewritten into a match block internally. This can be useful when passing a function to a method.

Example

1
with console from std:io
2
3
func myFunction(a int) requires a < 10
4
console.out("a is smaller than 10")
5
end
6
7
func myFunction(a int)
8
//This default function is called when every condition is false
9
console.out("a is " + a)
10
end
11
12
var fn lambda::(int)->none = myFunction
13
14
fn(1) //Output: a is smaller than 10
15
fn(50) //Output: a is 50
Copied!
1
with console from std:io
2
3
func onInit()
4
console.out("Component initialized!")
5
end
6
7
func onError(e)
8
console.outError("There was an error: {}".format(e))
9
end
10
11
...
12
13
comp.init(onInit, onError)
Copied!
Last modified 1yr ago