How to make a sequence of functions in REBOL?

64 Views Asked by At

I'd like to collect various functions in a sequence and activate them by the sequence index, like in this simple example:

mul2: func [n] [2 * n]
mul3: func [n] [2 * n]
...
(pick [mul2 mul3] 1) 2 ; yields 2

It seems that mul2 is not treated like a function when it's referred to as a sequence item:

type? (pick [mul2] 1) == word!

Is it possible to arrange functions into sequences?

While experiencing with this example, I noticed that

function? mul2

complains that the argument of mul2 is missing, instead of returning true. Where am I wrong?

1

There are 1 best solutions below

3
endo64 On BEST ANSWER

mul2 and mul3 are just words

mul2: func [n] [2 * n]
mul3: func [n] [3 * n]

w: pick [mul2 mul3] 1
type? w
; == word!

When you get it's value, then you'll have the function:

f: get pick [mul2 mul3] 1
; == func [n][2 * n]

type? :f
; == function!

function? :f
; == true

Notice that I used :f (get-word) to get the unevaluated value (the function itself) instead of f, because f will be evaluated and will require its parameters.

And then you can use f:

f 6
; == 12