I think you are trying to eliminate overtake - where the result is
longer than the right argument.
If that is the case, I do not think this definition achieves that:
take =: (([ <. #@:]) {. ])
_6 take i.4
0 0 0 1 2 3
But here's a deeper question: what problem does this solve? Is this
suggestion purely a matter of taste, or are there practical problems
which are better solved this way?
(If there are such problems, I'd be interested in hearing about them...)
Thanks,
--
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