lazy-lists: add first attempt at simple list comprehensions

chris.double 2006-10-05 02:26:26 +00:00
parent 32d3baed21
commit e252f06e7b
2 changed files with 20 additions and 1 deletions

View File

@ -51,6 +51,9 @@ M: cons nil? ( cons -- bool )
: 2list ( a b -- cons )
nil <cons> <cons> ;
: 3list ( a b c -- cons )
nil <cons> <cons> <cons> ;
! Both 'car' and 'cdr' are promises
: lazy-cons ( car cdr -- promise )
>r <promise> r> <promise> <cons>
@ -297,4 +300,10 @@ M: lazy-zip nil? ( lazy-zip -- bool )
[ car ] keep cdr [ car lcartesian-product ] keep cdr list>array swap [
swap [ swap [ add ] lmap-with ] lmap-with lconcat
] reduce
] if ;
] if ;
: lcomp2 ( list1 list2 quot -- list )
>r lcartesian-product r> swap [ swap >r first2 r> call ] lmap-with ;
: lcomp3 ( list1 list2 list3 quot -- list )
>r 3array seq>list lcartesian-product* r> swap [ swap >r first3 r> call ] lmap-with ;

View File

@ -157,3 +157,13 @@ HELP: lcartesian-product*
{ $values { "list" "a list of lists" } { "result" "list of cartesian products" } }
{ $description "Given a list of lists, return a list containing the cartesian product of those lists." }
{ $see-also leach lmap lmap-with lconcat ltake lsubset lfrom-by lcartesian-product } ;
HELP: lcomp2
{ $values { "list1" "a list" } { "list2" "a list" } { "quot" "a quotation with stack effect ( A B -- C )" } { "list" "the resulting list" } }
{ $description "List comprehension for the case where there are two input lists. The cartesian product of the two lists is generated and 'quot' called with each element from the cartesian product on the stack, the result of which is returned in the final 'list'." }
{ $see-also leach lmap lmap-with lconcat ltake lsubset lfrom-by lcartesian-product lcomp3 } ;
HELP: lcomp3
{ $values { "list1" "a list" } { "list2" "a list" } { "list3" "a list" } { "quot" "a quotation with stack effect ( A B C -- D )" } { "list" "the resulting list" } }
{ $description "List comprehension for the case where there are three input lists. The cartesian product of the lists are generated and 'quot' called with each element from the cartesian product on the stack, the result of which is returned in the final 'list'." }
{ $see-also leach lmap lmap-with lconcat ltake lsubset lfrom-by lcartesian-product lcomp2 } ;