sequences.windowed: Add vocabulary for doing windowed sequences that look
n steps back. Different from clumps in that maping over them gives you a sequence that's as long as the underlying sequence.db4
parent
46c5d64251
commit
80eae10aa0
|
@ -0,0 +1,36 @@
|
|||
! Copyright (C) 2012 Doug Coleman.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: help.markup help.syntax kernel math sequences ;
|
||||
IN: sequences.windowed
|
||||
|
||||
HELP: <windowed-sequence>
|
||||
{ $values
|
||||
{ "sequence" sequence } { "n" sequence }
|
||||
{ "windowed-sequence" windowed-sequence }
|
||||
}
|
||||
{ $description "Create a new windowed sequence of window size " { $snippet "n" } " over " { $snippet "sequence" } "." } ;
|
||||
|
||||
HELP: in-bound
|
||||
{ $values
|
||||
{ "n" integer } { "sequence" sequence }
|
||||
{ "n'" integer }
|
||||
}
|
||||
{ $description "Clamps an integer from 0 to the sequence length." } ;
|
||||
|
||||
HELP: in-bounds
|
||||
{ $values
|
||||
{ "a" sequence } { "b" sequence } { "sequence" sequence }
|
||||
{ "a'" sequence } { "b'" sequence }
|
||||
}
|
||||
{ $description "Clamps two integers from 0 to the sequence length. While not in bounds for calling " { $link nth } ", these integers are in bounds for calling " { $link <slice> } "." } ;
|
||||
|
||||
ARTICLE: "sequences.windowed" "Windowed sequences"
|
||||
|
||||
"The " { $vocab-link "sequences.windowed" } " vocabulary provides a read-only virtual sequence whose elements are slices of length " { $snippet "n" } " from the current element looking backwards, inclusive of the current element. Slices may be less than " { $snippet "n" } " elements in length, especially at the head of the sequence, where the first slice will be of length 1." $nl
|
||||
"Windowed sequences support " { $link nth } " and " { $link length } " from the " { $link "sequence-protocol" } "." $nl
|
||||
"Creating a windowed sequence:"
|
||||
{ $subsections <windowed-sequence> }
|
||||
"Helper words for creating bounds-checked slices:"
|
||||
{ $subsections in-bound in-bounds } ;
|
||||
|
||||
ABOUT: "sequences.windowed"
|
|
@ -0,0 +1,13 @@
|
|||
! Copyright (C) 2012 Doug Coleman.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: arrays sequences sequences.windowed tools.test ;
|
||||
IN: sequences.windowed.tests
|
||||
|
||||
{ { { 1 } { 1 2 } { 1 2 3 } { 2 3 4 } { 3 4 5 } { 4 5 6 } } }
|
||||
[ { 1 2 3 4 5 6 } 3 <windowed-sequence> [ >array ] map ] unit-test
|
||||
|
||||
{ 6 }
|
||||
[ { 1 2 3 4 5 6 } 3 <windowed-sequence> length ] unit-test
|
||||
|
||||
{ { 1 1 1 2 3 4 } }
|
||||
[ { 1 2 3 4 5 6 } 3 <windowed-sequence> [ infimum ] map ] unit-test
|
|
@ -0,0 +1,25 @@
|
|||
! Copyright (C) 2012 Doug Coleman.
|
||||
! 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 ;
|
Loading…
Reference in New Issue