"Roger's solution is significantly faster due to the global select."
Indeed, it is leaner too:
( T=. <;._1 ' Hi Ho hee haw Yo ' )
┌┬──┬┬┬──┬┬───┬┬┬───┬┬┬──┬┐
││Hi│││Ho││hee│││haw│││Yo││
└┴──┴┴┴──┴┴───┴┴┴───┴┴┴──┴┘
o=. @:
e=. &.>
st=. (] , < o (1&({::) * 2&({::))) o (] ; 7!:2 o ] ; 6!:2)
100 st &> ']`[@.('''' -: ])&.>~/\.&.|. T' ; '(, {~ a:=[)/\.&.|. T' ;
'(a:&~: (+/\@[ { {.@] , # ) ]) T'
┌───────────────────────────────┬─────┬─────────────┬────────────┐
│]`[@.('' -: ])&.>~/\.&.|. T │13440│1.77288822e_5│0.238276177 │
├───────────────────────────────┼─────┼─────────────┼────────────┤
│(, {~ a:=[)/\.&.|. T │8576 │1.84638611e_5│0.158346072 │
├───────────────────────────────┼─────┼─────────────┼────────────┤
│(a:&~: (+/\@[ { {.@] , # ) ]) T│6016 │9.05480724e_6│0.0544737204│
└───────────────────────────────┴─────┴─────────────┴────────────┘
I guess versatility does not come cheap, the adverb,
scan=. ((~/)\.)(&.|.)
can be used to perform a series of chained, left-associative, operations,
while preserving history:
]`[@.('' -: ])&.>scan T
┌┬──┬──┬──┬──┬──┬───┬───┬───┬───┬───┬───┬──┬──┐
││Hi│Hi│Hi│Ho│Ho│hee│hee│hee│haw│haw│haw│Yo│Yo│
└┴──┴──┴──┴──┴──┴───┴───┴───┴───┴───┴───┴──┴──┘
(,~ {~ a: = ])scan T
┌┬──┬──┬──┬──┬──┬───┬───┬───┬───┬───┬───┬──┬──┐
││Hi│Hi│Hi│Ho│Ho│hee│hee│hee│haw│haw│haw│Yo│Yo│
└┴──┴──┴──┴──┴──┴───┴───┴───┴───┴───┴───┴──┴──┘
,. o ((1 % 1 + [)scan o $&1) 33 NB. Golden Section constant...
1
0.5
0.666666667
0.6
0.625
0.615384615
0.619047619
0.617647059
0.618181818
0.617977528
0.618055556
0.618025751
0.618037135
0.618032787
0.618034448
0.618033813
0.618034056
0.618033963
0.618033999
0.618033985
0.61803399
0.618033988
0.618033989
0.618033989
0.618033989
0.618033989
0.618033989
0.618033989
0.618033989
0.618033989
0.618033989
0.618033989
0.618033989
,. o ((^&(1%3) o (1 + [))scan o $&1) 22 NB. Plastic constant...
1
1.25992105
1.31229384
1.32235382
1.32426874
1.32463263
1.32470175
1.32471488
1.32471737
1.32471785
1.32471794
1.32471795
1.32471796
1.32471796
1.32471796
1.32471796
1.32471796
1.32471796
1.32471796
1.32471796
1.32471796
1.32471796
,. o ((] + % o [)scan) o |. o i. 11 NB. Continued fraction constant
(right-associative)...
10
9.1
8.10989011
7.12330623
6.14038425
5.16285626
4.19369123
3.23845342
2.30878937
1.43312743
0.697774658
,. o ((] + %: o [)scan) o |. o i.22 NB. Nested Radical constant (right
associative)...
21
24.5825757
23.9580819
22.8946994
21.7848406
20.6674233
19.5461438
18.4211021
17.2919811
16.1583628
15.0197466
13.8755318
12.7249875
11.56721
10.4010601
9.22506746
8.03727962
6.83500963
5.61438513
4.36946938
3.09032758
1.75793276
> o (,e/scan) o (":e) o (0.1 , 2 + i.) 22 NB. Champernowne constant
(formatted)...
0.1
0.12
0.123
0.1234
0.12345
0.123456
0.1234567
0.12345678
0.123456789
0.12345678910
0.1234567891011
0.123456789101112
0.12345678910111213
0.1234567891011121314
0.123456789101112131415
0.12345678910111213141516
0.1234567891011121314151617
0.123456789101112131415161718
0.12345678910111213141516171819
0.1234567891011121314151617181920
0.123456789101112131415161718192021
0.12345678910111213141516171819202122
0.1234567891011121314151617181920212223
One can even play around with an old English formula ;)
100 ((10x ^ [) <. o * ({: o +: o ((1 + *)scan) o |. o (>: % 1 + 2 * >:) o
i. o ])) 333x
31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
On Mon, Jan 5, 2015 at 4:13 PM, Marshall Lochbaum <[email protected]>
wrote:
> Beat me to it by a little. You can tighten it up by not unboxing, and
> speed up the performance a tiny bit using selection rather than agenda.
>
> (, {~ a:=[)/\.&.|.
>
> Roger's solution is significantly faster due to the global select.
>
> Marshall
>
> On Mon, Jan 05, 2015 at 03:15:20PM -0500, Jose Mario Quintana wrote:
> > ' I'm thinking there's got to be some kind of
> > loopless prefix sort of solution but am at a loss to come up with it.'
> >
> > Maybe it was a suffix, rather than a prefix, solution what you really had
> > in mind?
> >
> > ]`[@.('' -: ])&.>~/\.&.|.
> >
> >
> >
> > On Mon, Jan 5, 2015 at 11:06 AM, Devon McCormick <[email protected]>
> wrote:
> >
> > > I just threw together a short J solution that does what I want but it
> uses
> > > a loop and a conditional. I'm thinking there's got to be some kind of
> > > loopless prefix sort of solution but am at a loss to come up with it.
> > >
> > > My code looks like this:
> > >
> > > fillEmptyFwd=: 3 : 0
> > > for_ix. i.<:#y do.
> > > if. 0=#>y{~>:ix do. y=. (ix{y) (>:ix)}y end.
> > > end.
> > > y
> > > )
> > >
> > > An example usage would be this:
> > >
> > > fillEmptyFwd '';'Hi';'';'';'Ho';'';'hee';'';'';'haw';'';'';'Yo';''
> > > ++--+--+--+--+--+---+---+---+---+---+---+--+--+
> > > ||Hi|Hi|Hi|Ho|Ho|hee|hee|hee|haw|haw|haw|Yo|Yo|
> > > ++--+--+--+--+--+---+---+---+---+---+---+--+--+
> > >
> > > Any ideas on something less loopy/conditional?
> > >
> > > --
> > > Devon McCormick, CFA
> > > ----------------------------------------------------------------------
> > > 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
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm