]> gitweb.factorcode.org Git - factor.git/blob - basis/combinators/random/random.factor
factor: trim using lists
[factor.git] / basis / combinators / random / random.factor
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 math math.order quotations random sequences summary ;
5 IN: combinators.random
6
7 : ifp ( p true false -- ) [ random-unit > ] 2dip if ; inline
8
9 : whenp ( p true -- ) [ ] ifp ; inline
10
11 : unlessp ( p false -- ) [ [ ] ] dip ifp ; inline
12
13 <PRIVATE
14
15 : with-drop ( quot -- quot' ) [ drop ] prepend ; inline
16
17 : prepare-pair ( pair -- pair' )
18     first2 [ [ [ - ] [ < ] 2bi ] curry ] [ with-drop ] bi* 2array ;
19
20 ERROR: bad-probabilities assoc ;
21
22 M: bad-probabilities summary
23     drop "The probabilities do not satisfy the rules stated in the docs." ;
24
25 : good-probabilities? ( assoc -- ? )
26     dup last pair? [
27         keys { [ sum 1 number= ] [ [ 0 1 between? ] all? ] } 1&&
28     ] [
29         but-last keys { [ sum 0 1 between? ] [ [ 0 1 between? ] all? ] } 1&&
30     ] if ;
31
32 ! Useful for unit-tests (no random part)
33 : (casep>quot) ( assoc -- quot )
34     dup good-probabilities? [
35         [ dup pair? [ prepare-pair ] [ with-drop ] if ] map
36         cond>quot
37     ] [ bad-probabilities ] if ;
38
39 MACRO: (casep) ( assoc -- quot ) (casep>quot) ;
40
41 : casep>quot ( assoc -- quot )
42     (casep>quot) [ random-unit ] prepend ;
43
44 : (conditional-probabilities) ( seq i -- p )
45     [ dup 0 > [ head [ 1 swap - ] [ * ] map-reduce ] [ 2drop 1 ] if ]
46     [ swap nth ] 2bi * ;
47
48 : conditional-probabilities ( seq -- seq' )
49     dup length <iota> [ (conditional-probabilities) ] with map ;
50
51 : (direct>conditional) ( assoc -- assoc' )
52     [ keys conditional-probabilities ] [ values ] bi zip ;
53
54 : direct>conditional ( assoc -- assoc' )
55     dup last pair? [ (direct>conditional) ] [
56         unclip-last [ (direct>conditional) ] [ suffix ] bi*
57     ] if ;
58
59 : call-random>casep ( seq -- assoc )
60     [ length recip ] keep [ 2array ] with map ;
61
62 PRIVATE>
63
64 MACRO: casep ( assoc -- quot ) casep>quot ;
65
66 MACRO: casep* ( assoc -- quot ) direct>conditional casep>quot ;
67
68 MACRO: call-random ( seq -- quot ) call-random>casep casep>quot ;
69
70 MACRO: execute-random ( seq -- quot )
71     [ 1quotation ] map call-random>casep casep>quot ;