return index of array within an array (or List... in this case)

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

return index of array within an array (or List... in this case)

by scacinto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

j = List[[0,2,3], [4,2,0], [3,0,4]];
j.at(2) // this works fine -- returns the array
[3,0,4]

j.indexOf([3,0,4]); // this returns 'nil'
j.includes([3,0,4]); // also returns 'nil'

is there some 'majicke' I can work on this list to return the index of the array or at least answer true?

thanx

S

Re: return index of array within an array (or List... in this case)

by James Harkins-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It's important to understand why 'includes' doesn't work in this case. It's the difference between equality and identity.

// execute each line separately

a = [3, 0, 4];

b = [3, 0, 4];

// equality test
a == b
--> true

// identity test
a === b
--> false

And the cruncher...

a.dump; b.dump;

Instance of Array {    (0E6DED00, gc=40, fmt=01, flg=00, set=02)
  indexed slots [3]
      0 : Integer 3
      1 : Integer 0
      2 : Integer 4
}
Instance of Array {    (0D5FD350, gc=40, fmt=01, flg=00, set=02)
  indexed slots [3]
      0 : Integer 3
      1 : Integer 0
      2 : Integer 4
}

Note that the memory addresses are different - 0E6DED00 vs 0D5FD350. Since they live at different places, they can't be the same object, and the identity test is true only if they are exactly the same object (not different instances of the same class with the same contents).

'Includes' tests for identity, not equality.

If you're using my library, you can use includesEqual which uses an equality test and will give you the results you want.

hjh



On Jun 30, 2008, at 9:48 PM, scacinto wrote:


j = List[[0,2,3], [4,2,0], [3,0,4]];
j.at(2) // this works fine -- returns the array
[3,0,4]

j.indexOf([3,0,4]); // this returns 'nil'
j.includes([3,0,4]); // also returns 'nil'

is there some 'majicke' I can work on this list to return the index of the
array or at least answer true?


: H. James Harkins

: jamshark70@...

: http://www.dewdrop-world.net

.::!:.:.......:.::........:..!.::.::...:..:...:.:.:.:..:


"Come said the Muse,

Sing me a song no poet has yet chanted,

Sing me the universal."  -- Whitman



Re: return index of array within an array (or List... in this case)

by scacinto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Good to know, thank you.  Using my deductive reasoning skills, I'll assume that indexOf also tests for identity and not equality.  Is there a general rule for, or way of finding out, what tests for which?

Thanks again.

-S



James Harkins-2 wrote:
It's important to understand why 'includes' doesn't work in this  
case. It's the difference between equality and identity.

// execute each line separately

a = [3, 0, 4];

b = [3, 0, 4];

// equality test
a == b
--> true

// identity test
a === b
--> false

And the cruncher...

a.dump; b.dump;

Instance of Array {    (0E6DED00, gc=40, fmt=01, flg=00, set=02)
   indexed slots [3]
       0 : Integer 3
       1 : Integer 0
       2 : Integer 4
}
Instance of Array {    (0D5FD350, gc=40, fmt=01, flg=00, set=02)
   indexed slots [3]
       0 : Integer 3
       1 : Integer 0
       2 : Integer 4
}

Note that the memory addresses are different - 0E6DED00 vs 0D5FD350.  
Since they live at different places, they can't be the same object,  
and the identity test is true only if they are exactly the same  
object (not different instances of the same class with the same  
contents).

'Includes' tests for identity, not equality.

If you're using my library, you can use includesEqual which uses an  
equality test and will give you the results you want.

hjh



On Jun 30, 2008, at 9:48 PM, scacinto wrote:

>
> j = List[[0,2,3], [4,2,0], [3,0,4]];
> j.at(2) // this works fine -- returns the array
> [3,0,4]
>
> j.indexOf([3,0,4]); // this returns 'nil'
> j.includes([3,0,4]); // also returns 'nil'
>
> is there some 'majicke' I can work on this list to return the index  
> of the
> array or at least answer true?


: H. James Harkins
: jamshark70@dewdrop-world.net
: http://www.dewdrop-world.net
.::!:.:.......:.::........:..!.::.::...:..:...:.:.:.:..:

"Come said the Muse,
Sing me a song no poet has yet chanted,
Sing me the universal."  -- Whitman

Re: return index of array within an array (or List... in this case)

by Dan Stowell :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

James mentioned "includesEqual" which is in his library. If you want a
solution that works on pure unadulterated SC, then note that the
"indexOfEqual" method is built-in and does the equivalent thing for
indexOf.

j = List[[0,2,3], [4,2,0], [3,0,4]];

j.indexOfEqual([3,0,4]);  // Finds it!
j.indexOfEqual([3,0,4]).notNil; // this does the same thing as 'includesEqual'

>Is there a general
> rule for, or way of finding out, what tests for which?

Good question. I don't know if I can state a general rule; but to find
out, look at the definition of the method. Highlight the word
"indexOf" in SC and press Cmd-Y. You'll get a list appearing, of
different classes' implementations. Out of those, highlight
"SequenceableCollection:indexOf" and press Cmd-Y. You'll see the
method's implementation, which uses a loop and a test of identity (the
triple equals sign, ===).

It can be a bit strange at first, trying to un-pick the source code in
this way, but it's often helpful.

Dan



2008/7/1 scacinto <scottpetersen@...>:

>
> Good to know, thank you.  Using my deductive reasoning skills, I'll assume
> that indexOf also tests for identity and not equality.  Is there a general
> rule for, or way of finding out, what tests for which?
>
> Thanks again.
>
> -S
>
>
>
>
> James Harkins-2 wrote:
>>
>> It's important to understand why 'includes' doesn't work in this
>> case. It's the difference between equality and identity.
>>
>> // execute each line separately
>>
>> a = [3, 0, 4];
>>
>> b = [3, 0, 4];
>>
>> // equality test
>> a == b
>> --> true
>>
>> // identity test
>> a === b
>> --> false
>>
>> And the cruncher...
>>
>> a.dump; b.dump;
>>
>> Instance of Array {    (0E6DED00, gc=40, fmt=01, flg=00, set=02)
>>    indexed slots [3]
>>        0 : Integer 3
>>        1 : Integer 0
>>        2 : Integer 4
>> }
>> Instance of Array {    (0D5FD350, gc=40, fmt=01, flg=00, set=02)
>>    indexed slots [3]
>>        0 : Integer 3
>>        1 : Integer 0
>>        2 : Integer 4
>> }
>>
>> Note that the memory addresses are different - 0E6DED00 vs 0D5FD350.
>> Since they live at different places, they can't be the same object,
>> and the identity test is true only if they are exactly the same
>> object (not different instances of the same class with the same
>> contents).
>>
>> 'Includes' tests for identity, not equality.
>>
>> If you're using my library, you can use includesEqual which uses an
>> equality test and will give you the results you want.
>>
>> hjh
>>
>>
>>
>> On Jun 30, 2008, at 9:48 PM, scacinto wrote:
>>
>>>
>>> j = List[[0,2,3], [4,2,0], [3,0,4]];
>>> j.at(2) // this works fine -- returns the array
>>> [3,0,4]
>>>
>>> j.indexOf([3,0,4]); // this returns 'nil'
>>> j.includes([3,0,4]); // also returns 'nil'
>>>
>>> is there some 'majicke' I can work on this list to return the index
>>> of the
>>> array or at least answer true?
>>
>>
>> : H. James Harkins
>> : jamshark70@...
>> : http://www.dewdrop-world.net
>> .::!:.:.......:.::........:..!.::.::...:..:...:.:.:.:..:
>>
>> "Come said the Muse,
>> Sing me a song no poet has yet chanted,
>> Sing me the universal."  -- Whitman
>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/return-index-of-array-within-an-array-%28or-List...-in-this-case%29-tp18207750p18208771.html
> Sent from the Supercollider - User mailing list archive at Nabble.com.
>
>
> _______________________________________________
> sc-users mailing list
>
>
> info (subscribe and unsubscribe): http://swiki.hfbk-hamburg.de:8888/MusicTechnology/880
> archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
> search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
>



--
http://www.mcld.co.uk

_______________________________________________
sc-users mailing list


info (subscribe and unsubscribe): http://swiki.hfbk-hamburg.de:8888/MusicTechnology/880
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/

Re: return index of array within an array (or List... in this case)

by scacinto :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Indeed, yes!  Thanks Dan.  I did a search for methods with "Equal" in them and discovered these, much to my satisfaction.  Checking for the '===' is a great idea, as well.  The reason for my general rule was to see if there was a quick and dirty was of establishing identity.  It seems that any integer is simply an integer -- returns an integer, etc. so there's no way of telling one from another.  At the same,  a = 'a' and b = 'a' are equivalent but a = "a" and b = "a" are not.  Of course, the string has indexed slots as the array does, and so is identifiable by its memory allocation number thingy -- and the same for functions.

