From a0d1c036ea8bc75bb9d709f9bd2a9fe427e83a5e Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Tue, 23 Oct 2012 19:03:34 -0700 Subject: [PATCH] io.random: much faster version of random-lines for large n. --- extra/io/random/random.factor | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/extra/io/random/random.factor b/extra/io/random/random.factor index a12574fb43..0c0f28d29f 100644 --- a/extra/io/random/random.factor +++ b/extra/io/random/random.factor @@ -1,8 +1,8 @@ ! Copyright (C) 2012 John Benediktsson ! See http://factorcode.org/license.txt for BSD license -USING: fry io kernel locals math random sequences -sequences.extras ; +USING: combinators fry io kernel locals math math.order random +sequences ; IN: io.random @@ -19,10 +19,10 @@ PRIVATE> :: 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 + line# n <=> { + { +lt+ [ line accum push ] } + { +eq+ [ line accum [ push ] [ randomize drop ] bi ] } + { +gt+ [ line# random :> r + r n < [ line r accum set-nth ] when ] } + } case ] each-numbered-line accum ;