I thought this was neat so I had to share it :-)

For BerkeleyDB I was running into a situation where I had to bitor a  
bunch of constant values. The declarations look like:

: DB_CREATE     HEX: 0000001 ; inline
: DB_FORCE       HEX: 0000004 ; inline
...
etc...

So I would end up with code that looked like:

DB_CREATE DB_INIT_MPOOL bitor DB_INIT_TXN bitor DB_INIT_LOG bitor .....

Ugly! What I really wanted to do was treat the functions as constants  
and put them in an array so I could do a reduce.

USING: kernel make words sequences ;
IN: arrays.const

: <const-array> ( seq -- seq )
   [ [ dup word? [ execute ] [ ] if , ] each ] { } make ;

: const-reduce ( seq quote -- n )
    [ <const-array> 0 ] dip reduce ;

Examples:

{ DB_CREATE DB_FORCE 23 } <const-array>
   => { 1 4 23 }

{ DB_CREATE DB_INIT_MPOOL DB_INIT_TXN DB_INIT LOG } [ bitor ] const- 
reduce
    => 720897

Maybe something similar already exists? Either way it was fun writing  
them :-)

Glenn

V. Glenn Tarcea
[EMAIL PROTECTED]


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to