Hi Ludovic,
You might want to check out the bi word. eg:
M: rectangle area
[ width>> ] [ height>> ] bi * ;
There is a really nice tutorial at http://elasticdog.com/
Look under the 'combinators' part for a description of the bi (and other
combinators) word.
Thanks,
Glenn
V. Glenn Tarcea
[email protected]
-----Original Message-----
From: Ludovic Kuty [mailto:[email protected]]
Sent: Monday, January 12, 2009 9:56 AM
To: [email protected]
Subject: [Factor-talk] Accessing multiple slots without using dup and swap
Hello,
I saw that the stack effect of every slot reader slot>> is ( object --
value ). Thus the object is popped off the stack. If I want to get the
value of another slot, I do not have the object to query it. So I have
to dup it first.
How can I do this the idiomatic way ?
Suppose that I have a rectangle class defined as (code kept to a bare
minimum for the sake of clarity):
TUPLE: rectangle { width float } { height float } ;
Now I define a method to calculate the area of a rectangle. Note the
use of dup and swap to access both slots. I feel that there should be
a better way to access them.
M: rectangle area
dup width>> swap height>> * ;
I can use it like this:
<rectangle>
100 >>width
50 >>height
area .
I thought of doing like this:
SYMBOL: temp
M: rectangle area
temp [ temp get width>> temp get height>> * ] with-variable ;
But now I have an implementation detail (the fact that temp is a
symbol) that is leaking out of the method body. It doesn't feel right.
I should be able to avoid SYMBOL and use "\ temp" but Factor curiously
complains: "Word not found in current vocabulary search path". The \
parsing word should avoid trying to execute the word. Any idea are
welcome.
Thank in advance,
Ludovic Kuty
----------------------------------------------------------------------------
--
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk
------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk