Call a function: Difference between revisions

m
→‎{{header|Factor}}: fixing formatting
(added a solution for Factor)
m (→‎{{header|Factor}}: fixing formatting)
Line 972:
=={{header|Factor}}==
* Calling a word with no arguments:
<lang Factor>foo</lang>
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>
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>
"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>
<email>
"jack@aol.com" >>from
{ "jill@aol.com" } >>to
"Hello there" >>subject
body >>body
send-email</lang>
</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>
\ foo
</lang>
Additionally, you can put words directly inside sequences and quotations:
<lang Factor>{ foo } [ foo ]</lang>
{ foo } [ foo ]
</lang>
 
* Obtaining the return value, which will be placed on the stack:
<lang Factor>foo</lang>
foo
</lang>
 
* Returns true if the word is defined in the Factor VM as opposed to in a vocabulary:
<lang Factor>\ foo primitive?</lang>
\ foo primitive?
</lang>
 
* Factor makes no distinction between subroutines and functions. Presumably, a subroutine is just a word which puts nothing on the stack.
Line 1,024 ⟶ 1,008:
 
* 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 20 31 } 2 [ - ] curry map .</lang>
! { -1 0 1 }
</lang>
 
=={{header|Forth}}==
1,808

edits