« Return to Thread: instance methods

Re: passing in arrays into a function

by Stefan Nussbaumer-3 :: Rate this Message:

Reply to Author | View in Thread

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

 « Return to Thread: instance methods

LightInTheBox - Buy quality products at wholesale price