Hello [EMAIL PROTECTED],

On 11-Jul-00, [EMAIL PROTECTED] wrote:

-- snip --

>>> make-adder: func [x][copy func [y][+ x y]]
>>> add6: make-adder 6
> ** Script Error: copy expected value argument of type: series port bitset.
> ** Where: copy func [y] [+ x y]
> 
> this didn't work, either:
>>> make-adder: func[x] [do [func [y] [+ x y]]]
> 
> nor this:
>>> make-adder: func[x] compose [func [y] [+ x y]]
> 
> So, is there a clever way to get it to cough up another instance
> of the function with it's own context that won't get changed by running
> make-adder again?
> 
> I guess I don't even care if the GC still ruins
> it because I assume that will actually get fixed, silly me.
> 

-- snip --

(please note that I havn't been following this thread too closely, so please correct 
me if I'm completely missing the point :-)

Let's have a look at the problem (as I understand it):
1. the 'x argument should be dynamic to the 'make-adder function but static/constant 
to the produced function.
2. the 'y argument should be dynamic to the produced function (no problem).

Instead of fooling around with contexts and the GC, let's make 'x "constant"

## make-adder: func [x] [func [y] compose [+ (x) y]]
## source make-adder
make-adder: func [x][func [y] compose [+ (x) y]]

The idea is, that at execution of 'make-adder, (x) is evaluated and replaced by the 
value of the argument 'x.
Let's try it:

## add1: make-adder 1
## add2: make-adder 2
## add1 10
== 11
## add2 10
== 12

Seems to work, let's look at the source of the newly generated functions:

## source add1
add1: func [y][+ 1 y]
## source add2
add2: func [y][+ 2 y]

Notice that 'x isn't there at all, and thus context/GC problems are avoided.

For a version that uses contexts, look at this:
(Notice that this approach *does* make the GC choke if doing a 'recycle)

## make-adder2: func [x] [get in make object! [val: x adder: func [y] [+ val y]] 
'adder]
## add2_1: make-adder2 1
## add2_2: make-adder2 2
## add2_1 10
== 11
## add2_2 10
== 12
## source add2_1
add2_1: func [y][+ val y]
## source add2_2
add2_2: func [y][+ val y]

-- doing some other stuff here --

## recycle
## add2_1 10
** CRASH (Should not happen) - Corrupt datatype: 78


Best regards
Thomas Jensen


Reply via email to