Content deleted Content added
Line 66:
=== Higher-Order Functions ===
The following program fragment defines a function <code>twice</code> that when given a function <code>f</code> returns a new function that applies <code>f</code> two times to its argument:
<syntaxhighlight lang="Scala">▼
def add(x: Int, y: Int): Int = x + y▼
▲<syntaxhighlight lang="Scala">
def inc(x: Int): Int = add(x, 1)▼
def twice(f: Int -> Int): Int -> Int = x -> f(f(x))
</syntaxhighlight>
The <code>twice</code> function can be used as follows:
<syntaxhighlight lang="Scala">
</syntaxhighlight>
|