20 lines
702 B
Mojo
20 lines
702 B
Mojo
# probe: a function passed as a plain def(Int) -> Int argument is a compile error in 1.1.0 (that type is a type, not a value's): the compiler's words for a named function and a lambda
|
|
# expect: compile-error
|
|
# note: kept as a checker test; the code below is exactly what godbolt rejected
|
|
# note: kept as a checker test; the code below is exactly what godbolt rejected
|
|
def apply_all(xs: List[Int], f: def(Int) -> 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(xs, square))
|
|
print(apply_all(xs, lambda (x: Int) -> Int: x * 10))
|