Call a function: Difference between revisions

added a solution for Factor
(added a solution for Factor)
Line 969:
// Partial application example
let add2 = (+) 2</lang>
 
=={{header|Factor}}==
* Calling a word with no arguments:
<lang Factor>
foo
</lang>
 
* Calling a word with a fixed number of arguments. This will pull as many objects as it needs from the stack. If there are not enough, it will result in a stack underflow.
<lang Factor>
foo
</lang>
 
* No special support for optional arguments.
 
* Variable arguments are achieved by defining a word that takes an integer, and operates on that many items at the top of the stack:
<lang Factor>
"a" "b" "c" 3 narray
</lang>
 
* The named arguments idiom is to define a tuple, set its slots, and pass it to a word:
<lang Factor>
<email>
"jack@aol.com" >>from
{ "jill@aol.com" } >>to
"Hello there" >>subject
body >>body
send-email
</lang>
 
* Factor lacks statements; everything is an expression.
 
* First-class context: this pushes a word to the stack. Use execute to evaluate.
<lang Factor>
\ foo
</lang>
Additionally, you can put words directly inside sequences and quotations:
<lang Factor>
{ foo } [ foo ]
</lang>
 
* Obtaining the return value, which will be placed on the stack:
<lang Factor>
foo
</lang>
 
* Returns true if the word is defined in the Factor VM as opposed to in a vocabulary:
<lang Factor>
\ foo primitive?
</lang>
 
* Factor makes no distinction between subroutines and functions. Presumably, a subroutine is just a word which puts nothing on the stack.
 
* By value or by reference is irrelevant; Factor is not applicative.
 
* Partial application is possible by use of curry. Here, the object 2 is curried into the left side of the quotation (anonymous function) [ - ]:
<lang Factor>
{ 1 2 3 } 2 [ - ] curry map .
! { -1 0 1 }
</lang>
 
=={{header|Forth}}==
1,808

edits