2023-10-02 01:41:38 +00:00
|
|
|
# Higher Order functions
|
|
|
|
|
|
|
|
|
|
|
|
## Function as parameter
|
|
|
|
|
|
|
|
```thp
|
|
|
|
fun map[A, B](Array[A] input, (A) -> B function) -> Array[B]
|
|
|
|
{
|
|
|
|
// implementation
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
## Function as return
|
|
|
|
|
|
|
|
```thp
|
|
|
|
fun generate_generator() -> () -> Int
|
|
|
|
{
|
|
|
|
// code...
|
2023-10-24 01:39:34 +00:00
|
|
|
return fun() {
|
|
|
|
322
|
|
|
|
}
|
2023-10-02 01:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-20 10:17:21 +00:00
|
|
|
val generator = generate_generator() // A function
|
|
|
|
val value = generate_generator()() // An Int
|
2023-10-02 01:41:38 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|