factor/extra/promises/promises.factor

46 lines
1.1 KiB
Factor
Raw Normal View History

2007-09-20 18:09:08 -04:00
! Copyright (C) 2004 Chris Double.
! See http://factorcode.org/license.txt for BSD license.
!
! Updated by Matthew Willis, July 2006
! Updated by Chris Double, September 2006
USING: arrays kernel sequences math vectors arrays namespaces
2008-09-10 23:11:40 -04:00
make quotations parser effects stack-checker words accessors ;
2007-09-20 18:09:08 -04:00
IN: promises
TUPLE: promise quot forced? value ;
: promise ( quot -- promise )
f f \ promise boa ;
2007-09-20 18:09:08 -04:00
: promise-with ( value quot -- promise )
curry promise ;
: promise-with2 ( value1 value2 quot -- promise )
2curry promise ;
: force ( promise -- value )
#! Force the given promise leaving the value of calling the
#! promises quotation on the stack. Re-forcing the promise
#! will return the same value and not recall the quotation.
2008-08-29 02:53:34 -04:00
dup forced?>> [
dup quot>> call >>value
t >>forced?
2007-09-20 18:09:08 -04:00
] unless
2008-08-29 02:53:34 -04:00
value>> ;
2007-09-20 18:09:08 -04:00
: stack-effect-in ( quot word -- n )
2008-08-29 02:53:34 -04:00
stack-effect [ ] [ infer ] ?if in>> length ;
2007-09-20 18:09:08 -04:00
: make-lazy-quot ( word quot -- quot )
[
dup ,
swap stack-effect-in \ curry <repetition> %
\ promise ,
] [ ] make ;
: LAZY:
2008-03-16 03:43:00 -04:00
CREATE-WORD
2007-09-20 18:09:08 -04:00
dup parse-definition
2008-01-06 22:36:34 -05:00
make-lazy-quot define ; parsing