there is also,

iota =: i. : (+ i.)


 3 iota 5 
3 4 5 6 7

 ([ iota >:@-~)/  3 7 
3 4 5 6 7

 3 iota _5 
7 6 5 4 3


________________________________
From: Joe Bogner <joebog...@gmail.com>
To: programm...@jsoftware.com 
Sent: Friday, April 6, 2018 3:50 PM
Subject: Re: [Jprogramming] Expression to generate sequ



Lightly tested and non-optimized and ugly, but seems to work!

genseq=:({: + i.@:>:@:-/)@]

seq =: (] (>/@[ \:~@]^:[ genseq) \:~) f.


] (>/@[ \:~@]^:[ ({: + i.@:>:@:-/)@]) \:~


seq 3 7 3 4 5 6 7 seq 7 3 7 6 5 4 3




On Fri, Apr 6, 2018 at 3:23 PM, Devon McCormick <devon...@gmail.com> wrote:

> Hi -
>
> I'm working with selecting sequential items from a vector given a starting
> and an ending point as a pair of integers.  If we assume we always get the
> pair as (lesser, greater), something like this suffices:
>
>    seq=: {. + [: i. [: >: -~/
>    seq 3 7
> 3 4 5 6 7
>
> However, if our assumption about the order of our points is violated, we
> get nonsense:
>
>    seq 7 3
> 7 8 9
>
> Just for robustness, how could we write a version of "seq" that would
> return the sequence in reverse order in this latter case, i.e. give the
> proper sequence from seven to three: 7 6 5 4 3 ?
>
> I generalized the "seq" verb to remove the ordering assumption: my idea was
> to generate the ascending sequence, then flip it if >/y is true.  I tried
> this with "agenda" but am not sure how to get it to work on one thing - the
> ascending vector - on the basis of comparison of another thing - the pair
> of integers.
>
> I did achieve this using the "power" conjunction:
>
>    seq=: 3 : '|.^:(>/y)](<./ + [: i. [: >: >./ - <./) y'
>    seq 3 7
> 3 4 5 6 7
>    seq 7 3
> 7 6 5 4 3
>
> Does anyone have any ideas for a more elegant, preferably tacit, solution?
>
> Thanks,
>
> Devon
> --
>
> Devon McCormick, CFA
>
> Quantitative Consultant
> ----------------------------------------------------------------------
> 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

Reply via email to