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