From c4637b5d7933eb4ff50a3e1639497b98a4eedd37 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Wed, 7 Nov 2012 17:05:06 -0800 Subject: [PATCH] math.extras: adding unique-indices. --- extra/math/extras/extras-tests.factor | 5 +++++ extra/math/extras/extras.factor | 12 ++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/extra/math/extras/extras-tests.factor b/extra/math/extras/extras-tests.factor index d55c1494c1..192a67ff81 100644 --- a/extra/math/extras/extras-tests.factor +++ b/extra/math/extras/extras-tests.factor @@ -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 diff --git a/extra/math/extras/extras.factor b/extra/math/extras/extras.factor index 22c22bbcac..ea97795a22 100644 --- a/extra/math/extras/extras.factor +++ b/extra/math/extras/extras.factor @@ -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 ;