add the %chance combinator

db4
Doug Coleman 2008-08-16 15:20:34 -05:00
parent bd8dcd4e0a
commit 107cadf604
2 changed files with 12 additions and 1 deletions

View File

@ -11,3 +11,12 @@ HELP: generate
"[ 20 random-prime ] [ 4 mod 3 = ] generate ."
"526367"
} ;
HELP: %chance
{ $values { "quot" quotation } { "n" integer } }
{ $description "Calls the quotation " { $snippet "n" } " percent of the time." }
{ $unchecked-example
"USING: io ;"
"[ \"hello, world! maybe.\" print ] 50 %chance"
""
} ;

View File

@ -4,7 +4,7 @@
! See http://factorcode.org/license.txt for BSD license.
USING: kernel combinators fry namespaces quotations hashtables
sequences assocs arrays inference effects math math.ranges
generalizations macros continuations locals ;
generalizations macros continuations random locals ;
IN: combinators.lib
@ -147,3 +147,5 @@ MACRO: predicates ( seq -- quot/f )
dup [ 1quotation [ drop ] prepend ] map
>r [ [ dup ] prepend ] map r> zip [ drop f ] suffix
[ cond ] curry ;
: %chance ( quot integer -- ) 100 random > swap when ; inline