Adding a randomize word to sequences-lib.
parent
901288a835
commit
fd969a3a7f
|
@ -18,3 +18,8 @@ HELP: each-withn
|
||||||
"passed to the quotation given to each-withn for each element in the sequence."
|
"passed to the quotation given to each-withn for each element in the sequence."
|
||||||
}
|
}
|
||||||
{ $see-also map-withn } ;
|
{ $see-also map-withn } ;
|
||||||
|
|
||||||
|
HELP: randomize
|
||||||
|
{ $values { "seq" sequence } { "seq'" sequence } }
|
||||||
|
{ $description "Shuffle the elements in the sequence randomly, returning the new sequence." } ;
|
||||||
|
|
||||||
|
|
|
@ -160,3 +160,16 @@ PRIVATE>
|
||||||
|
|
||||||
: ?nth* ( n seq -- elt/f ? )
|
: ?nth* ( n seq -- elt/f ? )
|
||||||
2dup bounds-check? [ nth-unsafe t ] [ 2drop f f ] if ; flushable
|
2dup bounds-check? [ nth-unsafe t ] [ 2drop f f ] if ; flushable
|
||||||
|
|
||||||
|
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||||
|
|
||||||
|
USE: math.ranges
|
||||||
|
USE: random
|
||||||
|
: randomize ( seq -- seq' )
|
||||||
|
dup length 1-
|
||||||
|
[ dup 1 > ] [
|
||||||
|
[ [0,b) random ] keep dup
|
||||||
|
[ rot [ exchange ] keep ] dip 1-
|
||||||
|
] [ drop ] while ;
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue