io.random: cleanup using "each-numbered-line", implement "random-lines".
parent
8e137839db
commit
f36ee26f82
|
@ -1,16 +1,28 @@
|
||||||
! Copyright (C) 2012 John Benediktsson
|
! Copyright (C) 2012 John Benediktsson
|
||||||
! See http://factorcode.org/license.txt for BSD license
|
! See http://factorcode.org/license.txt for BSD license
|
||||||
|
|
||||||
USING: io kernel math random ;
|
USING: fry io kernel locals math random sequences
|
||||||
|
sequences.extras ;
|
||||||
|
|
||||||
IN: io.random
|
IN: io.random
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
||||||
: ?replace ( old new n -- old/new )
|
: each-numbered-line ( ... quot: ( ... line number -- ... ) -- ... )
|
||||||
random zero? [ nip ] [ drop ] if ;
|
[ 1 ] dip '[ swap [ @ ] [ 1 + ] bi ] each-line drop ; inline
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
: random-readln ( -- line/f )
|
: random-line ( -- line/f )
|
||||||
f 1 [ swap [ ?replace ] [ 1 + ] bi ] each-line drop ;
|
f [ random zero? [ nip ] [ drop ] if ] each-numbered-line ;
|
||||||
|
|
||||||
|
:: random-lines ( n -- lines )
|
||||||
|
V{ } clone :> accum
|
||||||
|
[| line line# |
|
||||||
|
line# random :> r
|
||||||
|
r n < [
|
||||||
|
line# n <
|
||||||
|
[ line r accum insert-nth! ]
|
||||||
|
[ line r accum set-nth ] if
|
||||||
|
] when
|
||||||
|
] each-numbered-line accum ;
|
||||||
|
|
Loading…
Reference in New Issue