2009-03-23 18:12:41 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								! Copyright (C) 2007, 2009 Slava Pestov, Daniel Ehrenberg.  
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								! See http://factorcode.org/license.txt for BSD license.  
						 
					
						
							
								
									
										
										
										
											2009-03-23 18:12:41 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								USING:  help.syntax  help.markup  words  quotations  effects  ;
  
						 
					
						
							
								
									
										
										
										
											2008-01-06 22:36:34 -05:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								IN:  memoize  
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2009-04-23 04:48:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								ARTICLE: "memoize"  "Memoization" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"The "  { $vocab-link "memoize"  } " vocabulary implements a simple form of memoization, which is when a word caches results for every unique set of inputs that is supplied. Calling a memoized word with the same inputs more than once does not recalculate anything."  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$nl
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Memoization is useful in situations where the set of possible inputs is small, but the results are expensive to compute and should be cached. Memoized words should not have any side effects."  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								$nl
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								"Defining a memoized word at parse time:"  
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections POSTPONE:  MEMO:  }
							 
						 
					
						
							
								
									
										
										
										
											2009-04-23 04:48:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"Defining a memoized word at run time:"  
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections define-memoized }
							 
						 
					
						
							
								
									
										
										
										
											2009-04-23 04:48:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								"Clearing memoized results:"  
						 
					
						
							
								
									
										
										
										
											2009-10-01 15:56:36 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $subsections reset-memoized } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2009-04-23 04:48:32 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								ABOUT: "memoize" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								HELP:  define-memoized  
						 
					
						
							
								
									
										
										
										
											2009-03-23 18:12:41 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "word"  word } { "quot"  quotation } { "effect"  effect } }
							 
						 
					
						
							
								
									
										
										
										
											2009-10-08 13:24:07 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $description "Defines the given word at run time as one which memoizes its outputs given a particular input."  } ;
 
							 
						 
					
						
							
								
									
										
										
										
											2007-09-20 18:09:08 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								HELP:  MEMO:  
						 
					
						
							
								
									
										
										
										
											2011-01-23 14:00:17 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $syntax "MEMO: word ( stack -- effect ) definition... ;"  }
							 
						 
					
						
							
								
									
										
										
										
											2011-01-26 04:53:12 -05:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $values { "word"  "a new word to define"  } { "definition"  "a word definition"  } }
							 
						 
					
						
							
								
									
										
										
										
											2009-10-08 13:24:07 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								{ $description "Defines the given word at parse time as one which memoizes its output given a particular input. The stack effect is mandatory."  } ;
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								{ define-memoized POSTPONE:  MEMO:  } related-words