Paste: amb and bag-of

Author: Sam
Mode: factor
Date: Sat, 23 May 2009 19:40:34
Plain Text |
USING: arrays continuations kernel namespaces sequences ;

IN: amb

<PRIVATE

SYMBOL: amb-failure

PRIVATE>
        
: amb ( sequence -- elt )
    [
	swap [ [ amb-failure set continue-with ] callcc0 2drop ] curry
	amb-failure get [ each ] dip
        continue
    ] curry callcc1 ;

: amb-init ( -- )
    [ amb-failure set t ] callcc1
    [ amb-init "amb: no more alternatives" throw ] unless ;

: amb-fail ( -- ) amb-failure get continue ;

: bag-of ( quot -- seq )
    { } clone
    [
	[ swap push amb-fail ] curry compose
	[ ] 2array amb call
    ] keep ;                          

Annotation: Next

Author: Sam
Mode: factor
Date: Sat, 23 May 2009 19:43:41
Plain Text |
USING: arrays continuations kernel namespaces sequences ;

IN: amb

<PRIVATE

SYMBOL: amb-failure

PRIVATE>
        
: amb ( sequence -- elt )
    [
	swap [ [ amb-failure set continue-with ] callcc0 2drop ] curry
	amb-failure get [ each ] dip
        continue
    ] curry callcc1 ;

: amb-init ( -- )
    [ amb-failure set t ] callcc1
    [ amb-init "amb: no more alternatives" throw ] unless ;

: amb-fail ( -- ) amb-failure get continue ;

: bag-of ( quot -- seq )
    V{ } clone
    [
	[ swap push amb-fail ] curry compose
	[ ] 2array amb call( -- )
    ] keep ;                          

New Annotation

Summary:
Author:
Mode:
Body: