It turned out that 3 : 'o =: o v y' "0 l is twice as long as this simple for implementation (2.7s vs 1.4s): o =: (3 : 0) l o =. empty'' for_i. y do. o =. o v i end. o ) Actually in both these implementations the v was inlined.
The approach with >(v~&.>) / 1|.o;/|.l is a bit slower than the for (1.5s). G.P. On Tue, Feb 3, 2015 at 7:41 PM, Moon S <[email protected]> wrote: > I see. Thank you! > Still it's a pity there's no way to tell '/' an initial value without > boxing/unboxing. > > > (10000) 6!:2 '>(v~&.>) / 1|.o;;/|. l' > > 0.0016646 > > > (10000) 6!:2 '(,:o) v~ reduce |.l' > > 0.0016654 > > > (10000) 6!:2 '0[3 : ''q=:q v y'' "0 l[q=:o' > > 0.0038912 > > > On Tue, Feb 3, 2015 at 7:30 PM, 'Pascal Jasmin' via Programming < > [email protected]> wrote: > >> (,: 'xx') 4 : 'x,''_'',":y'~ reduce 1 2 3 4 >> xx_4_3_2_1 >> >> >> >> >> >> ----- Original Message ----- >> From: Moon S <[email protected]> >> To: [email protected] >> Cc: >> Sent: Tuesday, February 3, 2015 12:20 PM >> Subject: Re: [Jprogramming] Fold/reduce with initial value? >> >> boxscan/reduce neary works: >> >> boxscan =: ((&.>)/)(>@:) >> >> reduce =: 1 : '([: u boxscan ,~)&(<"_1)' >> >> o v~ reduce l >> >> x_x_4_3_2_1 >> >> >> On Tue, Feb 3, 2015 at 7:05 PM, 'Pascal Jasmin' via Programming < >> [email protected]> wrote: >> >> > if everything is the same shape, then the easy way >> > >> > 100 ,~ i.5 >> > 0 1 2 3 4 100 >> > >> > -~/ 100 ,~ i.5 >> > 90 >> > >> > >> > An adverb that works when the items you want to reduce are not (or may >> not >> > be) the same shape as the (initial value and) final answer >> > >> > boxscan =: ((&.>)/)(>@:) >> > >> > 100 ([:-~boxscan ,~)&(<"_1) i.5 >> > 90 >> > >> > >> > 100 ([:(- +/"1)~ boxscan ,~)&(<"_1) i.5 2 >> > 55 >> > >> > >> > then as an adverb, >> > >> > reduce =: 1 : '([: u boxscan ,~)&(<"_1)' >> > >> > 100 (- +/"1)~ reduce i.5 2 >> > 55 >> > >> > >> > 100 (- +/"1)~ reduce 0 1 ,: 2 3 >> > 94 >> > >> > >> > >> > ----- Original Message ----- >> > From: Moon S <[email protected]> >> > To: [email protected] >> > Cc: >> > Sent: Tuesday, February 3, 2015 11:15 AM >> > Subject: [Jprogramming] Fold/reduce with initial value? >> > >> > I have a list (l), some object (o) and a verb (v) modifying the object: >> > >> > l = l_0 l_1 ... l_n >> > o_new = o_old v l_i >> > >> > The list items and the object have different types. I want to apply all >> the >> > list items to the object: >> > >> > o_new = (...((o_old v l_0) v l_1) ... v l_n) NB. the order of items >> > doesn't matter >> > >> > Currently I do it like this: >> > >> > o =: o_old >> > 3 : 'o =: y v o' "0 l >> > >> > I suspect, there must be some built-in feature for that, like / or ^:, >> so >> > that I could write e.g. >> > o_new =: o_old v REDUCE l, but I couldn't find a suitable >> > verb/adverb/conjunction. >> > >> > Any help? >> > >> > Thank you. >> > >> > Georgiy Pruss. >> > ---------------------------------------------------------------------- >> > 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 >> > > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
