Executing words on data farther up the stack
Subject: Executing words on data farther up the stack
Newsgroups: gmane.comp.lang.factor.general
Date: 2008-08-21 20:39:11 GMT
Hi,
I’ve been working on an interface to BerkeleyDB for Factor. One thing I’ve noticed as I’ve worked through this is a common idiom like the following:
Say I have a tuple to hold my BerkeleyDB data like DB_ENV, DB, etc…. When I need to make a call I often end up with a stack that has the berkeleydb object on the bottom of the stack (denoted by <bdb>). When I make a call for example to open the database I’ll end up with a call like:
“/tmp/db” XXX bdb-open ! where XXX are flags or’d together.
So my stack looks like:
<bdb>
“/tmp/db”
XXX
The code in the bdb-open word wants to grab the <bdb> object and pull a couple of items from it.
I was ending up with code that looked like:
>r >r [ dbenv>> ] [ db>> ] bi r> r>
So, I ended up writing some utility functions that I put in a vocabulary I’m tentatively calling “combinatorsn”. The vocabulary implements the spread and cleave combinators but allows you to execute them at arbitrary stack positions. For example rather than the >r >r code above I have:
[ dbenv>> ] [ db>> ] bi-2
This word acts just like bi, except it skips over the first 2 stack entries and starts execution on the 3rd stack item, restoring the first 2 entries when done.
So:
“1 2 3 [ 100 + ] [ 200 + ] bi-2” results in a stack that looks like:
101
201
2
3
There is also a bi-n for arbitrary depth:
1 2 3 [ 100 + ] [ 200 + ] 2 bi-n
At the moment I have:
bi-n, bi-1, bi-2, bi-3
bi*-n, bi*-1 bi*-2 bi*-3
spread-n
cleave-n
2cleave-n
3cleave-n
I’m working my way through supporting all the other operators. Would this be of use to anyone else? Is there perhaps a better way? I’ve noticed I’m doing a fair bit of stack shuffling in my code as I slowly get used to working with the stack.
Thanks,
Glenn
V. Glenn Tarcea
gtarcea-63aXycvo3TyHXe+LvDLADg@public.gmane.org
Hey buddy, can you paradigm?
------------------------------------------------------------------------- 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@... https://lists.sourceforge.net/lists/listinfo/factor-talk
(and that's after factoring it down some). Will you be posting on approaches you've learned?
RSS Feed