Thnks for your post

S


Dan Stowell wrote:
James mentioned "includesEqual" which is in his library. If you want a
solution that works on pure unadulterated SC, then note that the
"indexOfEqual" method is built-in and does the equivalent thing for
indexOf.

j = List[[0,2,3], [4,2,0], [3,0,4]];

j.indexOfEqual([3,0,4]);  // Finds it!
j.indexOfEqual([3,0,4]).notNil; // this does the same thing as 'includesEqual'

>Is there a general
> rule for, or way of finding out, what tests for which?

Good question. I don't know if I can state a general rule; but to find
out, look at the definition of the method. Highlight the word
"indexOf" in SC and press Cmd-Y. You'll get a list appearing, of
different classes' implementations. Out of those, highlight
"SequenceableCollection:indexOf" and press Cmd-Y. You'll see the
method's implementation, which uses a loop and a test of identity (the
triple equals sign, ===).

It can be a bit strange at first, trying to un-pick the source code in
this way, but it's often helpful.

Dan



2008/7/1 scacinto <scottpetersen@frontiernet.net>:
>
> Good to know, thank you.  Using my deductive reasoning skills, I'll assume
> that indexOf also tests for identity and not equality.  Is there a general
> rule for, or way of finding out, what tests for which?
>
> Thanks again.
>
> -S
>
>
>
>
> James Harkins-2 wrote:
>>
>> It's important to understand why 'includes' doesn't work in this
>> case. It's the difference between equality and identity.
>>
>> // execute each line separately
>>
>> a = [3, 0, 4];
>>
>> b = [3, 0, 4];
>>
>> // equality test
>> a == b
>> --> true
>>
>> // identity test
>> a === b
>> --> false
>>
>> And the cruncher...
>>
>> a.dump; b.dump;
>>
>> Instance of Array {    (0E6DED00, gc=40, fmt=01, flg=00, set=02)
>>    indexed slots [3]
>>        0 : Integer 3
>>        1 : Integer 0
>>        2 : Integer 4
>> }
>> Instance of Array {    (0D5FD350, gc=40, fmt=01, flg=00, set=02)
>>    indexed slots [3]
>>        0 : Integer 3
>>        1 : Integer 0
>>        2 : Integer 4
>> }
>>
>> Note that the memory addresses are different - 0E6DED00 vs 0D5FD350.
>> Since they live at different places, they can't be the same object,
>> and the identity test is true only if they are exactly the same
>> object (not different instances of the same class with the same
>> contents).
>>
>> 'Includes' tests for identity, not equality.
>>
>> If you're using my library, you can use includesEqual which uses an
>> equality test and will give you the results you want.
>>
>> hjh
>>
>>
>>
>> On Jun 30, 2008, at 9:48 PM, scacinto wrote:
>>
>>>
>>> j = List[[0,2,3], [4,2,0], [3,0,4]];
>>> j.at(2) // this works fine -- returns the array
>>> [3,0,4]
>>>
>>> j.indexOf([3,0,4]); // this returns 'nil'
>>> j.includes([3,0,4]); // also returns 'nil'
>>>
>>> is there some 'majicke' I can work on this list to return the index
>>> of the
>>> array or at least answer true?
>>
>>
>> : H. James Harkins
>> : jamshark70@dewdrop-world.net
>> : http://www.dewdrop-world.net
>> .::!:.:.......:.::........:..!.::.::...:..:...:.:.:.:..:
>>
>> "Come said the Muse,
>> Sing me a song no poet has yet chanted,
>> Sing me the universal."  -- Whitman
>>
>>
>>
>
> --
> View this message in context: http://www.nabble.com/return-index-of-array-within-an-array-%28or-List...-in-this-case%29-tp18207750p18208771.html
> Sent from the Supercollider - User mailing list archive at Nabble.com.
>
>
> _______________________________________________
> sc-users mailing list
>
>
> info (subscribe and unsubscribe): http://swiki.hfbk-hamburg.de:8888/MusicTechnology/880
> archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
> search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
>



--
http://www.mcld.co.uk

_______________________________________________
sc-users mailing list


info (subscribe and unsubscribe): http://swiki.hfbk-hamburg.de:8888/MusicTechnology/880
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
LightInTheBox - Buy quality products at wholesale price