P.S. the "2 plus items over 2" thing can be approached this way: 2 (] + [ * <)i.5 0 1 2 5 6
And this will perform better than 2 + amdt (I.@:(6 < ])) i.5 and is also more concise, if that matters... FYI, -- Raul On Thu, Jun 18, 2015 at 10:00 PM, 'Pascal Jasmin' via Programming <[email protected]> wrote: > The verb take is defined in later versions of J as {. > > I recommend instead (this definition will not overwrite the one in z) > > take =: (([ <. #@:]) {. ]) > > 1 take i.2 NB. same as head > 0 > > 3 take i.2 > 0 1 > > > (i.0) -: 1 take i.0 > 1 > > > the reason this definition is better, is if you want the following, you can > still just use {. > > 3 {. i.2 > 0 1 0 > 1 {. i.0 > 0 > {. i.0 > 0 > > > my question is does anyone use the word take in code, such that it would > break under this redefinition? Even if you do, would you prefer having the > word take behave as above, and any code that depends on pure {. can just be > replaced with {. ? > > one of the reasons the code is useful (regardless of name) > > amdt =:2 : '(u (v{ ]))`(v"_)`]} ]' > > 2 + amdt (I.@:(2 < ])) i.5 NB. add 2 to items over 2 > 0 1 2 5 6 > > > 2 + amdt (I.@:(6 < ])) i.5 NB. no items pass filter, but no error, bc I. > returns i.0 > 0 1 2 3 4 > > > 2 + amdt (1 take I.@:(2 < ])) i.5 NB. just to first item over 2 > 0 1 2 5 4 > > 2 + amdt (2 take I.@:(1 < ])) i.5 NB. just to first 2 items over 1 > 0 1 4 5 4 > > > 2 + amdt (1 take I.@:(8 < ])) i.5 NB. no error. no unwanted updates > 0 1 2 3 4 > > > 2 + amdt (1 {. I.@:(8 < ])) i.5 NB. buggy - not intended > 2 1 2 3 4 > > 2 + amdt (2 {. I.@:(3 < ])) i.5 NB. buggy - not intended > 2 1 2 3 6 > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
