Task

Pass a function     as an argument     to another function.



Related task

   First-class functions


define square(x: Integer): Integer
{
    return x * x
}
 
var l = [1, 2, 3] # Inferred type: List[Integer].
 
# Transform using a user-defined function.
print(l.map(square)) # [1, 4, 9]
 
# Using a built-in method this time.
print(l.map(Integer.to_s)) # ["1", "2", "3"]
 
# Using a lambda (with the type of 'x' properly inferred).
print(l.map{|x| (x + 1).to_s()}) # ["2", "3", "4"]
 
# In reverse order using F#-styled pipes.
Boolean.to_i |> [true, false].map |> print
 
define apply[A, B](value: A, f: Function(A => B)): B
{
    return f(value)
}
 
# Calling user-defined transformation.
print(apply("123", String.parse_i)) # Some(123)