[sharingisfun] js based change filter

Sep 6, 2007 at 6:23pm

[sharingisfun] js based change filter

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.

#33569
Sep 6, 2007 at 6:31pm

#111940
Sep 6, 2007 at 7:39pm

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

#111941
Sep 6, 2007 at 8:04pm

#111942
Sep 6, 2007 at 9:17pm

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

#111943
Sep 6, 2007 at 11:02pm

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

#111944
Sep 6, 2007 at 11:33pm

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
>
>
>

#111945
Sep 7, 2007 at 3:55pm

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

#111946
Sep 8, 2007 at 3:06am

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);

#111947
Sep 8, 2007 at 2:32pm

#111948
Sep 8, 2007 at 7:04pm

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

#111949
Sep 13, 2007 at 7:21pm

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.

#111950

You must be logged in to reply to this topic.