factor/core/splitting/splitting.factor

110 lines
2.8 KiB
Factor
Raw Normal View History

2008-05-05 01:18:35 -04:00
! Copyright (C) 2005, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: kernel math namespaces strings arrays vectors sequences
2008-05-05 01:18:35 -04:00
sets math.order accessors ;
2007-09-20 18:09:08 -04:00
IN: splitting
2008-05-05 01:18:35 -04:00
TUPLE: abstract-groups seq n ;
2007-09-20 18:09:08 -04:00
2008-05-05 01:18:35 -04:00
: check-groups dup 0 <= [ "Invalid group count" throw ] when ; inline
2007-09-20 18:09:08 -04:00
2008-05-05 01:18:35 -04:00
: construct-groups ( seq n class -- groups )
>r check-groups r> boa ; inline
2007-09-20 18:09:08 -04:00
2008-05-05 01:18:35 -04:00
GENERIC: group@ ( n groups -- from to seq )
M: abstract-groups nth group@ subseq ;
M: abstract-groups set-nth group@ <slice> 0 swap copy ;
M: abstract-groups like drop { } like ;
INSTANCE: abstract-groups sequence
TUPLE: groups < abstract-groups ;
: <groups> ( seq n -- groups )
groups construct-groups ; inline
2007-09-20 18:09:08 -04:00
M: groups length
2008-05-05 01:18:35 -04:00
[ seq>> length ] [ n>> ] bi [ + 1- ] keep /i ;
2007-09-20 18:09:08 -04:00
M: groups set-length
2008-05-05 01:18:35 -04:00
[ n>> * ] [ seq>> ] bi set-length ;
M: groups group@
[ n>> [ * dup ] keep + ] [ seq>> ] bi [ length min ] keep ;
TUPLE: sliced-groups < groups ;
: <sliced-groups> ( seq n -- groups )
sliced-groups construct-groups ; inline
M: sliced-groups nth group@ <slice> ;
TUPLE: sliding-groups < abstract-groups ;
: <sliding-groups> ( seq n -- groups )
sliding-groups construct-groups ; inline
M: sliding-groups length
[ seq>> length ] [ n>> ] bi - 1+ ;
2007-09-20 18:09:08 -04:00
2008-05-05 01:18:35 -04:00
M: sliding-groups set-length
[ n>> + 1- ] [ seq>> ] bi set-length ;
2007-09-20 18:09:08 -04:00
2008-05-05 01:18:35 -04:00
M: sliding-groups group@
[ n>> over + ] [ seq>> ] bi ;
2007-09-20 18:09:08 -04:00
2008-05-05 01:18:35 -04:00
TUPLE: sliced-sliding-groups < groups ;
2007-09-20 18:09:08 -04:00
2008-05-05 01:18:35 -04:00
: <sliced-sliding-groups> ( seq n -- groups )
sliced-sliding-groups construct-groups ; inline
2007-09-20 18:09:08 -04:00
2008-05-05 01:18:35 -04:00
M: sliced-sliding-groups nth group@ <slice> ;
2007-09-20 18:09:08 -04:00
: group ( seq n -- array ) <groups> { } like ;
: ?head ( seq begin -- newseq ? )
2dup head? [ length tail t ] [ drop f ] if ;
: ?head-slice ( seq begin -- newseq ? )
2dup head? [ length tail-slice t ] [ drop f ] if ;
: ?tail ( seq end -- newseq ? )
2dup tail? [ length head* t ] [ drop f ] if ;
: ?tail-slice ( seq end -- newseq ? )
2dup tail? [ length head-slice* t ] [ drop f ] if ;
: split1 ( seq subseq -- before after )
dup pick start dup [
[ >r over r> head -rot length ] keep + tail
] [
2drop f
] if ;
: last-split1 ( seq subseq -- before after )
2008-03-29 21:36:58 -04:00
[ <reversed> ] bi@ split1 [ reverse ] bi@
2007-09-20 18:09:08 -04:00
dup [ swap ] when ;
: (split) ( separators n seq -- )
3dup rot [ member? ] curry find-from drop
2007-09-20 18:09:08 -04:00
[ [ swap subseq , ] 2keep 1+ swap (split) ]
[ swap dup zero? [ drop ] [ tail ] if , drop ] if* ; inline
: split, ( seq separators -- ) 0 rot (split) ;
: split ( seq separators -- pieces ) [ split, ] { } make ;
: string-lines ( str -- seq )
dup "\r\n" intersect empty? [
2008-03-11 04:30:14 -04:00
1array
] [
2007-09-20 18:09:08 -04:00
"\n" split [
1 head-slice* [
"\r" ?tail drop "\r" split
] map
] keep peek "\r" split suffix concat
2007-09-20 18:09:08 -04:00
] if ;