Flix (programming language): Difference between revisions

Content deleted Content added
JorKadeen (talk | contribs)
JorKadeen (talk | contribs)
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">
/// Returns the sum of x and y.
def add(x: Int, y: Int): Int = x + y
 
<syntaxhighlight lang="Scala">
/// Returns x plus one.
def inc(x: Int): Int = add(x, 1)
 
/// Returns a function that applies f twice.
def twice(f: Int -> Int): Int -> Int = x -> f(f(x))
</syntaxhighlight>
 
The <code>twice</code> function can be used as follows:
/// Returns x plus two.
 
def two(x: Int): Int = twice(inc)(x)
<syntaxhighlight lang="Scala">
def addinc(x: Int, y: Int): Int = x + y1
 
def inctwo(x: Int): Int = addtwice(x, 1inc)(0)
/// Returns 123 plus 4 = 127.
def main(): Int = twice(two)(123)
</syntaxhighlight>