1 ! Copyright (C) 2010 Jon Harper.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs combinators combinators.short-circuit
4 kernel macros math math.order quotations random sequences
8 : ifp ( p true false -- ) [ random-unit > ] 2dip if ; inline
10 : whenp ( p true -- ) [ ] ifp ; inline
12 : unlessp ( p false -- ) [ [ ] ] dip ifp ; inline
16 : with-drop ( quot -- quot' ) [ drop ] prepend ; inline
18 : prepare-pair ( pair -- pair' )
19 first2 [ [ [ - ] [ < ] 2bi ] curry ] [ with-drop ] bi* 2array ;
21 ERROR: bad-probabilities assoc ;
23 M: bad-probabilities summary
24 drop "The probabilities do not satisfy the rules stated in the docs." ;
26 : good-probabilities? ( assoc -- ? )
28 keys { [ sum 1 number= ] [ [ 0 1 between? ] all? ] } 1&&
30 but-last keys { [ sum 0 1 between? ] [ [ 0 1 between? ] all? ] } 1&&
33 ! Useful for unit-tests (no random part)
34 : (casep>quot) ( assoc -- quot )
35 dup good-probabilities? [
36 [ dup pair? [ prepare-pair ] [ with-drop ] if ] map
38 ] [ bad-probabilities ] if ;
40 MACRO: (casep) ( assoc -- quot ) (casep>quot) ;
42 : casep>quot ( assoc -- quot )
43 (casep>quot) [ random-unit ] prepend ;
45 : (conditional-probabilities) ( seq i -- p )
46 [ dup 0 > [ head [ 1 swap - ] [ * ] map-reduce ] [ 2drop 1 ] if ]
49 : conditional-probabilities ( seq -- seq' )
50 dup length <iota> [ (conditional-probabilities) ] with map ;
52 : (direct>conditional) ( assoc -- assoc' )
53 [ keys conditional-probabilities ] [ values ] bi zip ;
55 : direct>conditional ( assoc -- assoc' )
56 dup last pair? [ (direct>conditional) ] [
57 unclip-last [ (direct>conditional) ] [ suffix ] bi*
60 : call-random>casep ( seq -- assoc )
61 [ length recip ] keep [ 2array ] with map ;
65 MACRO: casep ( assoc -- quot ) casep>quot ;
67 MACRO: casep* ( assoc -- quot ) direct>conditional casep>quot ;
69 MACRO: call-random ( seq -- quot ) call-random>casep casep>quot ;
71 MACRO: execute-random ( seq -- quot )
72 [ 1quotation ] map call-random>casep casep>quot ;