]> gitweb.factorcode.org Git - factor.git/commitdiff
add docs for if-zero etc, add docs for 10^
authorDoug Coleman <doug.coleman@gmail.com>
Tue, 11 Aug 2009 23:45:01 +0000 (18:45 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Tue, 11 Aug 2009 23:45:01 +0000 (18:45 -0500)
basis/math/functions/functions-docs.factor
basis/math/functions/functions.factor
core/sequences/sequences-docs.factor
core/sequences/sequences.factor
extra/project-euler/common/common.factor

index 41800e46dafdcf514afa4ff15d6f1a49cb0c6d5c..0fe77fa4aeba1fd0c35e24a83dccc32f7af1b7aa 100644 (file)
@@ -50,8 +50,10 @@ ARTICLE: "power-functions" "Powers and logarithms"
 { $subsection exp }
 { $subsection cis }
 { $subsection log }
+{ $subsection log10 }
 "Raising a number to a power:"
 { $subsection ^ }
+{ $subsection 10^ }
 "Converting between rectangular and polar form:"
 { $subsection abs }
 { $subsection absq }
@@ -122,6 +124,10 @@ HELP: log
 { $values { "x" number } { "y" number } }
 { $description "Natural logarithm function. Outputs negative infinity if " { $snippet "x" } " is 0." } ;
 
+HELP: log10
+{ $values { "x" number } { "y" number } }
+{ $description "Logarithm function base 10. Outputs negative infinity if " { $snippet "x" } " is 0." } ;
+
 HELP: sqrt
 { $values { "x" number } { "y" number } }
 { $description "Square root function." } ;
@@ -261,6 +267,10 @@ HELP: ^
 { $description "Raises " { $snippet "x" } " to the power of " { $snippet "y" } ". If " { $snippet "y" } " is an integer the answer is computed exactly, otherwise a floating point approximation is used." }
 { $errors "Throws an error if " { $snippet "x" } " and " { $snippet "y" } " are both integer 0." } ;
 
+HELP: 10^
+{ $values { "x" number } { "y" number } }
+{ $description "Raises " { $snippet "x" } " to the power of 10. If " { $snippet "x" } " is an integer the answer is computed exactly, otherwise a floating point approximation is used." } ;
+
 HELP: gcd
 { $values { "x" integer } { "y" integer } { "a" integer } { "d" integer } }
 { $description "Computes the positive greatest common divisor " { $snippet "d" } " of " { $snippet "x" } " and " { $snippet "y" } ", and another value " { $snippet "a" } " satisfying:" { $code "a*y = d mod x" } }
index 8a0d39063b8a17f1a3be442b5ff83bcd50092a76..801522b37634a89dba9c31a3c0a7d94de5809082 100644 (file)
@@ -118,8 +118,6 @@ ERROR: non-trivial-divisor n ;
         -rot (^mod)
     ] if ; foldable
 
-: 10^ ( n -- n' ) 10 swap ^ ; inline
-
 GENERIC: absq ( x -- y ) foldable
 
 M: real absq sq ;
@@ -160,6 +158,10 @@ M: real log dup 0.0 >= [ flog ] [ 0.0 rect> log ] if ;
 
 M: complex log >polar swap flog swap rect> ;
 
+: 10^ ( x -- y ) 10 swap ^ ; inline
+
+: log10 ( x -- y ) log 10 log / ; inline
+
 GENERIC: cos ( x -- y ) foldable
 
 M: complex cos
index d7db7f5242f8dd3fc2f9df48a2982daad137c731..fbdd8268dac6048adcd67486318a82865972c346 100755 (executable)
@@ -123,7 +123,48 @@ HELP: unless-empty
     }
 } ;
 
