Hello,
Every now and then I ask slava for a feature: accessing tuple slots by name,
instead of using tuple-specific getter words. For example:
TUPLE: point x y z ;
TUPLE: foo z y x ;
1 2 3 <foo> "x" get-field
10 20 30 <point> "x" get-field
To try this out, I threw get-field together. Thanks to Factor's introspective
capabilities, this is possible.
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
USING: kernel-internals kernel generic words sequences math ;
IN: tuple-field
! A 'field' is a slot that has a name
: neg? ( number -- ? ) 0 < ;
: slot-names ( tuple -- seq ) class "slot-names" word-prop ;
: dig-for-field ( tuple field -- tuple i )
over slot-names dupd index
dup neg? [ drop >r delegate r> dig-for-field ] [ nip ] if ;
: get-field ( tuple field -- value ) dig-for-field 4 + slot ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Here's a convention I've experimented with:
: :x "x" get-field ;
: :y "y" get-field ;
: :z "z" get-field ;
set-field is similar to get-field. To complete the message passing
master-plan, add call-field. :-D
Ed
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk