Making lists into a mixin
							parent
							
								
									ffd80ad6df
								
							
						
					
					
						commit
						aa8769c29d
					
				| 
						 | 
				
			
			@ -9,13 +9,10 @@ quotations promises combinators io ;
 | 
			
		|||
IN: lazy-lists
 | 
			
		||||
 | 
			
		||||
! Lazy List Protocol
 | 
			
		||||
MIXIN: list
 | 
			
		||||
GENERIC: car   ( cons -- car )
 | 
			
		||||
GENERIC: cdr   ( cons -- cdr )
 | 
			
		||||
GENERIC: nil?  ( cons -- ? )
 | 
			
		||||
GENERIC: list? ( object -- ? )
 | 
			
		||||
 | 
			
		||||
M: object list? ( object -- bool )
 | 
			
		||||
  drop f ;
 | 
			
		||||
 | 
			
		||||
M: promise car ( promise -- car )
 | 
			
		||||
  force car ;
 | 
			
		||||
| 
						 | 
				
			
			@ -26,9 +23,6 @@ M: promise cdr ( promise -- cdr )
 | 
			
		|||
M: promise nil? ( cons -- bool )
 | 
			
		||||
  force nil? ;
 | 
			
		||||
 | 
			
		||||
M: promise list? ( object -- bool )
 | 
			
		||||
  drop t ;
 | 
			
		||||
 | 
			
		||||
TUPLE: cons car cdr ;
 | 
			
		||||
 | 
			
		||||
C: cons cons
 | 
			
		||||
| 
						 | 
				
			
			@ -45,9 +39,6 @@ M: cons cdr ( cons -- cdr )
 | 
			
		|||
M: cons nil? ( cons -- bool )
 | 
			
		||||
    nil eq? ;
 | 
			
		||||
 | 
			
		||||
M: cons list? ( object -- bool )
 | 
			
		||||
  drop t ;
 | 
			
		||||
 | 
			
		||||
: 1list ( obj -- cons )
 | 
			
		||||
    nil cons ;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -74,9 +65,6 @@ M: lazy-cons cdr ( lazy-cons -- cdr )
 | 
			
		|||
M: lazy-cons nil? ( lazy-cons -- bool )
 | 
			
		||||
    nil eq? ;
 | 
			
		||||
 | 
			
		||||
M: lazy-cons list? ( object -- bool )
 | 
			
		||||
  drop t ;
 | 
			
		||||
 | 
			
		||||
: 1lazy-list ( a -- lazy-cons )
 | 
			
		||||
  [ nil ] lazy-cons ;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -138,9 +126,6 @@ M: memoized-cons nil? ( memoized-cons -- bool )
 | 
			
		|||
    memoized-cons-nil?
 | 
			
		||||
  ] if ;
 | 
			
		||||
 | 
			
		||||
M: memoized-cons list? ( object -- bool )
 | 
			
		||||
  drop t ;
 | 
			
		||||
 | 
			
		||||
TUPLE: lazy-map cons quot ;
 | 
			
		||||
 | 
			
		||||
C: <lazy-map> lazy-map
 | 
			
		||||
| 
						 | 
				
			
			@ -159,9 +144,6 @@ M: lazy-map cdr ( lazy-map -- cdr )
 | 
			
		|||
M: lazy-map nil? ( lazy-map -- bool )
 | 
			
		||||
  lazy-map-cons nil? ;
 | 
			
		||||
 | 
			
		||||
M: lazy-map list? ( object -- bool )
 | 
			
		||||
  drop t ;
 | 
			
		||||
 | 
			
		||||
TUPLE: lazy-map-with value cons quot ;
 | 
			
		||||
 | 
			
		||||
C: <lazy-map-with> lazy-map-with
 | 
			
		||||
| 
						 | 
				
			
			@ -182,9 +164,6 @@ M: lazy-map-with cdr ( lazy-map-with -- cdr )
 | 
			
		|||
M: lazy-map-with nil? ( lazy-map-with -- bool )
 | 
			
		||||
  lazy-map-with-cons nil? ;
 | 
			
		||||
 | 
			
		||||
M: lazy-map-with list? ( object -- bool )
 | 
			
		||||
  drop t ;
 | 
			
		||||
 | 
			
		||||
TUPLE: lazy-take n cons ;
 | 
			
		||||
 | 
			
		||||
C: <lazy-take> lazy-take
 | 
			
		||||
| 
						 | 
				
			
			@ -206,9 +185,6 @@ M: lazy-take nil? ( lazy-take -- bool )
 | 
			
		|||
    lazy-take-cons nil?
 | 
			
		||||
  ] if ;
 | 
			
		||||
 | 
			
		||||
M: lazy-take list? ( object -- bool )
 | 
			
		||||
  drop t ;
 | 
			
		||||
 | 
			
		||||
TUPLE: lazy-until cons quot ;
 | 
			
		||||
 | 
			
		||||
C: <lazy-until> lazy-until
 | 
			
		||||
| 
						 | 
				
			
			@ -226,9 +202,6 @@ M: lazy-until cdr ( lazy-until -- cdr )
 | 
			
		|||
M: lazy-until nil? ( lazy-until -- bool )
 | 
			
		||||
   drop f ;
 | 
			
		||||
 | 
			
		||||
M: lazy-until list? ( lazy-until -- bool )
 | 
			
		||||
   drop t ;
 | 
			
		||||
 | 
			
		||||
TUPLE: lazy-while cons quot ;
 | 
			
		||||
 | 
			
		||||
C: <lazy-while> lazy-while
 | 
			
		||||
| 
						 | 
				
			
			@ -245,9 +218,6 @@ M: lazy-while cdr ( lazy-while -- cdr )
 | 
			
		|||
M: lazy-while nil? ( lazy-while -- bool )
 | 
			
		||||
   [ car ] keep lazy-while-quot call not ;
 | 
			
		||||
 | 
			
		||||
M: lazy-while list? ( lazy-while -- bool )
 | 
			
		||||
   drop t ;
 | 
			
		||||
 | 
			
		||||
TUPLE: lazy-subset cons quot ;
 | 
			
		||||
 | 
			
		||||
C: <lazy-subset> lazy-subset
 | 
			
		||||
| 
						 | 
				
			
			@ -285,9 +255,6 @@ M: lazy-subset nil? ( lazy-subset -- bool )
 | 
			
		|||
    ] if
 | 
			
		||||
  ] if ;
 | 
			
		||||
 | 
			
		||||
M: lazy-subset list? ( object -- bool )
 | 
			
		||||
  drop t ;
 | 
			
		||||
 | 
			
		||||
: list>vector ( list -- vector )
 | 
			
		||||
  [ [ , ] leach ] V{ } make ;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -311,9 +278,6 @@ M: lazy-append cdr ( lazy-append -- cdr )
 | 
			
		|||
M: lazy-append nil? ( lazy-append -- bool )
 | 
			
		||||
   drop f ;
 | 
			
		||||
 | 
			
		||||
M: lazy-append list? ( object -- bool )
 | 
			
		||||
  drop t ;
 | 
			
		||||
 | 
			
		||||
TUPLE: lazy-from-by n quot ;
 | 
			
		||||
 | 
			
		||||
C: lfrom-by lazy-from-by ( n quot -- list )
 | 
			
		||||
| 
						 | 
				
			
			@ -331,9 +295,6 @@ M: lazy-from-by cdr ( lazy-from-by -- cdr )
 | 
			
		|||
M: lazy-from-by nil? ( lazy-from-by -- bool )
 | 
			
		||||
  drop f ;
 | 
			
		||||
 | 
			
		||||
M: lazy-from-by list? ( object -- bool )
 | 
			
		||||
  drop t ;
 | 
			
		||||
 | 
			
		||||
TUPLE: lazy-zip list1 list2 ;
 | 
			
		||||
 | 
			
		||||
C: <lazy-zip> lazy-zip
 | 
			
		||||
| 
						 | 
				
			
			@ -351,9 +312,6 @@ M: lazy-zip cdr ( lazy-zip -- cdr )
 | 
			
		|||
M: lazy-zip nil? ( lazy-zip -- bool )
 | 
			
		||||
    drop f ;
 | 
			
		||||
 | 
			
		||||
M: lazy-zip list? ( object -- bool )
 | 
			
		||||
  drop t ;
 | 
			
		||||
 | 
			
		||||
TUPLE: sequence-cons index seq ;
 | 
			
		||||
 | 
			
		||||
C: <sequence-cons> sequence-cons
 | 
			
		||||
| 
						 | 
				
			
			@ -376,9 +334,6 @@ M: sequence-cons cdr ( sequence-cons -- cdr )
 | 
			
		|||
M: sequence-cons nil? ( sequence-cons -- bool )
 | 
			
		||||
    drop f ;
 | 
			
		||||
 | 
			
		||||
M: sequence-cons list? ( object -- bool )
 | 
			
		||||
  drop t ;
 | 
			
		||||
 | 
			
		||||
: >list ( object -- list )
 | 
			
		||||
  {
 | 
			
		||||
    { [ dup sequence? ] [ 0 swap seq>list ] }
 | 
			
		||||
| 
						 | 
				
			
			@ -419,9 +374,6 @@ M: lazy-concat nil? ( lazy-concat -- bool )
 | 
			
		|||
    drop f
 | 
			
		||||
  ] if ;
 | 
			
		||||
 | 
			
		||||
M: lazy-concat list? ( object -- bool )
 | 
			
		||||
  drop t ;
 | 
			
		||||
 | 
			
		||||
: lcartesian-product ( list1 list2 -- result )
 | 
			
		||||
  swap [ swap [ 2array ] lmap-with ] lmap-with lconcat ;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -492,3 +444,20 @@ M: lazy-io cdr ( lazy-io -- cdr )
 | 
			
		|||
 | 
			
		||||
M: lazy-io nil? ( lazy-io -- bool )
 | 
			
		||||
  car not ;
 | 
			
		||||
 | 
			
		||||
INSTANCE: cons list
 | 
			
		||||
INSTANCE: sequence-cons list
 | 
			
		||||
INSTANCE: memoized-cons list
 | 
			
		||||
INSTANCE: promise list
 | 
			
		||||
INSTANCE: lazy-io list
 | 
			
		||||
INSTANCE: lazy-concat list
 | 
			
		||||
INSTANCE: lazy-cons list
 | 
			
		||||
INSTANCE: lazy-map list
 | 
			
		||||
INSTANCE: lazy-map-with list
 | 
			
		||||
INSTANCE: lazy-take list
 | 
			
		||||
INSTANCE: lazy-append list
 | 
			
		||||
INSTANCE: lazy-from-by list
 | 
			
		||||
INSTANCE: lazy-zip list
 | 
			
		||||
INSTANCE: lazy-while list
 | 
			
		||||
INSTANCE: lazy-until list
 | 
			
		||||
INSTANCE: lazy-subset list
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue