factor/extra/io/random/random.factor

37 lines
978 B
Factor
Raw Normal View History

! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
2012-10-26 00:40:43 -04:00
USING: combinators destructors fry io io.encodings.binary
io.files io.streams.limited io.streams.random kernel locals
math math.order random sequences sequences.private ;
IN: io.random
<PRIVATE
: each-numbered-line ( ... quot: ( ... line number -- ... ) -- ... )
[ 1 ] dip '[ swap [ @ ] [ 1 + ] bi ] each-line drop ; inline
PRIVATE>
: random-line ( -- line/f )
f [ random zero? [ nip ] [ drop ] if ] each-numbered-line ;
:: random-lines ( n -- lines )
V{ } clone :> accum
[| line line# |
line# n <= [
line accum push
] [
line# random :> r
r n < [ line r accum set-nth-unsafe ] when
] if
] each-numbered-line accum ;
2012-10-26 00:40:43 -04:00
: random-file ( n path -- )
[
[ <random-stream> swap limit-stream ]
[ binary <file-writer> ] bi*
[ &dispose ] bi@ stream-copy
] with-destructors ;