diff --git a/basis/sequences/windowed/windowed-docs.factor b/basis/sequences/windowed/windowed-docs.factor new file mode 100644 index 0000000000..f839012265 --- /dev/null +++ b/basis/sequences/windowed/windowed-docs.factor @@ -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: +{ $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 } "." } ; + +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 } +"Helper words for creating bounds-checked slices:" +{ $subsections in-bound in-bounds } ; + +ABOUT: "sequences.windowed" diff --git a/basis/sequences/windowed/windowed-tests.factor b/basis/sequences/windowed/windowed-tests.factor new file mode 100644 index 0000000000..cb1ac0e1b1 --- /dev/null +++ b/basis/sequences/windowed/windowed-tests.factor @@ -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 [ >array ] map ] unit-test + +{ 6 } +[ { 1 2 3 4 5 6 } 3 length ] unit-test + +{ { 1 1 1 2 3 4 } } +[ { 1 2 3 4 5 6 } 3 [ infimum ] map ] unit-test diff --git a/basis/sequences/windowed/windowed.factor b/basis/sequences/windowed/windowed.factor new file mode 100644 index 0000000000..5dd986cb19 --- /dev/null +++ b/basis/sequences/windowed/windowed.factor @@ -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 + +: 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 ] 2bi ; + +M: windowed-sequence length + sequence>> length ; \ No newline at end of file