]> gitweb.factorcode.org Git - factor.git/commitdiff
some code cleanups, remove usages of deprecated combinators
authorSlava Pestov <slava@factorcode.org>
Thu, 7 Oct 2004 03:34:22 +0000 (03:34 +0000)
committerSlava Pestov <slava@factorcode.org>
Thu, 7 Oct 2004 03:34:22 +0000 (03:34 +0000)
14 files changed:
library/combinators.factor
library/httpd/url-encoding.factor
library/math/namespace-math.factor
library/math/pow.factor
library/math/quadratic.factor
library/namespaces.factor
library/random.factor
library/sbuf.factor
library/strings.factor
library/test/math/namespaces.factor [new file with mode: 0644]
library/test/random.factor
library/test/strings.factor
library/test/test.factor
library/vector-combinators.factor

index eb01315e8f1d611c7a736b7e3226b0b24f83639b..7ca1d1c1cf78a3bde57c5dc238ac0b1e898605fc 100644 (file)
@@ -30,18 +30,6 @@ USE: kernel
 USE: lists
 USE: stack
 
-: 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 quot quot -- )
-    #! Executes each quotation, with x on top of the stack.
-    #!
-    #! If the quotation compiles, this combinator compiles.
-    >r over >r call r> r> call ; inline interpret-only
-
 : slip ( quot x -- x )
     >r call r> ; inline interpret-only
 
@@ -51,30 +39,15 @@ USE: stack
 : 3slip ( quot x y z -- x y z )
     >r >r >r call r> r> r> ; inline interpret-only
 
-: dip ( a [ b ] -- b a )
-    #! Call b as if b was not present on the stack.
-    #!
-    #! If the quotation compiles, this combinator compiles.
-    swap >r call r> ; inline interpret-only
-
-: 2dip ( a b [ c ] -- c a b )
-    #! Call c as if a and b were not present on the stack.
-    #!
-    #! If the quotation compiles, this combinator compiles.
-    -rot >r >r call r> r> ; inline interpret-only
-
-: forever ( quot -- )
-    #! The code is evaluated in an infinite loop. Typically, a
-    #! continuation is used to escape the infinite loop.
-    #!
-    #! This combinator will not compile.
-    dup dip forever ; interpret-only
-
 : keep ( a quot -- a )
     #! Execute the quotation with a on the stack, and restore a
     #! after the quotation returns.
     over >r call r> ;
 
+: apply ( code input -- code output )
+    #! Apply code to input.
+    swap dup >r call r> swap ;
+
 : cond ( x list -- )
     #! The list is of this form:
     #!
@@ -111,20 +84,6 @@ USE: stack
     pick [ drop call ] [ nip nip call ] ifte ;
     inline interpret-only
 
-: interleave ( X quot -- )
-    #! Evaluate each element of the list with X on top of the
-    #! stack. When done, X is popped off the stack.
-    #!
-    #! To avoid unexpected results, each element of the list
-    #! must have stack effect ( X -- ).
-    #!
-    #! This combinator will not compile.
-    dup [
-        over [ unswons dip ] dip swap interleave
-    ] [
-        2drop
-    ] ifte ; interpret-only
-
 : unless ( cond quot -- )
     #! Execute a quotation only when the condition is f. The
     #! condition is popped off the stack.
@@ -158,6 +117,53 @@ USE: stack
     #! value than it produces.
     over [ call ] [ 2drop ] ifte ; inline interpret-only
 
+: forever ( quot -- )
+    #! The code is evaluated in an infinite loop. Typically, a
+    #! continuation is used to escape the infinite loop.
+    #!
+    #! This combinator will not compile.
+    dup dip forever ; interpret-only
+
+! DEPRECATED
+
+: 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 quot quot -- )
+    #! Executes each quotation, with x on top of the stack.
+    #!
+    #! If the quotation compiles, this combinator compiles.
+    >r over >r call r> r> call ; inline interpret-only
+
+: dip ( a [ b ] -- b a )
+    #! Call b as if b was not present on the stack.
+    #!
+    #! If the quotation compiles, this combinator compiles.
+    swap >r call r> ; inline interpret-only
+
+: 2dip ( a b [ c ] -- c a b )
+    #! Call c as if a and b were not present on the stack.
+    #!
+    #! If the quotation compiles, this combinator compiles.
+    -rot >r >r call r> r> ; inline interpret-only
+
+: interleave ( X quot -- )
+    #! Evaluate each element of the list with X on top of the
+    #! stack. When done, X is popped off the stack.
+    #!
+    #! To avoid unexpected results, each element of the list
+    #! must have stack effect ( X -- ).
+    #!
+    #! This combinator will not compile.
+    dup [
+        over [ unswons dip ] dip swap interleave
+    ] [
+        2drop
+    ] ifte ; interpret-only
+
 : while ( cond body -- )
     #! Evaluate cond. If it leaves t on the stack, evaluate
     #! body, and recurse.
index 3a657ace236488a071b6476be285fe110e6e7e47..e8aa7ff004fa17ea841bf0ca480ab66575880e2d 100644 (file)
@@ -47,14 +47,14 @@ USE: unparser
     ] [
         ! Note that hex> will push f if there is an invalid
         ! hex literal
-        [ succ dup 2 + ] dip substring hex> [ >char % ]  when*
+        >r succ dup 2 + r> substring hex> [ >char % ] when*
     ] ifte ;
 
 : url-decode-% ( index str -- index str )
-    2dup url-decode-hex [ 3 + ] dip ;
+    2dup url-decode-hex >r 3 + r> ;
 
-: url-decode-+-or-other ( index str -- index str )
-    CHAR: + CHAR: \s replace % [ succ ] dip ;
+: url-decode-+-or-other ( index str ch -- index str )
+    CHAR: + CHAR: \s replace % >r succ r> ;
 
 : url-decode-iter ( index str -- )
     2dup str-length >= [
index 3283946428d30fda222be64751b7121cd592092f..93f7248c4723ff09a1c20acd55ac3fb50c9705c7 100644 (file)
@@ -31,10 +31,10 @@ USE: logic
 USE: namespaces
 USE: stack
 
-: +@ ( num var -- ) dup [ get + ] dip set ;
-: -@ ( num var -- ) dup [ get swap - ] dip set ;
-: *@ ( num var -- ) dup [ get * ] dip set ;
-: /@ ( num var -- ) dup [ get / ] dip set ;
+: +@ ( num var -- ) tuck get + put ;
+: -@ ( num var -- ) tuck get swap - put ;
+: *@ ( num var -- ) tuck get * put ;
+: /@ ( num var -- ) tuck get swap / put ;
 : neg@ ( var -- ) dup get neg put ;
 : pred@ ( var -- ) dup get pred put ;
 : succ@ ( var -- ) dup get succ put ;
index a8ffae1d47b8366fff411716dc35980deb45d295..031633212b0c25352a3d53d583580d6076df3da6 100644 (file)
@@ -47,10 +47,10 @@ USE: logic
     ] ifte ;
 
 : ^mag ( w abs arg -- magnitude )
-    [ [ >rect swap ] dip swap fpow ] dip rot * fexp / ;
+    >r >r >rect swap r> swap fpow r> rot * fexp / ;
 
 : ^theta ( w abs arg -- theta )
