Skip to content

Commit

Permalink
random: support random on sets.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Oct 5, 2012
1 parent 2b719f5 commit cfe3cba
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions basis/random/random.factor
Expand Up @@ -2,11 +2,12 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.data arrays assocs byte-arrays USING: accessors alien.data arrays assocs byte-arrays
byte-vectors combinators combinators.short-circuit fry byte-vectors combinators combinators.short-circuit fry
hashtables hashtables.private hints io.backend io.binary hashtables hashtables.private hash-sets hints io.backend
kernel locals math math.bitwise math.constants math.functions io.binary kernel locals math math.bitwise math.constants
math.order math.ranges namespaces sequences sequences.private math.functions math.order math.ranges namespaces sequences
sets summary system typed vocabs ; sequences.private sets summary system typed vocabs ;
QUALIFIED-WITH: alien.c-types c QUALIFIED-WITH: alien.c-types c
QUALIFIED-WITH: sets sets
IN: random IN: random


SYMBOL: system-random-generator SYMBOL: system-random-generator
Expand Down Expand Up @@ -83,6 +84,10 @@ M: hashtable random
[ array-nth ] [ [ 1 + ] dip array-nth ] 2bi 2array [ array-nth ] [ [ 1 + ] dip array-nth ] 2bi 2array
] if-zero ; ] if-zero ;


M: sets:set random members random ;

M: hash-set random table>> random first ;

: 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 ]
Expand Down

0 comments on commit cfe3cba

Please sign in to comment.