"{ { 1 2 } { 3 4 } }"
}
{ $example
- "USING: grouping ;"
+ "USING: grouping prettyprint sequences ;"
"{ 1 2 3 4 } dup"
"2 <groups> concat sequence= ."
"t"
}
{ "With clumps, collecting the first element of each subsequence but the last one, together with the last subsequence, yields the original sequence:"
{ $example
- "USING: grouping ;"
+ "USING: grouping prettyprint ;"
"{ 1 2 3 4 } 2 clump ."
"{ { 1 2 } { 2 3 } { 3 4 } }"
}
{ $example
- "USING: grouping assocs sequences ;"
+ "USING: grouping assocs sequences prettyprint ;"
"{ 1 2 3 4 } dup"
"2 <clumps> unclip-last [ keys ] dip append sequence= ."
"t"
}
{ "With circular clumps, collecting the first element of each subsequence yields the original sequence. Collecting the " { $snippet "n" } "th element of each subsequence would rotate the original sequence " { $snippet "n" } " elements rightward:"
{ $example
- "USING: grouping ;"
+ "USING: grouping prettyprint ;"
"{ 1 2 3 4 } 2 circular-clump ."
"{ { 1 2 } { 2 3 } { 3 4 } { 4 1 } }"
}
{ $example
- "USING: grouping assocs sequences ;"
+ "USING: grouping assocs sequences prettyprint ;"
"{ 1 2 3 4 } dup"
"2 <circular-clumps> keys sequence= ."
"t"
}
{ $example
- "USING: grouping ;"
+ "USING: grouping prettyprint ;"
"{ 1 2 3 4 }"
"2 <circular-clumps> [ second ] { } map-as ."
"{ 2 3 4 1 }"
$nl
"You can perform an operation on each element of an array:"
{ $example
+ "USING: io sequences prettyprint ;"
"{ 1 2 3 } [ \"The number is \" write . ] each"
"The number is 1\nThe number is 2\nThe number is 3"
}
{ $code ": subtract-n ( seq n -- seq' ) swap [ over - ] map nip ;" }
"Three shuffle words are required to pass the value around. Instead, the loop-invariant value can be partially applied to a quotation using " { $link curry } ", yielding a new quotation that is passed to " { $link map } ":"
{ $example
+ "USING: sequences prettyprint ;"
": subtract-n ( seq n -- seq' ) [ - ] curry map ;"
"{ 10 20 30 } 5 subtract-n ."
"{ 5 15 25 }"
: ?nth-of ( seq n -- elt/f ) swap ?nth ; inline
-: ??nth-of ( seq n -- elt ? ) swap ??nth ; inline
+: ??nth-of ( seq n -- elt/f ? ) swap ??nth ; inline
: index-or-length ( seq n -- seq n' ) over length min ; inline