Forums > Javascript

[sharingisfun] js based change filter

September 6, 2007 | 6:23 pm

A javascript and help file for a "change" style filter for any messsage, list, float, or int, rather than just ints or floats.

http://www.yowstar.com/share/changeany.zip

Enjoy,

Peter.


September 6, 2007 | 6:31 pm


September 6, 2007 | 7:39 pm

whoops, grabbed the .pat, not the .js, for the archive.
.zip updated!


September 6, 2007 | 8:04 pm


September 6, 2007 | 9:17 pm

On 6 sept. 07, at 22:04, keith manlove wrote:

> One thing… and forgive me if this is obvious… what’s the anything
> function for?

anything allows you to receive any message. For example, if you send
an integer and the msg_int method is not defined, the anything
function will be triggered. This is espacially useful for symbols as
it’s not possible to think about every symbol you could send.

ej


September 6, 2007 | 11:02 pm

On 6 sept. 07, at 23:17, Emmanuel Jourdan wrote:

> anything allows you to receive any message. For example, if you
> send an integer and the msg_int method is not defined, the anything
> function will be triggered. This is espacially useful for symbols
> as it’s not possible to think about every symbol you could send.

Maybe an example will make it clearer, to achieve the changeany you
could have only use the anything method:

var lastThing = new Array();

function anything()
{
var i;
var tmp = arrayfromargs(messagename, arguments); // make an array
from the message
var isDifferent = false;

if (lastThing.length == tmp.length) {
for (i = 0; i < tmp.length; i++)
if (lastThing[i] != tmp[i]) {
isDifferent = true;
break;
}
} else
isDifferent = true;

if (isDifferent) { // it has changed
outlet(0, tmp);
lastThing = arrayfromargs(messagename, arguments); // it’s easier
to call arrayfromargs again to make the copy of the array
}
}

ej


September 6, 2007 | 11:33 pm

Thanks a lot for both of these. I’ve been learning javascript and
seeing examples like these really enlighten things for me. Very
slick. The use of arrayfromargs is especially nice.

Thanks again,
Keith

On 9/6/07, Emmanuel Jourdan wrote:
> On 6 sept. 07, at 23:17, Emmanuel Jourdan wrote:
>
> > anything allows you to receive any message. For example, if you
> > send an integer and the msg_int method is not defined, the anything
> > function will be triggered. This is espacially useful for symbols
> > as it’s not possible to think about every symbol you could send.
>
> Maybe an example will make it clearer, to achieve the changeany you
> could have only use the anything method:
>
> var lastThing = new Array();
>
> function anything()
> {
> var i;
> var tmp = arrayfromargs(messagename, arguments); // make an array
> from the message
> var isDifferent = false;
>
> if (lastThing.length == tmp.length) {
> for (i = 0; i < tmp.length; i++)
> if (lastThing[i] != tmp[i]) {
> isDifferent = true;
> break;
> }
> } else
> isDifferent = true;
>
> if (isDifferent) { // it has changed
> outlet(0, tmp);
> lastThing = arrayfromargs(messagename, arguments); // it’s easier
> to call arrayfromargs again to make the copy of the array
> }
> }
>
> ej
>
>
>


September 7, 2007 | 3:55 pm

On 7 sept. 07, at 01:33, keith manlove wrote:

> Thanks a lot for both of these. I’ve been learning javascript and
> seeing examples like these really enlighten things for me. Very
> slick. The use of arrayfromargs is especially nice.

Sure. arrayfromargs is really cool in that particular case, because
it’s an easy way to copy the array, which is usually a lame is
JavaScript.

ej


September 8, 2007 | 3:06 am

Quote: Emmanuel Jourdan wrote on Sat, 08 September 2007 03:55
—————————————————-
> it’s an easy way to copy the array, which is usually a lame is
> JavaScript.

I don’t see any practical difference between a hypothetical array copy method like:

newarray = oldarray.copy();

and using the slice method:

newarray = oldarray.slice(0);

But in any case, you can always add a copy method to the Array object prototype:

Array.prototype.copy = function()
{
return this.slice(0);
}

a = new Array(1, 2, 3, 4, 5);
b = a.copy();
post(b);


September 8, 2007 | 2:32 pm


September 8, 2007 | 7:04 pm

On Sep 7, 2007, at 8:55 AM, Emmanuel Jourdan wrote:

> Sure. arrayfromargs is really cool in that particular case, because
> it’s an easy way to copy the array, which is usually a lame is
> JavaScript.

FWIW, the real reason that arrayfromargs() exists is that the
arguments object is *not* an array, even though it can be accessed
with array syntax. More info below:

http://developer.mozilla.org/en/docs/
Core_JavaScript_1.5_Reference:Functions:arguments

-Joshua


September 13, 2007 | 7:21 pm

ej,
thanks for the more elegant version. I suspected there was a way to leverage anything() to take care of, well, everything, but I was getting hung up on it for reasons i can’t recall.
and thanks for the slice reminder – i always forget about that!

Peter.


Viewing 12 posts - 1 through 12 (of 12 total)