instance methods

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

instance methods

by Stefan Nussbaumer-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

sorry, probably i'm missing something ...

i have this class-definition:

Test {

    someTest { arg in; ^in*5 }

}

after having recompiled i'm trying to execute this:

10.someTest.postln

shouldn't that post 50? i'm just getting an error: ERROR: Message
'somTest' not understood (and some other lines ...).


i've read through the writing classes help and i've written a couple of
class methods that work the way they are supposed to. what am i missing
about instance methods?

thanks for any help

stefan
_______________________________________________
sc-users mailing list
sc-users@...
http://lists.create.ucsb.edu/mailman/listinfo/sc-users

Re: instance methods

by Josh Parmenter :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

But, 10 isn't an instance of Test... you would need to do:

a = Test.new;
a.someTest(10);

Josh

On May 12, 2008, at 9:33 AM, Stefan Nussbaumer wrote:

> sorry, probably i'm missing something ...
>
> i have this class-definition:
>
> Test {
>
>    someTest { arg in; ^in*5 }
>
> }
>
> after having recompiled i'm trying to execute this:
>
> 10.someTest.postln
>
> shouldn't that post 50? i'm just getting an error: ERROR: Message
> 'somTest' not understood (and some other lines ...).
>
>
> i've read through the writing classes help and i've written a couple  
> of
> class methods that work the way they are supposed to. what am i  
> missing
> about instance methods?
>
> thanks for any help
>
> stefan
> _______________________________________________
> sc-users mailing list
> sc-users@...
> http://lists.create.ucsb.edu/mailman/listinfo/sc-users

******************************************
/* Joshua D. Parmenter
http://www.realizedsound.net/josh/

“Every composer – at all times and in all cases – gives his own  
interpretation of how modern society is structured: whether actively  
or passively, consciously or unconsciously, he makes choices in this  
regard. He may be conservative or he may subject himself to continual  
renewal; or he may strive for a revolutionary, historical or social  
palingenesis." - Luigi Nono
*/

_______________________________________________
sc-users mailing list
sc-users@...
http://lists.create.ucsb.edu/mailman/listinfo/sc-users

Re: instance methods

by Eirik Blekesaune :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Stefan,

If you create the method definition like this:

Test {
        *someTest {arg in; ^in*5}
}

you've created a classmethod which you can run with:
Test.someTest(10) //returns 50


If you want to use an instance method you need to create an instance  
of Test:
a = Test.new; //create an instance of Test

Then you can use the method for that object:
a.someTest(10) //returns 50


The reason that the example you posted doesn't work is that the number  
10 is in fact an instance of Integer ( 10.class.postln ), not an  
instance of Test. This gives you access to Integers method, and the  
methods of Integers superclasses.

-Eirik




Den 12. mai. 2008 kl. 18.33 skrev Stefan Nussbaumer:

> sorry, probably i'm missing something ...
>
> i have this class-definition:
>
> Test {
>
>    someTest { arg in; ^in*5 }
>
> }
>
> after having recompiled i'm trying to execute this:
>
> 10.someTest.postln
>
> shouldn't that post 50? i'm just getting an error: ERROR: Message
> 'somTest' not understood (and some other lines ...).
>
>
> i've read through the writing classes help and i've written a couple  
> of
> class methods that work the way they are supposed to. what am i  
> missing
> about instance methods?
>
> thanks for any help
>
> stefan
> _______________________________________________
> sc-users mailing list
> sc-users@...
> http://lists.create.ucsb.edu/mailman/listinfo/sc-users

_______________________________________________
sc-users mailing list
sc-users@...
http://lists.create.ucsb.edu/mailman/listinfo/sc-users

Re: passing in arrays into a function (was: instance methods)

by Stefan Nussbaumer-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Josh Parmenter schrieb:
> But, 10 isn't an instance of Test... you would need to do:
>
> a = Test.new;
> a.someTest(10);

stupid me, of course ... sorry. spring has let my braincells go for a
walk ...

much rather i'm interested how and if i can pass in an array into a
function (a method). currently i'm doing something like this:

*myMethod { arg min, max;
    ...
}

which let's me operate on a consecutive range of numbers.
instead of a range i'd like to pass in an array like  [ 34, 35, 39, 42 ]

would there be an easy way of doing this?

thanks, stefan

_______________________________________________
sc-users mailing list
sc-users@...
http://lists.create.ucsb.edu/mailman/listinfo/sc-users

Re: passing in arrays into a function (was: instance methods)

by Josh Parmenter :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ah... spring. It may show up here in Seattle soon.

Not sure what you mean... you can already pass in the array if you  
want...

if myMethod is:

        *myMethod {arg in;
                ^in * 5;
        }

Then

a = Test.myMethod(10); // should work
a = Test.myMethod([0, 1, 2, 3, 4]); // should also work.

Or - do you want the method to do something different with the input?  
Or make the input flexible to take multiple values???

        *myMethod {arg ... in; // in will always be an array... no matter how  
many elements you pass in
                ^[in.minItem, in.maxItem]
                }

Then:

a = Test.myMethod(10); // should return [10, 10]
a = Test.myMethod(10, 12, 4, 56, 102); // should return [4, 102]

Hope that help.

Josh

On May 12, 2008, at 10:03 AM, Stefan Nussbaumer wrote:

> Josh Parmenter schrieb:
>> But, 10 isn't an instance of Test... you would need to do:
>>
>> a = Test.new;
>> a.someTest(10);
>
> stupid me, of course ... sorry. spring has let my braincells go for a
> walk ...
>
> much rather i'm interested how and if i can pass in an array into a
> function (a method). currently i'm doing something like this:
>
> *myMethod { arg min, max;
>    ...
> }
>
> which let's me operate on a consecutive range of numbers.
> instead of a range i'd like to pass in an array like  [ 34, 35, 39,  
> 42 ]
>
> would there be an easy way of doing this?
>
> thanks, stefan
>
> _______________________________________________
> sc-users mailing list
> sc-users@...
> http://lists.create.ucsb.edu/mailman/listinfo/sc-users

******************************************
/* Joshua D. Parmenter
http://www.realizedsound.net/josh/

“Every composer – at all times and in all cases – gives his own  
interpretation of how modern society is structured: whether actively  
or passively, consciously or unconsciously, he makes choices in this  
regard. He may be conservative or he may subject himself to continual  
renewal; or he may strive for a revolutionary, historical or social  
palingenesis." - Luigi Nono
*/

_______________________________________________
sc-users mailing list
sc-users@...
http://lists.create.ucsb.edu/mailman/listinfo/sc-users

Re: passing in arrays into a function

by Stefan Nussbaumer-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Josh Parmenter schrieb:

> ...
>
> a = Test.myMethod(10); // should work
> a = Test.myMethod([0, 1, 2, 3, 4]); // should also work.
>
> Or - do you want the method to do something different with the input?  
> Or make the input flexible to take multiple values???
>
> *myMethod {arg ... in; // in will always be an array... no matter how  
> many elements you pass in
> ^[in.minItem, in.maxItem]
> }
>
> Then:
>
> a = Test.myMethod(10); // should return [10, 10]
> a = Test.myMethod(10, 12, 4, 56, 102); // should return [4, 102]
>
> Hope that help.
>  

that help a lot :) thanks for clarification ... i've always been
confused about this

i've broken my function down to this now. i want to sort out
"characterists" of numbers - say, i'm dividing it by itsself to one and
any time a whole-numbered result is returned i'll add it to my
output-array (i have to cheat a little bit as i don't want to have empty
arrays in my result, so i just add 1 to the original value and let
function work on it again ... until i have the number of slots in my
output array:

    *divFunc { arg val, list, limit, inList, secondrun=false, pat=nil;
            var temp, outList, lim, max, min, orig;
            if(secondrun == false, { orig = list }, { orig = pat });
//            [list, inList].postln;
            max = list.maxItem;
//            max.postln;
            min = list.minItem;
            if(limit.notNil, {
                if(list.size < limit, { lim = list.size }, { lim = limit });
            });
//            limit.postln;
            if(inList.isNil, { outList = Array() }, { outList = inList });
            val.isPrime.if{val = val + 1};
            val.do{ |i| if(list.includes(i), {
                temp = val/i;
                if(temp.mod(1) == 0 and:{ outList.includes(i).not }
and:{ orig.includes(i) }, { outList = outList.add(i) });
            })};
            if(limit.notNil and:{ outList.size < lim }, {
//                "case 1".postln;
                ^this.divFuncArr(val+1, list, lim, outList, true, orig)
            }, {
//                "case 2".postln;
                ^outList.sort
            });
    }


should work:

Test.divFunc(23187, [4, 7, 17, 18], 3) // given you name the class
"Test" and the number to operate on isn't too high ...

good way to go or can this be optimized?


thanks a lot again (and hope, spring is going to arrive in seattle soon)

stefan
_______________________________________________
sc-users mailing list
sc-users@...
http://lists.create.ucsb.edu/mailman/listinfo/sc-users