From 107cadf604bade7bbcdeebc0c008025f75038581 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 16 Aug 2008 15:20:34 -0500 Subject: [PATCH] add the %chance combinator --- extra/combinators/lib/lib-docs.factor | 9 +++++++++ extra/combinators/lib/lib.factor | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/extra/combinators/lib/lib-docs.factor b/extra/combinators/lib/lib-docs.factor index fe2f3556ef..cde3b4d259 100755 --- a/extra/combinators/lib/lib-docs.factor +++ b/extra/combinators/lib/lib-docs.factor @@ -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" + "" +} ; diff --git a/extra/combinators/lib/lib.factor b/extra/combinators/lib/lib.factor index a7d5e4cf58..bbf6644b41 100755 --- a/extra/combinators/lib/lib.factor +++ b/extra/combinators/lib/lib.factor @@ -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