]> gitweb.factorcode.org Git - factor.git/commitdiff
some combinators cleaned up, whatever else
authorSlava Pestov <slava@factorcode.org>
Tue, 31 Aug 2004 04:27:09 +0000 (04:27 +0000)
committerSlava Pestov <slava@factorcode.org>
Tue, 31 Aug 2004 04:27:09 +0000 (04:27 +0000)
TODO.FACTOR.txt
library/combinators.factor
library/hashtables.factor
library/math/math-combinators.factor
library/test/benchmark/empty-loop.factor

index 841f62c1ba9face812692f10fbebf1d5c123c2d1..83977b260a0de92bfc55f3de80ceb11ebfa2a9d8 100644 (file)
 + misc:\r
 \r
 - directory listings:\r
+  - nicer way to combine two paths\r
   - icons for file responder\r
   - cwd, cd, pwd, dir., pwd. words\r
 - -1.1 3 ^ shouldn't give a complex number\r
 - don't rehash strings on every startup\r
 - 'cascading' styles\r
 - jedit ==> jedit-word, jedit takes a file name\r
-- rethink strhead/strtail&co\r
+- introduce ifte* and ?str-head/?str-tail where appropriate\r
 - namespace clone drops static var bindings\r
 - ditch expand\r
 - set-object-path\r
index 5e2f418451be8236721544a30c62ba52a16dd25c..de52ff0e5b79c39b813056e91501f00f91085e5f 100644 (file)
@@ -30,13 +30,13 @@ USE: kernel
 USE: lists
 USE: stack
 
-: 2apply ( x y [ code ] -- )
+: 2apply ( x y quot -- )
     #! First applies the code to x, then to y.
     #!
     #! If the quotation compiles, this combinator compiles.
     2dup >r >r nip call r> r> call ; inline interpret-only
 
-: cleave ( x [ code1 ] [ code2 ] -- )
+: cleave ( x quot quot -- )
     #! Executes each quotation, with x on top of the stack.
     #!
     #! If the quotation compiles, this combinator compiles.
@@ -54,7 +54,7 @@ USE: stack
     #! If the quotation compiles, this combinator compiles.
     -rot >r >r call r> r> ; inline interpret-only
 
-: forever ( code -- )
+: forever ( quot -- )
     #! The code is evaluated in an infinite loop. Typically, a
     #! continuation is used to escape the infinite loop.
     #!
@@ -102,7 +102,7 @@ USE: stack
     pick [ drop call ] [ nip nip call ] ifte ;
     inline interpret-only
 
-: interleave ( X list -- )
+: interleave ( X quot -- )
     #! Evaluate each element of the list with X on top of the
     #! stack. When done, X is popped off the stack.
     #!
@@ -116,7 +116,7 @@ USE: stack
         2drop
     ] ifte ; interpret-only
 
-: unless ( cond [ if false ] -- )
+: unless ( cond quot -- )
     #! Execute a quotation only when the condition is f. The
     #! condition is popped off the stack.
     #!
@@ -124,7 +124,7 @@ USE: stack
     #! values as it produces.
     [ ] swap ifte ; inline interpret-only
 
-: unless* ( cond [ if false ] -- )
+: unless* ( cond quot -- )
     #! If cond is f, pop it off the stack and evaluate the
     #! quotation. Otherwise, leave cond on the stack.
     #!
@@ -132,7 +132,7 @@ USE: stack
     #! value than it produces.
     over [ drop ] [ nip call ] ifte ; inline interpret-only
 
-: when ( cond [ if true ] -- )
+: when ( cond quot -- )
     #! Execute a quotation only when the condition is not f. The
     #! condition is popped off the stack.
     #!
@@ -140,7 +140,7 @@ USE: stack
     #! values as it produces.
     [ ] ifte ; inline interpret-only
 
-: when* ( cond [ code ] -- )
+: when* ( cond quot -- )
     #! If the condition is true, it is left on the stack, and
     #! the quotation is evaluated. Otherwise, the condition is
     #! popped off the stack.
@@ -149,12 +149,13 @@ USE: stack
     #! value than it produces.
     dupd [ drop ] ifte ; inline interpret-only
 
-: while ( [ P ] [ R ] -- )
-    #! Evaluate P. If it leaves t on the stack, evaluate R, and
-    #! recurse.
+: while ( cond body -- )
+    #! Evaluate cond. If it leaves t on the stack, evaluate
+    #! body, and recurse.
     #!
-    #! In order to compile, the stack effect of P * ( X -- ) * R
-    #! must consume as many values as it produces.
+    #! In order to compile, the stack effect of
+    #! cond * ( X -- ) * body must consume as many values as
+    #! it produces.
     2dup >r >r >r call [
         r> call r> r> while
     ] [
index d64faf75fe3aa6e2e8d24216ad38d033af0f903a..18f71b5473402d6b9539186b24e6868fef05f71a 100644 (file)
@@ -84,10 +84,13 @@ USE: vectors
     #! Push a list of keys in a hashtable.
     [ ] swap [ car swons ] hash-each ;
 
-: hash-values ( hash -- list )
+: hash-values ( hash -- alist )
     #! Push a list of values in a hashtable.
     [ ] swap [ cdr swons ] hash-each ;
 
 : hash>alist ( hash -- list )
     #! Push a list of key/value pairs in a hashtable.
     [ ] swap [ swons ] hash-each ;
+
+: alist>hash ( alist -- hash )
+    37 <hashtable> swap [ unswons pick set-hash ] each ;
index ee234742f257e9cd5e03092c1b53567f2588181f..297297933915573259f31211523773fe4f107915 100644 (file)
@@ -30,27 +30,28 @@ USE: combinators
 USE: kernel
 USE: stack
 
-: times ( n [ code ] -- )
+: times ( n quot -- )
     #! Evaluate a quotation n times.
     #!
     #! In order to compile, the code must produce as many values
     #! as it consumes.
-    [
-        over 0 >
+    tuck >r dup 0 <= [
+        r> 3drop
     ] [
-        tuck >r pred >r call r> r>
-    ] while 2drop ; inline interpret-only
+        pred >r call r> r> times
+    ] ifte ; inline interpret-only
 
-: times* ( n [ code ] -- )
+: (times) ( limit n quot -- )
+    pick pick <= [
+        3drop
+    ] [
+        tuck >r tuck >r rot >r call r> r> succ r> (times)
+    ] ifte ; inline interpret-only
+
+: times* ( n quot -- )
     #! Evaluate a quotation n times, pushing the index at each
     #! iteration. The index ranges from 0 to n-1.
     #!
     #! In order to compile, the code must consume one more value
     #! than it produces.
-    0 rot
-    [
-        2dup <
-    ] [
-        >r 2dup succ >r >r swap call r> r> r>
-    ] while
-    3drop ; inline interpret-only
+    0 swap (times) ; inline interpret-only
index 87bf850dd2df39cfe964aeddf5a369aeec72a267..531a015598d966ee4f73e5006d47f9bde7fd17eb 100644 (file)
@@ -1,5 +1,7 @@
 IN: scratchpad
 USE: math
+USE: stack
 USE: test
 
-[ 2000000 [ ] times ] time
+[ 5000000 [ ] times ] time
+[ 5000000 [ drop ] times* ] time