-    [ [ >rect ] dip flog * swap ] dip * + ;
+    >r >r >rect r> flog * swap r> * + ;
 
 : ^ ( z w -- z^w )
     over real? over integer? and [
index 8200316bdac53e1084a104033565b1736c349929..8ed80ff3c4741c6dfa5c370f74d8bfabe5bc3840 100644 (file)
@@ -31,7 +31,7 @@ USE: math
 USE: stack
 
 : quadratic-complete ( a b c -- a b c a b )
-    [ 2dup ] dip -rot ;
+    >r 2dup r> -rot ;
 
 : quadratic-d ( c a b -- sqrt[b^2 - 4*a*c] )
     sq -rot 4 * * - sqrt ;
@@ -40,7 +40,7 @@ USE: stack
     neg swap / 2 / ;
 
 : quadratic-roots ( a b d -- alpha beta )
-    3dup - quadratic-root [ + quadratic-root ] dip ;
+    3dup - quadratic-root >r + quadratic-root r> ;
 
 : quadratic ( a b c -- alpha beta )
     #! Finds both roots of the polynomial a*x^2 + b*x + c using
index 987e4283ede931317a513879869e9dcea7fb1973..0c59a67b49060df888a92991afe9df0454fd9db6 100644 (file)
@@ -96,7 +96,7 @@ USE: vectors
 : lazy ( var [ a ] -- value )
     #! If the value of the variable is f, set the value to the
     #! result of evaluating [ a ].
-    over get [ drop get ] [ dip dupd set ] ifte ;
+    over get [ drop get ] [ swap >r call dup r> set ] ifte ;
 
 : alist> ( alist namespace -- )
     #! Set each key in the alist to its value in the
@@ -106,20 +106,17 @@ USE: vectors
 : alist>namespace ( alist -- namespace )
     <namespace> tuck alist> ;
 
-: object-path-traverse ( name object -- object )
+: traverse-path ( name object -- object )
     dup has-namespace? [ get* ] [ 2drop f ] ifte ;
 
-: object-path-iter ( object list -- object )
-    [
-        uncons [ swap object-path-traverse ] dip
-        object-path-iter
-    ] when* ;
+: (object-path) ( object list -- object )
+    [ uncons >r swap traverse-path r> (object-path) ] when* ;
 
 : object-path ( list -- object )
     #! An object path is a list of strings. Each string is a
     #! variable name in the object namespace at that level.
     #! Returns f if any of the objects are not set.
-    this swap object-path-iter ;
+    this swap (object-path) ;
 
 : on ( var -- ) t put ;
 : off ( var -- ) f put ;
index ef31058884950644af71256bea4cccb242adae5a..1e0c28ec17b9a6b9a5867b0d40d4c07dcc9b44bb 100644 (file)
@@ -65,7 +65,7 @@ USE: stack
 
 : random-element-iter ( list index -- elem )
     #! Used by random-element*. Do not call directly.
-    [ unswons unswons ] dip ( list elem probability index )
+    >r unswons unswons r>   ( list elem probability index )
     swap -                  ( list elem index )
     dup 0 <= [
         drop nip
@@ -84,16 +84,13 @@ USE: stack
     #! Returns a random subset of the given list of comma pairs.
     #! The car of each pair is a probability, the cdr is the
     #! item itself. Only the cdr of the comma pair is returned.
-    dup [ [ [ ] ] dip car+ ] dip ( [ ] probabilitySum list )
-    [
-        [ 1 over random-int ] dip ( [ ] probabilitySum probability elem )
-        uncons ( [ ] probabilitySum probability elema elemd )
-        -rot ( [ ] probabilitySum elemd probability elema )
-        > ( [ ] probabilitySum elemd boolean )
+    [,
+        [ car+ ] keep ( probabilitySum list )
         [
-            drop
-        ] [
-            -rot ( elemd [ ] probabilitySum )
-            [ cons ] dip ( [ elemd ] probabilitySum )
-        ] ifte
-    ] each drop ;
+            >r 1 over random-int r> ( probabilitySum probability elem )
+            uncons ( probabilitySum probability elema elemd )
+            -rot ( probabilitySum elemd probability elema )
+            > ( probabilitySum elemd boolean )
+            [ drop ] [ , ] ifte
+        ] each drop
+    ,] ;
index 145ca7255ef6c9815185568ac04844437ea47bc6..809558ce49d29d32c6417c1df024d0ad1c66d0d0 100644 (file)
@@ -67,7 +67,9 @@ USE: stack
     #! Apply a quotation to each character in the string, and
     #! push a new string constructed from return values.
     #! The quotation must have stack effect ( X -- X ).
-    <% swap [ swap dup >r call % r> ] str-each drop %> ;
+    over str-length <sbuf> rot [
+        swap >r apply r> tuck sbuf-append
+    ] str-each nip sbuf>str ;
 
 : split-next ( index string split -- next )
     3dup index-of* dup -1 = [
index 06cdbb10bfe25e55fe845d60566acf7685a78a9b..30e7bff038cc099ab2ec84d65d06f9fe52cb9b2a 100644 (file)
@@ -80,45 +80,39 @@ USE: stack
 : str/ ( str index -- str str )
     #! Returns 2 strings, that when concatenated yield the
     #! original string.
-    2dup str-tail [ str-head ] dip ;
+    2dup str-tail >r str-head r> ;
 
 : str// ( str index -- str str )
     #! Returns 2 strings, that when concatenated yield the
     #! original string, without the character at the given
     #! index.
-    2dup succ str-tail [ str-head ] dip ;
+    2dup succ str-tail >r str-head r> ;
 
 : >title ( str -- str )
-    1 str/ [ >upper ] dip >lower cat2 ;
+    1 str/ >r >upper r> >lower cat2 ;
 
 : str-headcut ( str begin -- str str )
     str-length str/ ;
 
+: =? ( x y z -- z/f )
+    #! Push z if x = y, otherwise f.
+    -rot = [ drop f ] unless ;
+
 : str-head? ( str begin -- str )
     #! If the string starts with begin, return the rest of the
     #! string after begin. Otherwise, return f.
-    2dup str-length< [
-        2drop f
-    ] [
-        tuck str-headcut
-        [ = ] dip f ?
-    ] ifte ;
+    2dup str-length< [ 2drop f ] [ tuck str-headcut =? ] ifte ;
 
 : ?str-head ( str begin -- str ? )
     dupd str-head? dup [ nip t ] [ drop f ] ifte ;
 
 : str-tailcut ( str end -- str str )
-    str-length [ dup str-length ] dip - str/ ;
+    str-length >r dup str-length r> - str/ swap ;
 
 : str-tail? ( str end -- str )
     #! If the string ends with end, return the start of the
     #! string before end. Otherwise, return f.
-    2dup str-length< [
-        2drop f
-    ] [
-        tuck str-tailcut swap
-        [ = ] dip f ?
-    ] ifte ;
+    2dup str-length< [ 2drop f ] [ tuck str-tailcut =? ] ifte ;
 
 : ?str-tail ( str end -- str ? )
     dupd str-tail? dup [ nip t ] [ drop f ] ifte ;
@@ -143,7 +137,7 @@ USE: stack
     #! Execute the code, with each character of the string
     #! pushed onto the stack.
     over str-length [
-        -rot 2dup [ [ str-nth ] dip call ] 2dip
+        -rot 2dup >r >r >r str-nth r> call r> r>
     ] times* 2drop ;
 
 : str-sort ( list -- sorted )
diff --git a/library/test/math/namespaces.factor b/library/test/math/namespaces.factor
new file mode 100644 (file)
index 0000000..951c691
--- /dev/null
@@ -0,0 +1,14 @@
+IN: scratchpad
+USE: namespaces
+USE: test
+USE: math
+
+5 "x" set
+
+[ 6 ] [ 1 "x" +@ "x" get ] unit-test
+[ 5 ] [ 1 "x" -@ "x" get ] unit-test
+[ 10 ] [ 2 "x" *@ "x" get ] unit-test
+[ 2 ] [ 5 "x" /@ "x" get ] unit-test
+[ -2 ] [ "x" neg@ "x" get ] unit-test
+[ -3 ] [ "x" pred@ "x" get ] unit-test
+[ -2 ] [ "x" succ@ "x" get ] unit-test
index 2373f05df5abb282a3c8313919b7b616d8c70cd0..915573ad1ab1f3fd46922808c765293b8a64793d 100644 (file)
@@ -16,15 +16,28 @@ unit-test
     [ 10 | t ]
     [ 20 | f ]
     [ 30 | "monkey" ]
+    [ 24 | 1/2 ]
+    [ 13 | { "Hello" "Banana" } ]
 ] "random-pairs" set
 
+"random-pairs" get [ cdr ] map "random-values" set
+
 [ f ]
 [
     "random-pairs" get
-    random-element* [ t f "monkey" ] contains? not
+    random-element* "random-values" get contains? not
 ] unit-test
 
 : check-random-int ( min max -- )
     2dup random-int -rot between? assert ;
 
 [ ] [ 100 [ -12 674 check-random-int ] times ] unit-test
+
+: check-random-subset ( expected pairs -- )
+    random-subset* [ over contains? ] all? nip ;
+
+[ t ] [
+    "random-values" get
+    "random-pairs" get
+    check-random-subset
+] unit-test
index 5c98bc8458af127c6bdd8601939988996f800497..9e306a4dc4ad7f073e4de15ceb5db0554c4b8eb9 100644 (file)
@@ -91,3 +91,15 @@ unit-test
 [ f ] [ [ 0 10 "hello" substring ] [ not ] catch ] unit-test
 
 [ [ "hell" "o wo" "rld" ] ] [ 4 "hello world" split-n ] unit-test
+
+[ 4 ] [
+    0 "There are Four Upper Case characters"
+    [ LETTER? [ succ ] when ] str-each
+] unit-test
+
+[ "Replacing+spaces+with+plus" ]
+[
+    "Replacing spaces with plus"
+    [ CHAR: \s CHAR: + replace ] str-map
+]
+unit-test
index 20ba8a397a98d25221904ef5928c878939497250..87d60954edb14741a21ba4aa2049db4e69e4a938 100644 (file)
@@ -98,6 +98,7 @@ USE: unparser
         "math/complex"
         "math/irrational"
         "math/simpson"
+        "math/namespaces"
         "httpd/url-encoding"
         "httpd/html"
         "httpd/httpd"
index ef6e023e7d6b0f228e2a66e554180f4019e4a519..8b13a1d05af4d09d5448e0a37e7f485746f253d7 100644 (file)
@@ -39,18 +39,12 @@ USE: stack
         -rot 2dup >r >r >r vector-nth r> call r> r>
     ] times* 2drop ;
 
-: (vector-map-step) ( element code -- result code )
-    dup >r call r> ;
-
-: (vector-map) ( code target element -- result code target )
-    -rot >r (vector-map-step) r> ;
-
 : vector-map ( vector code -- vector )
     #! Applies code to each element of the vector, return a new
     #! vector with the results. The code must have stack effect
     #! ( obj -- obj ).
     over vector-length <vector> rot [
-        (vector-map) swapd tuck vector-push
+        swap >r apply r> tuck vector-push
     ] vector-each nip ;
 
 : vector-and ( vector -- ? )