random: implement random for assocs.
parent
b3ff75cec2
commit
fc5f9451e8
|
@ -2,10 +2,10 @@
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors alien.c-types alien.data arrays assocs
|
USING: accessors alien.c-types alien.data arrays assocs
|
||||||
byte-arrays byte-vectors combinators combinators.short-circuit
|
byte-arrays byte-vectors combinators combinators.short-circuit
|
||||||
fry io.backend io.binary kernel locals math math.bitwise
|
fry hashtables hashtables.private hints io.backend io.binary
|
||||||
math.constants math.functions math.order math.ranges namespaces
|
kernel locals math math.bitwise math.constants math.functions
|
||||||
sequences sequences.private sets summary system vocabs hints
|
math.order math.ranges namespaces sequences sequences.private
|
||||||
typed ;
|
sets summary system typed vocabs ;
|
||||||
IN: random
|
IN: random
|
||||||
|
|
||||||
SYMBOL: system-random-generator
|
SYMBOL: system-random-generator
|
||||||
|
@ -72,6 +72,16 @@ M: sequence random
|
||||||
[ length random-integer ] keep nth
|
[ length random-integer ] keep nth
|
||||||
] if-empty ;
|
] if-empty ;
|
||||||
|
|
||||||
|
M: assoc random >alist random ;
|
||||||
|
|
||||||
|
M: hashtable random
|
||||||
|
dup assoc-size [ drop f ] [
|
||||||
|
[ 0 ] [ array>> ] [ random ] tri* 1 + [
|
||||||
|
[ 2dup array-nth tombstone? [ 2 + ] 2dip ] loop
|
||||||
|
] times [ 2 - ] dip
|
||||||
|
[ array-nth ] [ [ 1 + ] dip array-nth ] 2bi 2array
|
||||||
|
] if-zero ;
|
||||||
|
|
||||||
: randomize-n-last ( seq n -- seq )
|
: randomize-n-last ( seq n -- seq )
|
||||||
[ dup length dup ] dip - 1 max '[ dup _ > ]
|
[ dup length dup ] dip - 1 max '[ dup _ > ]
|
||||||
[ [ random ] [ 1 - ] bi [ pick exchange-unsafe ] keep ]
|
[ [ random ] [ 1 - ] bi [ pick exchange-unsafe ] keep ]
|
||||||
|
|
Loading…
Reference in New Issue