@@ -44,6 +44,32 @@ tail ( seq n -- tailseq ) ! drop first n
4444{ 1 2 3 4 5 } 3 tail . ! => { 4 5 }
4545```
4646
47+ ## Padding
48+
49+ ```
50+ pad-head ( seq n elt -- padded ) ! prepend copies of elt
51+ pad-tail ( seq n elt -- padded ) ! append copies of elt
52+ ```
53+
54+ Both extend ` seq ` until its length is at least ` n ` . If ` seq ` is
55+ already that long, it is returned unchanged.
56+
57+ ``` factor
58+ { 2 5 0 } 6 0 pad-tail . ! => { 2 5 0 0 0 0 }
59+ { 4 1 } 5 9 pad-head . ! => { 9 9 9 4 1 }
60+ ```
61+
62+ ## The same words work on strings
63+
64+ A string is a sequence of characters, so everything above works on
65+ strings too — the result is just another string instead of an array:
66+
67+ ``` factor
68+ "hello" length . ! => 5
69+ "hello" 3 head . ! => "hel"
70+ "abc" 6 CHAR: . pad-tail . ! => "abc..."
71+ ```
72+
4773## Aggregating
4874
4975` sum ` (in [ ` math.statistics ` ] [ math.statistics ] ) adds the elements of
@@ -61,7 +87,7 @@ count ( seq quot -- n )
6187```
6288
6389``` factor
64- { 2 5 0 7 4 1 } [ 5 >= ] count . ! => 2
90+ { 2 5 0 7 4 1 } [ even? ] count . ! => 3
6591```
6692
6793## Predicates over the whole sequence
@@ -73,7 +99,7 @@ empty? ( seq -- ? )
7399```
74100
75101``` factor
76- { 2 5 0 7 } [ zero? ] any? . ! => t
102+ { 2 5 0 7 } [ 4 > ] any? . ! => t
77103{ 2 5 0 7 } [ 0 > ] all? . ! => f
78104```
79105
@@ -89,8 +115,8 @@ suffix ( seq elt -- newseq )
89115```
90116
91117``` factor
92- { 2 5 0 7 4 1 } unclip-last 1 + suffix .
93- ! => { 2 5 0 7 4 2 }
118+ { 10 20 30 } unclip-last 2 * suffix .
119+ ! => { 10 20 60 }
94120```
95121
96122[ sequences ] : https://docs.factorcode.org/content/vocab-sequences.html
0 commit comments