2005-01-14 12:01:48 -05:00
|
|
|
! Copyright (C) 2003, 2005 Slava Pestov.
|
2005-01-29 14:18:28 -05:00
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2005-04-03 16:55:56 -04:00
|
|
|
IN: lists USING: errors generic kernel math sequences ;
|
2005-04-02 02:39:33 -05:00
|
|
|
|
|
|
|
|
! Sequence protocol
|
2005-05-14 17:18:45 -04:00
|
|
|
M: f length drop 0 ;
|
|
|
|
|
M: cons length cdr length 1 + ;
|
2005-04-03 16:55:56 -04:00
|
|
|
|
2005-04-25 19:54:21 -04:00
|
|
|
M: f empty? drop t ;
|
|
|
|
|
M: cons empty? drop f ;
|
|
|
|
|
|
2004-07-16 02:26:21 -04:00
|
|
|
: 2list ( a b -- [ a b ] )
|
|
|
|
|
unit cons ;
|
|
|
|
|
|
2004-12-30 02:40:14 -05:00
|
|
|
: 2unlist ( [ a b ] -- a b )
|
|
|
|
|
uncons car ;
|
|
|
|
|
|
2004-07-16 02:26:21 -04:00
|
|
|
: 3list ( a b c -- [ a b c ] )
|
|
|
|
|
2list cons ;
|
|
|
|
|
|
2005-01-31 14:02:09 -05:00
|
|
|
: 3unlist ( [ a b c ] -- a b c )
|
|
|
|
|
uncons uncons car ;
|
|
|
|
|
|
2005-05-05 22:30:58 -04:00
|
|
|
M: general-list contains? ( obj list -- ? )
|
2005-02-09 20:57:19 -05:00
|
|
|
#! Test if a list contains an element equal to an object.
|
2005-01-19 21:01:47 -05:00
|
|
|
[ = ] some-with? >boolean ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-02-09 20:57:19 -05:00
|
|
|
: memq? ( obj list -- ? )
|
|
|
|
|
#! Test if a list contains an object.
|
|
|
|
|
[ eq? ] some-with? >boolean ;
|
|
|
|
|
|
2004-07-30 02:44:12 -04:00
|
|
|
: partition-add ( obj ? ret1 ret2 -- ret1 ret2 )
|
2004-11-27 00:33:17 -05:00
|
|
|
rot [ swapd cons ] [ >r cons r> ] ifte ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-11-21 21:16:16 -05:00
|
|
|
: partition-step ( ref list combinator -- ref cdr combinator car ? )
|
|
|
|
|
pick pick car pick call >r >r unswons r> swap r> ; inline
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-11-21 21:16:16 -05:00
|
|
|
: (partition) ( ref list combinator ret1 ret2 -- ret1 ret2 )
|
2004-11-21 03:29:18 -05:00
|
|
|
>r >r over [
|
|
|
|
|
partition-step r> r> partition-add (partition)
|
2004-07-16 02:26:21 -04:00
|
|
|
] [
|
2004-11-21 21:16:16 -05:00
|
|
|
3drop r> r>
|
2004-11-16 12:35:19 -05:00
|
|
|
] ifte ; inline
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-11-21 21:16:16 -05:00
|
|
|
: partition ( ref list combinator -- list1 list2 )
|
2004-07-16 02:26:21 -04:00
|
|
|
#! The combinator must have stack effect:
|
|
|
|
|
#! ( ref element -- ? )
|
2004-11-21 21:16:16 -05:00
|
|
|
[ ] [ ] (partition) ; inline
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
|
: sort ( list comparator -- sorted )
|
2004-11-21 03:29:18 -05:00
|
|
|
#! To sort in ascending order, comparator must have stack
|
|
|
|
|
#! effect ( x y -- x>y ).
|
2004-07-16 02:26:21 -04:00
|
|
|
over [
|
2004-11-21 21:16:16 -05:00
|
|
|
( Partition ) [ >r uncons dupd r> partition ] keep
|
2004-11-21 03:29:18 -05:00
|
|
|
( Recurse ) [ sort swap ] keep sort
|
|
|
|
|
( Combine ) swapd cons append
|
2004-07-16 02:26:21 -04:00
|
|
|
] [
|
|
|
|
|
drop
|
2004-11-16 12:35:19 -05:00
|
|
|
] ifte ; inline
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
|
: unique ( elem list -- list )
|
2004-11-15 21:37:49 -05:00
|
|
|
#! Prepend an element to a list if it does not occur in the
|
|
|
|
|
#! list.
|
2004-08-24 15:11:10 -04:00
|
|
|
2dup contains? [ nip ] [ cons ] ifte ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-04-25 19:54:21 -04:00
|
|
|
M: general-list reverse ( list -- list )
|
2004-10-13 17:42:03 -04:00
|
|
|
[ ] swap [ swons ] each ;
|
|
|
|
|
|
2005-05-16 01:15:48 -04:00
|
|
|
M: f map ( list quot -- list ) drop ;
|
|
|
|
|
|
|
|
|
|
M: cons map ( list quot -- list | quot: elt -- elt )
|
|
|
|
|
(each) rot >r map r> swons ;
|
2005-01-01 17:20:48 -05:00
|
|
|
|
2004-08-03 02:08:11 -04:00
|
|
|
: remove ( obj list -- list )
|
2005-02-01 21:47:10 -05:00
|
|
|
#! Remove all occurrences of objects equal to this one from
|
|
|
|
|
#! the list.
|
2005-01-14 12:01:48 -05:00
|
|
|
[ = not ] subset-with ;
|
2004-08-03 02:08:11 -04:00
|
|
|
|
2005-02-01 21:47:10 -05:00
|
|
|
: remq ( obj list -- list )
|
|
|
|
|
#! Remove all occurrences of the object from the list.
|
|
|
|
|
[ eq? not ] subset-with ;
|
|
|
|
|
|
2004-10-28 23:58:23 -04:00
|
|
|
: prune ( list -- list )
|
|
|
|
|
#! Remove duplicate elements.
|
2005-04-25 19:54:21 -04:00
|
|
|
dup [ uncons prune unique ] when ;
|
2004-10-28 23:58:23 -04:00
|
|
|
|
2004-11-17 20:59:28 -05:00
|
|
|
: all=? ( list -- ? )
|
|
|
|
|
#! Check if all elements of a list are equal.
|
2005-04-19 20:28:01 -04:00
|
|
|
[ uncons [ = ] all-with? ] [ t ] ifte* ;
|
2004-11-17 20:59:28 -05:00
|
|
|
|
2004-12-18 23:18:32 -05:00
|
|
|
M: cons = ( obj cons -- ? )
|
2004-09-28 00:24:36 -04:00
|
|
|
2dup eq? [
|
|
|
|
|
2drop t
|
|
|
|
|
] [
|
2004-11-04 21:36:33 -05:00
|
|
|
over cons? [
|
|
|
|
|
2dup 2car = >r 2cdr = r> and
|
|
|
|
|
] [
|
|
|
|
|
2drop f
|
|
|
|
|
] ifte
|
2004-09-28 00:24:36 -04:00
|
|
|
] ifte ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-04-03 16:55:56 -04:00
|
|
|
M: f = ( obj f -- ? ) eq? ;
|
|
|
|
|
|
2005-01-14 14:56:19 -05:00
|
|
|
M: cons hashcode ( cons -- hash ) car hashcode ;
|
2004-09-30 21:49:49 -04:00
|
|
|
|
2004-12-02 22:44:36 -05:00
|
|
|
: count ( n -- [ 0 ... n-1 ] )
|
2005-05-16 01:15:48 -04:00
|
|
|
0 swap <range> >list ;
|
2005-01-23 16:47:28 -05:00
|
|
|
|
|
|
|
|
: project ( n quot -- list )
|
|
|
|
|
>r count r> map ; inline
|
2004-12-23 01:14:07 -05:00
|
|
|
|
2005-02-17 19:01:11 -05:00
|
|
|
: project-with ( elt n quot -- list )
|
|
|
|
|
swap [ with rot ] project 2nip ; inline
|
|
|
|
|
|
2005-05-18 16:26:22 -04:00
|
|
|
M: general-list head ( n list -- list )
|
2004-12-23 01:14:07 -05:00
|
|
|
#! Return the first n elements of the list.
|
2005-05-18 16:26:22 -04:00
|
|
|
over 0 > [
|
|
|
|
|
unswons >r >r 1 - r> head r> swons
|
|
|
|
|
] [
|
|
|
|
|
2drop f
|
|
|
|
|
] ifte ;
|
2004-12-23 02:14:40 -05:00
|
|
|
|
2005-05-18 16:26:22 -04:00
|
|
|
M: general-list tail ( n list -- tail )
|
2004-12-23 02:14:40 -05:00
|
|
|
#! Return the rest of the list, from the nth index onward.
|
2005-05-18 16:26:22 -04:00
|
|
|
swap [ cdr ] times ;
|
2004-12-23 02:14:40 -05:00
|
|
|
|
2005-05-03 04:40:13 -04:00
|
|
|
M: cons nth ( n list -- element )
|
|
|
|
|
over 0 = [ nip car ] [ >r 1 - r> cdr nth ] ifte ;
|
2005-04-26 00:35:55 -04:00
|
|
|
|
2004-12-23 02:14:40 -05:00
|
|
|
: intersection ( list list -- list )
|
|
|
|
|
#! Make a list of elements that occur in both lists.
|
|
|
|
|
[ over contains? ] subset nip ;
|
2005-01-13 14:41:08 -05:00
|
|
|
|
|
|
|
|
: difference ( list1 list2 -- list )
|
|
|
|
|
#! Make a list of elements that occur in list2 but not
|
|
|
|
|
#! list1.
|
|
|
|
|
[ over contains? not ] subset nip ;
|
2005-02-25 18:11:10 -05:00
|
|
|
|
2005-04-21 00:49:19 -04:00
|
|
|
: contained? ( list1 list2 -- ? )
|
|
|
|
|
#! Is every element of list1 in list2?
|
|
|
|
|
swap [ swap contains? ] all-with? ;
|
|
|
|
|
|
2005-02-25 18:11:10 -05:00
|
|
|
: <queue> ( -- queue )
|
|
|
|
|
#! Make a new functional queue.
|
|
|
|
|
[[ [ ] [ ] ]] ;
|
|
|
|
|
|
|
|
|
|
: queue-empty? ( queue -- ? )
|
|
|
|
|
uncons or not ;
|
|
|
|
|
|
|
|
|
|
: enque ( obj queue -- queue )
|
|
|
|
|
uncons >r cons r> cons ;
|
|
|
|
|
|
|
|
|
|
: deque ( queue -- obj queue )
|
|
|
|
|
uncons [
|
|
|
|
|
uncons swapd cons
|
|
|
|
|
] [
|
|
|
|
|
reverse uncons f swons
|
|
|
|
|
] ifte* ;
|