<commit_before>! Copyright (C) 2012 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors kernel math math.order sequences ;
IN: sequences.windowed

TUPLE: windowed-sequence { sequence sequence read-only } { n integer } ;

INSTANCE: windowed-sequence sequence

C: <windowed-sequence> windowed-sequence
        
: in-bound ( n sequence -- n' )
    [ drop 0 ] [ length ] bi clamp ; inline

: in-bounds ( a b sequence -- a' b' sequence )
    [ nip in-bound ]
    [ [ nip ] dip in-bound ]
    [ 2nip ] 3tri ;
    
M: windowed-sequence nth
    [ [ 1 + ] dip n>> [ - ] [ drop ] 2bi ]
    [ nip sequence>> in-bounds <slice> ] 2bi ;
    
M: windowed-sequence length
    sequence>> length ;<commit_msg>sequences.windowed: faster windows and rolling words.<commit_after>! Copyright (C) 2012 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors fry kernel math math.order math.statistics
sequences sequences.private ;
IN: sequences.windowed

TUPLE: windowed-sequence
    { sequence sequence read-only }
    { n integer } ;

INSTANCE: windowed-sequence sequence

C: <windowed-sequence> windowed-sequence

M: windowed-sequence nth-unsafe
    [ 1 + ] dip [ n>> dupd [-] swap ] [ sequence>> ] bi <slice> ; inline

M: windowed-sequence length
    sequence>> length ; inline

: in-bound ( n sequence -- n' )
    [ drop 0 ] [ length ] bi clamp ; inline

: in-bounds ( a b sequence -- a' b' sequence )
    [ nip in-bound ] [ [ nip ] dip in-bound ] [ 2nip ] 3tri ;

: rolling-map ( seq n quot: ( slice -- elt ) -- newseq )
    [ <windowed-sequence> ] [ map ] bi* ; inline

: rolling-sum ( seq n -- newseq )
    [ sum ] rolling-map ;

: rolling-mean ( seq n -- newseq )
    [ mean ] rolling-map ;

: rolling-median ( seq n -- newseq )
    [ median ] rolling-map ;

: rolling-supremum ( seq n -- newseq )
    [ supremum ] rolling-map ;

: rolling-infimum ( seq n -- newseq )
    [ infimum ] rolling-map ;

: rolling-count ( ... u n quot: ( ... elt -- ... ? ) -- ... v )
    '[ _ count ] rolling-map ; inline
{'fraction_non_alphanumeric': 0.14299706170421156, 'fraction_numerical': 0.00881488736532811, 'mean_word_length': 2.615929203539823, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2544185', 'n_tokens_mistral': 697, 'n_tokens_neox': 671, 'n_words': 204}