On Nov 17, 2007 8:34 AM, Edushka S. <[EMAIL PROTECTED]> wrote:
> On Nov 17, 2007 11:43 PM, Raul Miller <[EMAIL PROTECTED]> wrote:
> > F=: (nsum, nmin, cresult f. >. nsum-nmin)/@,&0
>
> Nice trick :)
> But what if I needed accumulator to be of some different, non numeric,
> type?
Well... I have not said that / is equivalent to foldr
The design of u/ does allow an equivalence, however, if you can define
a suitable u with an appropriate identity operation.
> Or, say, the same example but let's make initial value equal to
> "1 2 3"? :)
For that, I might use &.(-&1 2 3) perhaps with an appropriately
redefined core function.
> Besides, it still runs too slow in comparison to pure Insert case. And
> even slower than my foldr version:
Well... first off my definition of nsum was inefficient. I should have written
nmin=: nsum <. csummin f.
but that's not going to get the orders of magnitude speed improvement
you get from mcs..
> Unfortunately it still does not solve my problem with large files (as
> normal Hakell-like foldr solution should have).
When I have had to manage intermediate result size with very
large files, I generally take an approach like your mcs and
apply it to large (but not too large) chunks of data, and merge
the results.
In other words, I might use something like this:
G=:3 :0
blocksize=. 1e4
offsets=. i.@>.&.(%&blocksize) #y
lengths=. blocksize <.(-&offsets) #y
'sum0 mmx0 tsum mins'=. 0
max=.__
for_j.i.#offsets do.
n=. y {~ offsets (+ i.)&(j&{) lengths
sum0=. {: sums=. +/\ sum0, n
mmx0=. {: mins=. <./\ tsum=. mmx0 0} sums
max=.max>.>./tsum-mins
end.
max
)
I would set blocksize smaller if you wanted to inspect the multi-block
aspect of this verb (I used 3 for my early testing). I do not know if
different large sizes might perform better or worse. (Because of cache
sizes and/or interpretive overhead.)
There might even be an automated way of transforming something
like {:@mcs to G, but I am not sure how I would approach that.
For now, understanding your problem domain should work.
Good luck,
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm