]> gitweb.factorcode.org Git - factor.git/commitdiff
rosetta-code.equilibrium-index: simplify.
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 10 Feb 2017 00:04:20 +0000 (16:04 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 10 Feb 2017 00:04:20 +0000 (16:04 -0800)
extra/rosetta-code/equilibrium-index/equilibrium-index.factor

index d5f1cb6200f380ce886ce00fffa71769f9b9528d..a3ad1f6d2c0048efaf1bcea9b6f8c82b9f9ad871 100644 (file)
@@ -1,11 +1,14 @@
 ! Copyright (c) 2012 Anonymous
 ! See http://factorcode.org/license.txt for BSD license.
-USING: kernel math math.vectors sequences ;
+USING: kernel math math.statistics math.vectors sequences ;
 IN: rosetta-code.equilibrium-index
 
 ! http://rosettacode.org/wiki/Equilibrium_index
 
-! An equilibrium index of a sequence is an index into the sequence such that the sum of elements at lower indices is equal to the sum of elements at higher indices. For example, in a sequence A:
+! An equilibrium index of a sequence is an index into the
+! sequence such that the sum of elements at lower indices is
+! equal to the sum of elements at higher indices. For example,
+! in a sequence A:
 !   A0 = − 7
 !   A1 = 1
 !   A2 = 5
@@ -28,12 +31,6 @@ IN: rosetta-code.equilibrium-index
 ! equilibrium indices (if any). Assume that the sequence may be
 ! very long.
 
-: accum-left ( seq id quot -- seq )
-    accumulate nip ; inline
-
-: accum-right ( seq id quot -- seq )
-    [ <reversed> ] 2dip accum-left <reversed> ; inline
-
-: equilibrium-indices ( seq -- inds )
-    0 [ + ] [ accum-left ] [ accum-right ] 3bi v=
-    V{ } swap dup length iota [ [ suffix ] curry [ ] if ] 2each ;
+: equilibrium-indices ( seq -- indices )
+    [ cum-sum0 ] [ <reversed> cum-sum0 <reversed> ] bi
+    v= t swap indices ;