-{ if-empty when-empty unless-empty } related-words
+HELP: if-zero
+{ $values { "n" number } { "quot1" quotation } { "quot2" quotation } }
+{ $description "Makes an implicit check if the number is zero. A zero is dropped and " { $snippet "quot1" } " is called. Otherwise, if the number is not zero, " { $snippet "quot2" } " is called on it." }
+{ $example
+    "USING: kernel math prettyprint sequences ;"
+    "3 [ \"zero\" ] [ sq ] if-zero ."
+    "9"
+} ;
+
+HELP: when-zero
+{ $values
+     { "n" number } { "quot" "the first quotation of an " { $link if-zero } } }
+{ $description "Makes an implicit check if the sequence is empty. A zero is dropped and the " { $snippet "quot" } " is called." }
+{ $examples "This word is equivalent to " { $link if-zero } " with an empty second quotation:"
+    { $example
+    "USING: sequences prettyprint ;"
+    "0 [ 4 ] [ ] if-zero ."
+    "4"
+    }
+    { $example
+    "USING: sequences prettyprint ;"
+    "0 [ 4 ] when-zero ."
+    "4"
+    }
+} ;
+
+HELP: unless-zero
+{ $values
+     { "n" number } { "quot" "the second quotation of an " { $link if-empty } } }
+{ $description "Makes an implicit check if the number is zero. A zero is dropped. Otherwise, the " { $snippet "quot" } " is called on the number." }
+{ $examples "This word is equivalent to " { $link if-zero } " with an empty first quotation:"
+    { $example
+    "USING: sequences math prettyprint ;"
+    "3 [ ] [ sq ] if-empty ."
+    "9"
+    }
+    { $example
+    "USING: sequences math prettyprint ;"
+    "3 [ sq ] unless-zero ."
+    "9"
+    }
+} ;
 
 HELP: delete-all
 { $values { "seq" "a resizable sequence" } }
@@ -1393,6 +1434,18 @@ $nl
 $nl
 "More elaborate counted loops can be performed with " { $link "math.ranges" } "." ;
 
+ARTICLE: "sequences-if" "Control flow with sequences"
+"To reduce the boilerplate of checking if a sequence is empty or a number is zero, several combinators are provided."
+$nl
+"Checking if a sequence is empty:"
+{ $subsection if-empty }
+{ $subsection when-empty }
+{ $subsection unless-empty }
+"Checking if a number is zero:"
+{ $subsection if-zero }
+{ $subsection when-zero }
+{ $subsection unless-zero } ;
+
 ARTICLE: "sequences-access" "Accessing sequence elements"
 { $subsection ?nth }
 "Concise way of extracting one of the first four elements:"
@@ -1658,6 +1711,8 @@ $nl
 "Using sequences for looping:"
 { $subsection "sequences-integers" }
 { $subsection "math.ranges" }
+"Using sequences for control flow:"
+{ $subsection "sequences-if" }
 "For inner loops:"
 { $subsection "sequences-unsafe" } ;
 
index 2e41d9d2e19d8accc58240a61c04d228dcf5ec63..39c38d8688ef614b3dee7c1f4401f3d23a64fca3 100755 (executable)
@@ -46,9 +46,9 @@ PRIVATE>
 : if-zero ( n quot1 quot2 -- )
     [ dup zero? ] (if-empty) ; inline
 
-: when-zero ( seq quot -- ) [ ] if-zero ; inline
+: when-zero ( n quot -- ) [ ] if-zero ; inline
 
-: unless-zero ( seq quot -- ) [ ] swap if-zero ; inline
+: unless-zero ( n quot -- ) [ ] swap if-zero ; inline
 
 : delete-all ( seq -- ) 0 swap set-length ;
 
index 497fc31de7fc41cd89725daee7ff720c28147f6c..c97c6f1a950a3901e8391ea659f0d94fa665e9b2 100644 (file)
@@ -62,9 +62,6 @@ PRIVATE>
 : cartesian-product ( seq1 seq2 -- seq1xseq2 )
     [ [ 2array ] with map ] curry map concat ;
 
-: log10 ( m -- n )
-    log 10 log / ;
-
 : mediant ( a/c b/d -- (a+b)/(c+d) )
     2>fraction [ + ] 2bi@ / ;