 
 : call-random>casep ( seq -- assoc )
     [ length recip ] keep [ 2array ] with map ;
-    
+
 PRIVATE>
 
 MACRO: casep ( assoc -- ) casep>quot ;

        [ dup pair? [ prepare-pair ] [ with-drop ] if ] map
        cond>quot
    ] [ bad-probabilities ] if ;
    
MACRO: (casep) ( assoc -- ) (casep>quot) ;

: casep>quot ( assoc -- quot )
    (casep>quot) [ random-unit ] prepend ;
    
: (conditional-probabilities) ( seq i -- p )
    [ dup 0 > [ head [ 1 swap - ] [ * ] map-reduce ] [ 2drop 1 ] if ] [ swap nth ] 2bi * ;
    
: conditional-probabilities ( seq -- seq' )
    dup length iota [ (conditional-probabilities) ] with map ;
    
: (direct>conditional) ( assoc -- assoc' )
        [ keys conditional-probabilities ] [ values ] bi zip ;
        
: direct>conditional ( assoc -- assoc' )
    dup last pair? [ (direct>conditional) ] [
        unclip-last [ (direct>conditional) ] [ suffix ] bi*

IN: combinators.random

: ifp ( p true false -- ) [ random-unit > ] 2dip if ; inline

: whenp ( p true -- ) [ ] ifp ; inline

: unlessp ( p false -- ) [ [ ] ] dip ifp ; inline

<PRIVATE
