23 lines
817 B
Mojo
23 lines
817 B
Mojo
# probe: functions as values the way the manual passes them: a type parameter F: def(Int) -> Int with f: F, for a named function, a lambda, and a capturing lambda
|
|
# status: the expected output is Programming+'s interpreter's prediction; corpus.py verify confirms or refutes it on godbolt
|
|
# expect-stdout: [1, 4, 9, 16]
|
|
# expect-stdout: [10, 20, 30, 40]
|
|
# expect-stdout: [101, 102, 103, 104]
|
|
def apply_all[F: def(Int) -> Int, //](f: F, xs: List[Int]) -> List[Int]:
|
|
var out = List[Int]()
|
|
for x in xs:
|
|
out.append(f(x))
|
|
return out^
|
|
|
|
|
|
def square(x: Int) -> Int:
|
|
return x * x
|
|
|
|
|
|
def main():
|
|
var xs: List[Int] = [1, 2, 3, 4]
|
|
print(apply_all(square, xs))
|
|
print(apply_all(lambda (x: Int) -> Int: x * 10, xs))
|
|
var offset = 100
|
|
print(apply_all(lambda (x: Int) -> Int: x + offset, xs))
|