{ { } } [ { } [ + ] 0accumulate ] unit-test
{ { 100 101 103 107 } } [ { 100 1 2 4 } [ + ] 0accumulate ] unit-test
+{
+ H{ { t 6 } { f 5 } }
+ { 0 0 1 1 2 3 4 2 3 4 5 }
+} [
+ { 2 7 1 8 1 7 1 8 2 8 4 } [ even? ] occurrence-count-by
+] unit-test
+
+{
+ H{ { 8 3 } { 1 3 } { 2 2 } { 4 1 } { 7 2 } }
+ { 0 0 0 0 1 1 2 1 1 2 0 }
+} [
+ { 2 7 1 8 1 7 1 8 2 8 4 } occurrence-count
+] unit-test
+
+{
+ H{ { 8 3 } { 1 3 } { 2 2 } { 4 1 } { 7 2 } }
+ { 0 0 0 0 1 1 2 1 1 2 0 }
+} [
+ { 2 7 1 8 1 7 1 8 2 8 4 } [ ] occurrence-count-by
+] unit-test
+
{ { 0 1 2 3 } } [ 8 <iota> [ 4 < ] take-while >array ] unit-test
{ { } } [ { 15 16 } [ 4 < ] take-while >array ] unit-test
{ { 0 1 2 } } [ 3 <iota> [ 4 < ] take-while >array ] unit-test
: 0accumulate ( ... seq quot: ( ... prev elt -- ... next ) -- ... final newseq )
over 0accumulate-as ; inline
+: occurrence-count-by ( seq quot: ( elt -- elt' ) -- hash seq )
+ '[ nip @ over inc-at* ] H{ } clone -rot 0accumulate ; inline
+
+: occurrence-count ( seq -- hash seq )
+ [ ] occurrence-count-by ; inline
+
: 0reduce ( seq quot: ( prev elt -- next ) -- result )
[ 0 ] dip reduce ; inline