math.extras: adding unique-indices.

db4
John Benediktsson 2012-11-07 17:05:06 -08:00
parent f0999e83dc
commit c4637b5d79
2 changed files with 13 additions and 4 deletions

View File

@ -72,3 +72,8 @@ IN: math.extras.test
{ { 0 5 1 2 2 } } [
{ -10 10 2 2.5 3 } [ { 1 2 3 4 5 } search-sorted ] map
] unit-test
{
{ 1 2 3 4 }
{ 0 1 0 0 2 3 }
} [ { 1 2 1 1 3 4 } unique-indices ] unit-test

View File

@ -1,10 +1,11 @@
! Copyright (C) 2012 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: assocs combinators.short-circuit fry grouping kernel
locals math math.combinatorics math.constants math.functions
math.order math.primes math.ranges math.statistics math.vectors
memoize random sequences sequences.extras sorting ;
USING: assocs assocs.extras combinators.short-circuit fry
grouping kernel locals math math.combinatorics math.constants
math.functions math.order math.primes math.ranges
math.statistics math.vectors memoize random sequences
sequences.extras sets sorting ;
IN: math.extras
@ -194,3 +195,6 @@ PRIVATE>
: weighted-random ( histogram -- obj )
unzip cum-sum [ last random ] [ search-sorted ] bi swap nth ;
: unique-indices ( seq -- unique indices )
[ members ] keep over dup length iota H{ } zip-as '[ _ at ] map ;