]> gitweb.factorcode.org Git - factor.git/blob - extra/io/random/random.factor
dbb02edf2a7cf094d9cf6cbba0f7f1c52527a61c
[factor.git] / extra / io / random / random.factor
1 ! Copyright (C) 2012 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3
4 USING: fry io kernel locals math random sequences
5 sequences.private ;
6
7 IN: io.random
8
9 <PRIVATE
10
11 : each-numbered-line ( ... quot: ( ... line number -- ... ) -- ... )
12     [ 1 ] dip '[ swap _ [ 1 + ] bi ] each-line drop ; inline
13
14 PRIVATE>
15
16 : random-line ( -- line/f )
17     f [ random zero? [ nip ] [ drop ] if ] each-numbered-line ;
18
19 :: random-lines ( n -- lines )
20     V{ } clone :> accum
21     [| line line# |
22         line# n <= [
23             line accum push
24         ] [
25             line# random :> r
26             r n < [ line r accum set-nth-unsafe ] when
27         ] if
28     ] each-numbered-line accum ;