Paste: consecutives

Author: Jon
Mode: factor
Date: Wed, 1 Nov 2017 19:46:32
Plain Text |
USING: arrays assocs combinators kernel math sequences sorting ;
IN: gcj.2017.finals.A

: consecutives-markers ( seq -- seq' )
    dup rest
    [ [ = ] [ 1 - = ] 2bi or ] 2map
    [ f suffix ]
    [ f prefix ] bi zip ;

: consecutives-indices ( seq -- indices ) 
    consecutives-markers
    [ V{ } clone ] dip
    [ swap {
        { { f f } [ dup 2array over push ] }
        { { t f } [ over push ] }
        { { f t } [ over pop swap 2array over push ] }
        [ 2drop ]
    } case ] each-index ;
: consecutives ( seq -- seqs )
    natural-sort 
    [ consecutives-indices ] keep
    [ [ nth ] curry map ] curry map ;
    
{ V{
    { 4 4 }
    { 6 9 }
    { 12 13 }
} } [ { 4 6 7 8 8 8 8 9 12 12 13 } consecutives ] unit-test

New Annotation

Summary:
Author:
Mode:
Body: