factor/basis/boxes/boxes.factor

27 lines
603 B
Factor
Raw Normal View History

! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2008-05-14 20:03:07 -04:00
USING: kernel accessors ;
IN: boxes
2008-05-15 01:03:21 -04:00
TUPLE: box value occupied ;
: <box> ( -- box ) box new ;
2008-05-14 20:03:07 -04:00
ERROR: box-full box ;
: >box ( value box -- )
2008-05-15 01:03:21 -04:00
dup occupied>>
[ box-full ] [ t >>occupied (>>value) ] if ;
2008-05-14 20:03:07 -04:00
ERROR: box-empty box ;
: box> ( box -- value )
2008-05-15 01:03:21 -04:00
dup occupied>>
[ [ f ] change-value f >>occupied drop ] [ box-empty ] if ;
: ?box ( box -- value/f ? )
2008-05-15 01:03:21 -04:00
dup occupied>> [ box> t ] [ drop f f ] if ;
2008-02-29 20:10:30 -05:00
: if-box? ( box quot -- )
2008-11-29 14:21:40 -05:00
[ ?box ] dip [ drop ] if ; inline