generic, sequences: update docs for iota
							parent
							
								
									ad6d8556e0
								
							
						
					
					
						commit
						e8378b3dc6
					
				| 
						 | 
					@ -11,15 +11,13 @@ $nl
 | 
				
			||||||
{ $code
 | 
					{ $code
 | 
				
			||||||
    "GENERIC: explain ( object -- )"
 | 
					    "GENERIC: explain ( object -- )"
 | 
				
			||||||
    "M: object explain drop \"an object\" print ;"
 | 
					    "M: object explain drop \"an object\" print ;"
 | 
				
			||||||
    "M: number explain drop \"a number\" print ;"
 | 
					    "M: generic explain drop \"a class word\" print ;"
 | 
				
			||||||
    "M: sequence explain drop \"a sequence\" print ;"
 | 
					    "M: class explain drop \"a generic word\" print ;"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
"The linear order is the following, from least-specific to most-specific:"
 | 
					"The linear order is the following, from least-specific to most-specific:"
 | 
				
			||||||
{ $code "{ object sequence number }" }
 | 
					{ $code "{ object generic class }" }
 | 
				
			||||||
"Neither " { $link number } " nor " { $link sequence } " are subclasses of each other, yet their intersection is the non-empty " { $link integer } " class. Calling " { $snippet "explain" } " with an integer on the stack will print " { $snippet "a number" } " because " { $link number } " precedes " { $link sequence } " in the class linearization order. If this was not the desired outcome, define a method on the intersection:"
 | 
					"Neither " { $link class } " nor " { $link generic } " are subclasses of each other, and their intersection is non-empty. Calling " { $snippet "explain" } " with a word on the stack that is both a class and a generic word will print " { $snippet "a generic word" } " because " { $link class } " precedes " { $link generic } " in the class linearization order. (One example of a word which is both a class and a generic word is the class of classes, " { $link class } ", which is also a word to get the class of an object.)"
 | 
				
			||||||
{ $code "M: integer explain drop \"an integer\" print ;" }
 | 
					$nl
 | 
				
			||||||
"Now, the linear order is the following, from least-specific to most-specific:"
 | 
					 | 
				
			||||||
{ $code "{ object sequence number integer }" }
 | 
					 | 
				
			||||||
"The " { $link order } " word can be useful to clarify method dispatch order:"
 | 
					"The " { $link order } " word can be useful to clarify method dispatch order:"
 | 
				
			||||||
{ $subsections order } ;
 | 
					{ $subsections order } ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1409,17 +1409,20 @@ ARTICLE: "virtual-sequences" "Virtual sequences"
 | 
				
			||||||
"A virtual sequence is an implementation of the " { $link "sequence-protocol" } " which does not store its own elements, and instead computes them, either from scratch or by retrieving them from another sequence."
 | 
					"A virtual sequence is an implementation of the " { $link "sequence-protocol" } " which does not store its own elements, and instead computes them, either from scratch or by retrieving them from another sequence."
 | 
				
			||||||
$nl
 | 
					$nl
 | 
				
			||||||
"Implementations include the following:"
 | 
					"Implementations include the following:"
 | 
				
			||||||
{ $subsections reversed slice iota }
 | 
					{ $subsections reversed slice }
 | 
				
			||||||
"Virtual sequences can be implemented with the " { $link "virtual-sequences-protocol" } ", by translating an index in the virtual sequence into an index in another sequence." ;
 | 
					"Virtual sequences can be implemented with the " { $link "virtual-sequences-protocol" } ", by translating an index in the virtual sequence into an index in another sequence."
 | 
				
			||||||
 | 
					{ $see-also "sequences-integers" } ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ARTICLE: "sequences-integers" "Counted loops"
 | 
					ARTICLE: "sequences-integers" "Counted loops"
 | 
				
			||||||
"Integers support the sequence protocol in a trivial fashion; a non-negative integer presents its non-negative predecessors as elements. For example, the integer 3, when viewed as a sequence, contains the elements 0, 1, and 2. This is very useful for performing counted loops."
 | 
					"A virtual sequence is defined for iterating over integers from zero."
 | 
				
			||||||
 | 
					{ $subsection iota }
 | 
				
			||||||
 | 
					"For example, calling " { $link iota } " on the integer 3 produces a sequence containing the elements 0, 1, and 2. This is very useful for performing counted loops."
 | 
				
			||||||
$nl
 | 
					$nl
 | 
				
			||||||
"For example, the " { $link each } " combinator, given an integer, simply calls a quotation that number of times, pushing a counter on each iteration that ranges from 0 up to that integer:"
 | 
					"This means the " { $link each } " combinator, given an integer, simply calls a quotation that number of times, pushing a counter on each iteration that ranges from 0 up to that integer:"
 | 
				
			||||||
{ $example "3 [ . ] each" "0\n1\n2" }
 | 
					{ $example "3 iota [ . ] each" "0\n1\n2" }
 | 
				
			||||||
"A common idiom is to iterate over a sequence, while also maintaining a loop counter. This can be done using " { $link each-index } ", " { $link map-index } " and " { $link reduce-index } "."
 | 
					"A common idiom is to iterate over a sequence, while also maintaining a loop counter. This can be done using " { $link each-index } ", " { $link map-index } " and " { $link reduce-index } "."
 | 
				
			||||||
$nl
 | 
					$nl
 | 
				
			||||||
"Combinators that produce new sequences, such as " { $link map } ", will output an array if the input is an integer."
 | 
					"Combinators that produce new sequences, such as " { $link map } ", will output an array if the input is an instance of " { $link iota } "."
 | 
				
			||||||
$nl
 | 
					$nl
 | 
				
			||||||
"More elaborate counted loops can be performed with " { $link "math.ranges" } "." ;
 | 
					"More elaborate counted loops can be performed with " { $link "math.ranges" } "." ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue