REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 3-Aug-2010 Edit History  

REBOL 3 Functions: apply

apply  func  block  /only

Apply a function to a reduced block of arguments.

Arguments:

func [any-function!] - Function value to apply

block [block!] - Block of args, reduced first (unless /only)

Refinements:

/only - Use arg values as-is, do not reduce the block

See also:

do   switch   case  

Description

When you evaluate a function, you normally provide any arguments directly in-line with its call:

append data 123

However, there are times when you want to store the arguments as a single block and pass them to the function. This is the purpose of the apply function. The above example can be written as:

apply :append [data 123]

or, using a variable to hold the block:

args: [data 123]
apply :append args

If any arguments are missing from the block, a none! is passed instead:

data: [456]
apply :append [data]
probe data
[456 none]

Function refinements can also be passed in the order they are specified by the arguments spec block. For example, we can see:

>> ? append
USAGE:
    APPEND series value /part length /only /dup count

So in this example we use the /dup refinement:

data: [456]
apply :append [data 1 none none none true 3]
probe data
[456 1 1 1]

Note that the refinement itself must be set to true.


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin