Further improvement:
grp=: +/@( >/~ [:(1+[:i.<.)&.(10&^.) >./)
NB. replaces the rather slow 10&(<....@^.)
rank'grp d';'10&(<....@^.) d'[d=:i.1e6
+----+-----+----+----+
|rank|tm*sz|time|size|
+----+-----+----+----+
| 0 |1.00 |1.00|1.00|
+----+-----+----+----+
| 1 |4.36 |2.32|1.88|
+----+-----+----+----+
rank '(= +/"1@({&f)@digits + [:(-~<./)grp) d';';([: spcl&.> grp
</.])d';';(grp <@spcl/.])d'
+----+-----+----+----+
|rank|tm*sz|time|size|
+----+-----+----+----+
| 1 |1.02 |1.03|1.00|
+----+-----+----+----+
| 0 |1.00 |1.00|1.01|
+----+-----+----+----+
| 2 |1.33 |1.09|1.23|
+----+-----+----+----+
d#~ ".&> '(= +/"1@({&f)@digits + [:(-~<./)grp) d';';([: spcl&.> grp
</.])d';';(grp <@spcl/.])d'
85837 408297
85837 408297
85837 408297
To put it in perspective
rank '(= +/"1@({&f)@digits + [:(-~<./)grp) d';';([: spcl&.> grp
</.])d';'special2 d'[d=:i.1e5
+----+-----+-----+----+
|rank|tm*sz|time |size|
+----+-----+-----+----+
| 1 |1.04 | 1.05|8.89|
+----+-----+-----+----+
| 0 |1.00 | 1.00|9.02|
+----+-----+-----+----+
| 2 |4.75 |42.85|1.00|
+----+-----+-----+----+
with
special2=: = +/@(+ !)@(10&#.^:_1"0)
from http://jsoftware.com/pipermail/programming/2010-August/020211.html
R.E. Boss
> -----Oorspronkelijk bericht-----
> Van: [email protected] [mailto:programming-
> [email protected]] Namens R.E. Boss
> Verzonden: maandag 30 augustus 2010 14:00
> Aan: 'Programming forum'
> Onderwerp: Re: [Jprogramming] Splitting an integer into its digits
>
> Little more efficient is
>
> (= +/"1@({&f)@digits + [:(->./)10&(<....@^.))
>
> rank '(= +/"1@({&f)@digits + [:(->./)10&(<....@^.)) d';';([: spcl&.>
> 10&(<....@^.) </.])d';';(10&(<....@^.)<@(spcl)/.])d'
> +----+-----+----+----+
> |rank|tm*sz|time|size|
> +----+-----+----+----+
> | 0 |1.00 |1.00|1.00|
> +----+-----+----+----+
> | 1 |1.16 |1.15|1.01|
> +----+-----+----+----+
> | 2 |1.50 |1.22|1.23|
> +----+-----+----+----+
>
> d #~(= +/"1@({&f)@digits + [:(->./)10&(<....@^.)) d
> 85837 408297
>
>
> R.E. Boss
>
>
> > -----Oorspronkelijk bericht-----
> > Van: [email protected] [mailto:programming-
> > [email protected]] Namens R.E. Boss
> > Verzonden: maandag 30 augustus 2010 9:13
> > Aan: 'Programming forum'
> > Onderwerp: Re: [Jprogramming] Splitting an integer into its digits
> >
> > f=: (+!) i. 10
> > digits=: 10&#.^:_1
> > spcl=: = +/"1@({&f)@digits
> >
> > rank ';([: spcl&.> 10&(<....@^.) </.])d';';(10&(<....@^.)
> > <@(spcl)/.])d'[d=:i.1e6
> > +----+-----+----+----+
> > |rank|tm*sz|time|size|
> > +----+-----+----+----+
> > | 0 |1.00 |1.00|1.00|
> > +----+-----+----+----+
> > | 1 |1.29 |1.06|1.22|
> > +----+-----+----+----+
> >
> > d#~".&> ';([: (spcl)&.> 10&(<....@^.) </.])d';';(10&(<....@^.)
> <@(spcl)/.])d'
> > 85837 408297
> > 85837 408297
> >
> >
> > R.E. Boss
> >
> >
> > > -----Oorspronkelijk bericht-----
> > > Van: [email protected] [mailto:programming-
> > > [email protected]] Namens Marshall Lochbaum
> > > Verzonden: maandag 30 augustus 2010 4:36
> > > Aan: 'Programming forum'
> > > Onderwerp: Re: [Jprogramming] Splitting an integer into its digits
> > >
> > > The limiting factor here isn't the speed of the part that removes the
> > > initial 0s, it's the fact that you are doing the whole thing rank 1.
> > This
> > > way, (+/"1@({&f)) has to reinitialize for every row, which is SLOW.
> > > Essentially, this approach is the same as (stuff)@digits"0 in that the
> > > output from digits for each scalar is rank 1, so then applying a rank
> 1
> > > verb
> > > to it is the same but with more overhead.
> > >
> > > The only way to really get rid of the padding problem is to do it for
> > only
> > > the numbers which have a certain number of digits, and loop over all
> the
> > > digits less than your maximum.
> > >
> > > Marshall
> > >
> >
> >
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm