Jsoftware
High-Performance Development Platform

Checking if the first element is the same as the last

View: New views
8 Messages — Rating Filter:   Alert me  

Checking if the first element is the same as the last

by unigee :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

I seem to be having a bit of trouble performing this idea.

I am trying to get a subset of a list, and comparing the first element
against the last element of the result.


    2 >\/ (1 2 3 4 1 2 3 4)
1 2
2 3
3 4
4 1
1 2
2 3
3 4

t1 =: 4 : '(_1{a) -: (0{ a=. y >\/ (x))'
   1 2 3 4 1 2 3 4 t1 2
0


   4 >\/ (1 2 3 4 1 2 3 4)
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
1 2 3 4

   1 2 3 4 1 2 3 4 t1 4
1

t1 works as intended, but I am having trouble making it work with more
than one right argument at a time
Here are my failed attempts.

   1 2 3 4 1 2 3 4 t1 2 3 4 NB. result should be 0 0 1
0

   1 2 3 4 1 2 3 4 t1"0 2 3 4
|length error
|   1 2 3 4 1 2 3 4     t1"0 2 3 4

   1 2 3 4 1 2 3 4 t1 each (2 3 4)
|length error
|   1 2 3 4 1 2 3 4     t1 each(2 3 4)

   (1 2 3 4 1 2 3 4) t1"0 (2 3 4)
|length error
|   (1 2 3 4 1 2 3 4)    t1"0(2 3 4)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Checking if the first element is the same as the last

by Don Guinn-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Try
   t1 =: (4 : '(_1{a) -: (0{ a=. y >\/ (x))')"_ 0


On Tue, Nov 10, 2009 at 6:58 AM, Ian Gorse <unigee@...> wrote:

> Hi
>
> I seem to be having a bit of trouble performing this idea.
>
> I am trying to get a subset of a list, and comparing the first element
> against the last element of the result.
>
>
>    2 >\/ (1 2 3 4 1 2 3 4)
> 1 2
> 2 3
> 3 4
> 4 1
> 1 2
> 2 3
> 3 4
>
> t1 =: 4 : '(_1{a) -: (0{ a=. y >\/ (x))'
>   1 2 3 4 1 2 3 4 t1 2
> 0
>
>
>   4 >\/ (1 2 3 4 1 2 3 4)
> 1 2 3 4
> 2 3 4 1
> 3 4 1 2
> 4 1 2 3
> 1 2 3 4
>
>   1 2 3 4 1 2 3 4 t1 4
> 1
>
> t1 works as intended, but I am having trouble making it work with more
> than one right argument at a time
> Here are my failed attempts.
>
>   1 2 3 4 1 2 3 4 t1 2 3 4 NB. result should be 0 0 1
> 0
>
>   1 2 3 4 1 2 3 4 t1"0 2 3 4
> |length error
> |   1 2 3 4 1 2 3 4     t1"0 2 3 4
>
>   1 2 3 4 1 2 3 4 t1 each (2 3 4)
> |length error
> |   1 2 3 4 1 2 3 4     t1 each(2 3 4)
>
>   (1 2 3 4 1 2 3 4) t1"0 (2 3 4)
> |length error
> |   (1 2 3 4 1 2 3 4)    t1"0(2 3 4)
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Checking if the first element is the same as the last

by R.E. Boss :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

   1 2 3 4 1 2 3 4 t1"(_ 0) 2 3 4 NB. all of x vs. each of y
0 0 1


R.E. Boss


> -----Oorspronkelijk bericht-----
> Van: programming-bounces@... [mailto:programming-
> bounces@...] Namens Ian Gorse
> Verzonden: dinsdag 10 november 2009 13:59
> Aan: Programming forum
> Onderwerp: [Jprogramming] Checking if the first element is the same as the
> last
>
> Hi
>
> I seem to be having a bit of trouble performing this idea.
>
> I am trying to get a subset of a list, and comparing the first element
> against the last element of the result.
>
>
>     2 >\/ (1 2 3 4 1 2 3 4)
> 1 2
> 2 3
> 3 4
> 4 1
> 1 2
> 2 3
> 3 4
>
> t1 =: 4 : '(_1{a) -: (0{ a=. y >\/ (x))'
>    1 2 3 4 1 2 3 4 t1 2
> 0
>
>
>    4 >\/ (1 2 3 4 1 2 3 4)
> 1 2 3 4
> 2 3 4 1
> 3 4 1 2
> 4 1 2 3
> 1 2 3 4
>
>    1 2 3 4 1 2 3 4 t1 4
> 1
>
> t1 works as intended, but I am having trouble making it work with more
> than one right argument at a time
> Here are my failed attempts.
>
>    1 2 3 4 1 2 3 4 t1 2 3 4 NB. result should be 0 0 1
> 0
>
>    1 2 3 4 1 2 3 4 t1"0 2 3 4
> |length error
> |   1 2 3 4 1 2 3 4     t1"0 2 3 4
>
>    1 2 3 4 1 2 3 4 t1 each (2 3 4)
> |length error
> |   1 2 3 4 1 2 3 4     t1 each(2 3 4)
>
>    (1 2 3 4 1 2 3 4) t1"0 (2 3 4)
> |length error
> |   (1 2 3 4 1 2 3 4)    t1"0(2 3 4)
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Parent Message unknown Re: Checking if the first element is the same as the last

by unigee :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you both for the help
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Checking if the first element is the same as the last

by neitzel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Ian,

>    t1 =: 4 : '(_1{a) -: (0{ a=. y >\/ (x))'

Uuuh... let's _simplify_ this.

(x) doesn't gain you anything, x will do.  (Too much C macros? :-)

In  y >\/ x  what is the / supposed to do?  Was it an attempt to
make "... tl 2 3 4" compute three matrices with different widths?
I'd really like to know.  However, it does actually nothing in this
context, and I strongly suggest to change the simplified y >/ x
into a  y ]\ x so readers of your code don't start searching for
boxes.  You want to have a list of you unaltered sliding windows,
and the identity function expresses this clearly.

In your problem statement, you wrote "want to compare the first
element with the last element".  Lucky you: J has "first" and
"last" as primitives {. and {: so there's no need for fidgeting
around with indexes.  We'll also reorder their application to match
your original problem statement more closely:

tl =: 4 : '({.a) -: ({:a =. y ]\ x)'

Repeated occurences of anything in the code can usually be factored
out in some way.  ({.a) -: ({:a)  is crying out loud to be re-written
with a fork:  ({. -: {:) a  and at that point you don't even need
your temp variable anymore:

tl =: 4 : '({. -: {:) y ]\ x'

To quote Laurie Anderson:  "much, Much, MUCH... better."
BTW, this might give you some ideas, too:

   (3 ,: _3) {. 'abcdefghijklmnopqrstuvwxyz'
abc
xyz

                                                        Martin Neitzel
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Checking if the first element is the same as the last

by Matthew Brand-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

4 : '({. -: {:) y ]\ x' does ({. -: {:) after doing y ]\ x , which is
({. -: {:)@: <something like y ]\ x>

y ]\ x is (]\)~

So can write:

({. -: {:) @: (]\)~


2009/11/10  <neitzel@...>:

> Hi Ian,
>
>>    t1 =: 4 : '(_1{a) -: (0{ a=. y >\/ (x))'
>
> Uuuh... let's _simplify_ this.
>
> (x) doesn't gain you anything, x will do.  (Too much C macros? :-)
>
> In  y >\/ x  what is the / supposed to do?  Was it an attempt to
> make "... tl 2 3 4" compute three matrices with different widths?
> I'd really like to know.  However, it does actually nothing in this
> context, and I strongly suggest to change the simplified y >/ x
> into a  y ]\ x so readers of your code don't start searching for
> boxes.  You want to have a list of you unaltered sliding windows,
> and the identity function expresses this clearly.
>
> In your problem statement, you wrote "want to compare the first
> element with the last element".  Lucky you: J has "first" and
> "last" as primitives {. and {: so there's no need for fidgeting
> around with indexes.  We'll also reorder their application to match
> your original problem statement more closely:
>
> tl =: 4 : '({.a) -: ({:a =. y ]\ x)'
>
> Repeated occurences of anything in the code can usually be factored
> out in some way.  ({.a) -: ({:a)  is crying out loud to be re-written
> with a fork:  ({. -: {:) a  and at that point you don't even need
> your temp variable anymore:
>
> tl =: 4 : '({. -: {:) y ]\ x'
>
> To quote Laurie Anderson:  "much, Much, MUCH... better."
> BTW, this might give you some ideas, too:
>
>   (3 ,: _3) {. 'abcdefghijklmnopqrstuvwxyz'
> abc
> xyz
>
>                                                        Martin Neitzel
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Checking if the first element is the same as the last

by Viktor Cerovski :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Try also:

   t2 =: {. -: -@[ {. ]

   a =: 1 2 3 4 1 2 3 4
   2 3 4 t2"0 1 a
0 0 1

It works like this:

     3{.a
1 2 3
   _3{.a
2 3 4  
   
   (4{.a) -: _4{.a
1

   4 t2 a
1

   2 4 t2"0 1 (1 2 3 4 1 2 3 4)
0 1

   2 4 t2"0 1 ] 1 2 3 4 1 2 3 4
0 1


unigee wrote:
Hi

I seem to be having a bit of trouble performing this idea.

I am trying to get a subset of a list, and comparing the first element
against the last element of the result.


    2 >\/ (1 2 3 4 1 2 3 4)
1 2
2 3
3 4
4 1
1 2
2 3
3 4

t1 =: 4 : '(_1{a) -: (0{ a=. y >\/ (x))'
   1 2 3 4 1 2 3 4 t1 2
0


   4 >\/ (1 2 3 4 1 2 3 4)
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
1 2 3 4

   1 2 3 4 1 2 3 4 t1 4
1

t1 works as intended, but I am having trouble making it work with more
than one right argument at a time
Here are my failed attempts.

   1 2 3 4 1 2 3 4 t1 2 3 4 NB. result should be 0 0 1
0

   1 2 3 4 1 2 3 4 t1"0 2 3 4
|length error
|   1 2 3 4 1 2 3 4     t1"0 2 3 4

   1 2 3 4 1 2 3 4 t1 each (2 3 4)
|length error
|   1 2 3 4 1 2 3 4     t1 each(2 3 4)

   (1 2 3 4 1 2 3 4) t1"0 (2 3 4)
|length error
|   (1 2 3 4 1 2 3 4)    t1"0(2 3 4)
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Checking if the first element is the same as the last

by unigee :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Although I have been using J for a while now, it has become a hobby of
mine to code with it. As a consequence I spend very little time
writing in J (perhaps 2 to 3 hours a week). As J and programming in
general has very little to do with my full time employment (in fact
they employ very different skill sets), its very easy to 'forget' how
things can be done.

The points you have outlined are simply, mistakes on my part as I
still class myself in a very elementary stage.

I raise my hat to you for the detailed explanations on how you
simplified my original t1 verb. As this mailing list is usually the
only way I get to learn how to do things better.

t1 =: 4 : '(_1{a) -: (0{ a=. y >\/ (x))'
t2 =: 4 : '({. -: {:) y ]\ x'

Its very nice how you broke t1 down into components and explained how
to simplify them, until the final result was t2,
I highly appreciate it.

Also, thanks Matthew and Viktor for the additional verbs.

On Tue, Nov 10, 2009 at 11:36 PM,  <neitzel@...> wrote:

> Hi Ian,
>
>>    t1 =: 4 : '(_1{a) -: (0{ a=. y >\/ (x))'
>
> Uuuh... let's _simplify_ this.
>
> (x) doesn't gain you anything, x will do.  (Too much C macros? :-)
>
> In  y >\/ x  what is the / supposed to do?  Was it an attempt to
> make "... tl 2 3 4" compute three matrices with different widths?
> I'd really like to know.  However, it does actually nothing in this
> context, and I strongly suggest to change the simplified y >/ x
> into a  y ]\ x so readers of your code don't start searching for
> boxes.  You want to have a list of you unaltered sliding windows,
> and the identity function expresses this clearly.
>
> In your problem statement, you wrote "want to compare the first
> element with the last element".  Lucky you: J has "first" and
> "last" as primitives {. and {: so there's no need for fidgeting
> around with indexes.  We'll also reorder their application to match
> your original problem statement more closely:
>
> tl =: 4 : '({.a) -: ({:a =. y ]\ x)'
>
> Repeated occurences of anything in the code can usually be factored
> out in some way.  ({.a) -: ({:a)  is crying out loud to be re-written
> with a fork:  ({. -: {:) a  and at that point you don't even need
> your temp variable anymore:
>
> tl =: 4 : '({. -: {:) y ]\ x'
>
> To quote Laurie Anderson:  "much, Much, MUCH... better."
> BTW, this might give you some ideas, too:
>
>   (3 ,: _3) {. 'abcdefghijklmnopqrstuvwxyz'
> abc
> xyz
>
>                                                        Martin Neitzel
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm