47 lines
1.2 KiB
Plaintext
47 lines
1.2 KiB
Plaintext
fn apply(f: fn(Int): Int, x: Int): Int = f(x)
|
|
|
|
fn compose(f: fn(Int): Int, g: fn(Int): Int): fn(Int): Int = fn(x: Int): Int => f(g(x))
|
|
|
|
fn double(x: Int): Int = x * 2
|
|
|
|
fn addOne(x: Int): Int = x + 1
|
|
|
|
fn square(x: Int): Int = x * x
|
|
|
|
let result1 = apply(double, 21)
|
|
|
|
let doubleAndAddOne = compose(addOne, double)
|
|
|
|
let result2 = doubleAndAddOne(5)
|
|
|
|
let result3 = square(addOne(double(5)))
|
|
|
|
fn add(a: Int): fn(Int): Int = fn(b: Int): Int => a + b
|
|
|
|
let add5 = add(5)
|
|
|
|
let result4 = add5(10)
|
|
|
|
fn multiply(a: Int, b: Int): Int = a * b
|
|
|
|
let times3 = fn(x: Int): Int => multiply(3, x)
|
|
|
|
let result5 = times3(7)
|
|
|
|
let transform = fn(record: { x: Int, y: Int }): Int => record.x + record.y
|
|
|
|
let point = { x: 10, y: 20 }
|
|
|
|
let recordSum = transform(point)
|
|
|
|
fn printResults(): Unit with {Console} = {
|
|
Console.print("apply(double, 21) = " + toString(result1))
|
|
Console.print("compose(addOne, double)(5) = " + toString(result2))
|
|
Console.print("pipe: 5 |> double |> addOne |> square = " + toString(result3))
|
|
Console.print("curried add5(10) = " + toString(result4))
|
|
Console.print("partial times3(7) = " + toString(result5))
|
|
Console.print("record transform = " + toString(recordSum))
|
|
}
|
|
|
|
let output = run printResults() with {}
|