]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences.windowed: Add vocabulary for doing windowed sequences that look
authorDoug Coleman <doug.coleman@gmail.com>
Thu, 21 Jun 2012 04:40:38 +0000 (21:40 -0700)
committerDoug Coleman <doug.coleman@gmail.com>
Thu, 21 Jun 2012 04:42:16 +0000 (21:42 -0700)
n steps back. Different from clumps in that maping over them gives you a
sequence that's as long as the underlying sequence.

basis/sequences/windowed/windowed-docs.factor [new file with mode: 0644]
basis/sequences/windowed/windowed-tests.factor [new file with mode: 0644]
basis/sequences/windowed/windowed.factor [new file with mode: 0644]

diff --git a/basis/sequences/windowed/windowed-docs.factor b/basis/sequences/windowed/windowed-docs.factor
new file mode 100644 (file)
index 0000000..f839012
--- /dev/null
@@ -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"
diff --git a/basis/sequences/windowed/windowed-tests.factor b/basis/sequences/windowed/windowed-tests.factor
new file mode 100644 (file)
index 0000000..cb1ac0e
--- /dev/null
@@ -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
diff --git a/basis/sequences/windowed/windowed.factor b/basis/sequences/windowed/windowed.factor
new file mode 100644 (file)
index 0000000..5dd986c
--- /dev/null
@@ -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 ;
\ No newline at end of file