From f36ee26f82f598a17e6f8cc32ce1b277ce16cd03 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Tue, 23 Oct 2012 16:11:07 -0700 Subject: [PATCH] io.random: cleanup using "each-numbered-line", implement "random-lines". --- extra/io/random/random.factor | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/extra/io/random/random.factor b/extra/io/random/random.factor index 3186e263e4..d9ce6c6838 100644 --- a/extra/io/random/random.factor +++ b/extra/io/random/random.factor @@ -1,16 +1,28 @@ ! Copyright (C) 2012 John Benediktsson ! 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 -: random-readln ( -- line/f ) - f 1 [ swap [ ?replace ] [ 1 + ] bi ] each-line drop ; +: random-line ( -- line/f ) + 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 ;