Function into Array (beginner)

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Function into Array (beginner)

Marigold Maripol
Hi SCers-

I'm learning about functions at the moment and was hoping for a little
help in translating them for use in SC.

Here's an example:
This is ideally supposed to take two arguments: a value for where "x"
should start and a value for "y", which, if exceeded, will break the
loop:

((
a={
arg x=10, y=100;
 {x<y}.while ({
    x=x+1;
x=x*8;
x.postln;
});
});
a.value;
)

I have two issues that I'd like to ask for advice on:
First, it seems like the loop breaks after the second number is
computed and "y" has been exceeded.

Secondly, I'd like to be able to collect the results of this procedure
in an array.

Can anyone show me how to manage these problems?

Thanks,
MM

_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Function into Array (beginner)

5tefan


Am 2017-01-11 um 01:05 schrieb Marigold Maripol:

> Hi SCers-
>
> I'm learning about functions at the moment and was hoping for a little
> help in translating them for use in SC.
>
> Here's an example:
> This is ideally supposed to take two arguments: a value for where "x"
> should start and a value for "y", which, if exceeded, will break the
> loop:
>
> ((
> a={
> arg x=10, y=100;
>  {x<y}.while ({
>     x=x+1;
> x=x*8;
> x.postln;
> });
> });
> a.value;
> )
>
> I have two issues that I'd like to ask for advice on:
> First, it seems like the loop breaks after the second number is
> computed and "y" has been exceeded.
>
> Secondly, I'd like to be able to collect the results of this procedure
> in an array.
>
> Can anyone show me how to manage these problems?

Maybe like this?

// collect takes an array of values (integers from 10 to 99 in this
// case), passes each value in the array to the
// function and creates an array of the results
~arrFunc = { (10..99).collect{ |x| (x * 8).postln } };
~arrFunc.value;

hth, STefan


_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Function into Array (beginner)

Marigold Maripol
thanks for the advice..
i guess i left something out, though:
i'd like to be able to change the values of x and y dynamically,
generating different size arrays.
is there a way to factor that into the code?

On Tue, Jan 10, 2017 at 8:16 PM, Stefan Nussbaumer <[hidden email]> wrote:

>
>
> Am 2017-01-11 um 01:05 schrieb Marigold Maripol:
>> Hi SCers-
>>
>> I'm learning about functions at the moment and was hoping for a little
>> help in translating them for use in SC.
>>
>> Here's an example:
>> This is ideally supposed to take two arguments: a value for where "x"
>> should start and a value for "y", which, if exceeded, will break the
>> loop:
>>
>> ((
>> a={
>> arg x=10, y=100;
>>  {x<y}.while ({
>>     x=x+1;
>> x=x*8;
>> x.postln;
>> });
>> });
>> a.value;
>> )
>>
>> I have two issues that I'd like to ask for advice on:
>> First, it seems like the loop breaks after the second number is
>> computed and "y" has been exceeded.
>>
>> Secondly, I'd like to be able to collect the results of this procedure
>> in an array.
>>
>> Can anyone show me how to manage these problems?
>
> Maybe like this?
>
> // collect takes an array of values (integers from 10 to 99 in this
> // case), passes each value in the array to the
> // function and creates an array of the results
> ~arrFunc = { (10..99).collect{ |x| (x * 8).postln } };
> ~arrFunc.value;
>
> hth, STefan
>
>
> _______________________________________________
> sc-users mailing list
>
> info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
> archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
> search: http://www.listarc.bham.ac.uk/lists/sc-users/search/

_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Function into Array (beginner)

Ofelia Negrete
(
a = {
arg x = b, y = c;
{x<y}.while({
x = x + 1;
x = x*8;
x.postln
});
});





b = 4;
c = 100;


a.value;


if you change b & c, the result changes

On Tue, Jan 10, 2017 at 8:23 PM, Marigold Maripol <[hidden email]> wrote:
thanks for the advice..
i guess i left something out, though:
i'd like to be able to change the values of x and y dynamically,
generating different size arrays.
is there a way to factor that into the code?

On Tue, Jan 10, 2017 at 8:16 PM, Stefan Nussbaumer <[hidden email]> wrote:
>
>
> Am 2017-01-11 um 01:05 schrieb Marigold Maripol:
>> Hi SCers-
>>
>> I'm learning about functions at the moment and was hoping for a little
>> help in translating them for use in SC.
>>
>> Here's an example:
>> This is ideally supposed to take two arguments: a value for where "x"
>> should start and a value for "y", which, if exceeded, will break the
>> loop:
>>
>> ((
>> a={
>> arg x=10, y=100;
>>  {x<y}.while ({
>>     x=x+1;
>> x=x*8;
>> x.postln;
>> });
>> });
>> a.value;
>> )
>>
>> I have two issues that I'd like to ask for advice on:
>> First, it seems like the loop breaks after the second number is
>> computed and "y" has been exceeded.
>>
>> Secondly, I'd like to be able to collect the results of this procedure
>> in an array.
>>
>> Can anyone show me how to manage these problems?
>
> Maybe like this?
>
> // collect takes an array of values (integers from 10 to 99 in this
> // case), passes each value in the array to the
> // function and creates an array of the results
> ~arrFunc = { (10..99).collect{ |x| (x * 8).postln } };
> ~arrFunc.value;
>
> hth, STefan
>
>
> _______________________________________________
> sc-users mailing list
>
> info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
> archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
> search: http://www.listarc.bham.ac.uk/lists/sc-users/search/

_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Function into Array (beginner)

Eli Fieldsteel
In reply to this post by Marigold Maripol
It's important to separate the value being modified from the value used for incrementing the conditional test. In your case, they are both x.

Perhaps this?

Eli

(
a = {
        arg x=10, y=100;
        var ar;
        ar = []; //variable ar initially an empty array
        {x < y}.while ({
                ar = ar.add(x * 8); //add the computed result to the array
                x = x + 1; //increment
        });
        ar; //when x ≥ y, return the result
};
)

~myArray = a.value; //default values

~myArray = a.value(20,50); //user-specified values for args x and y
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Function into Array (beginner)

Marigold Maripol
Thanks, Eli - that covers it!

On Tue, Jan 10, 2017 at 10:29 PM, Eli Fieldsteel
<[hidden email]> wrote:

> It's important to separate the value being modified from the value used for
> incrementing the conditional test. In your case, they are both x.
>
> Perhaps this?
>
> Eli
>
> (
> a = {
>         arg x=10, y=100;
>         var ar;
>         ar = []; //variable ar initially an empty array
>         {x < y}.while ({
>                 ar = ar.add(x * 8); //add the computed result to the array
>                 x = x + 1; //increment
>         });
>         ar; //when x ≥ y, return the result
> };
> )
>
> ~myArray = a.value; //default values
>
> ~myArray = a.value(20,50); //user-specified values for args x and y
>
>
>
> --
> View this message in context: http://new-supercollider-mailing-lists-forums-use-these.2681727.n2.nabble.com/Function-into-Array-beginner-tp7630058p7630062.html
> Sent from the SuperCollider Users New (Use this!!!!) mailing list archive at Nabble.com.
>
> _______________________________________________
> sc-users mailing list
>
> info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
> archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
> search: http://www.listarc.bham.ac.uk/lists/sc-users/search/

_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Function into Array (beginner)

Marigold Maripol
Alright, sorry, one more thing..

If I'd like to process that array, to demodulate too-high frequencies
and scale-up too-low frequencies, ... is there a recommended practice
in SC?

Here's a way that doesn't work:
(
b=(
({
arg size=100;
var x=0, newAr=[], g=~myArray;
     {x<=(size-1)}.while({
         newAr = newAr.add(
         if (g[x]>12000)
         {g[x]=g[x]/2; x=x+1};

         if (g[x]<20)
         {g[x]=g[x]*10; x=x+1}
         {x=x+1};
)
});
});
)
)

On Wed, Jan 11, 2017 at 12:35 AM, Marigold Maripol
<[hidden email]> wrote:

> Thanks, Eli - that covers it!
>
> On Tue, Jan 10, 2017 at 10:29 PM, Eli Fieldsteel
> <[hidden email]> wrote:
>> It's important to separate the value being modified from the value used for
>> incrementing the conditional test. In your case, they are both x.
>>
>> Perhaps this?
>>
>> Eli
>>
>> (
>> a = {
>>         arg x=10, y=100;
>>         var ar;
>>         ar = []; //variable ar initially an empty array
>>         {x < y}.while ({
>>                 ar = ar.add(x * 8); //add the computed result to the array
>>                 x = x + 1; //increment
>>         });
>>         ar; //when x ≥ y, return the result
>> };
>> )
>>
>> ~myArray = a.value; //default values
>>
>> ~myArray = a.value(20,50); //user-specified values for args x and y
>>
>>
>>
>> --
>> View this message in context: http://new-supercollider-mailing-lists-forums-use-these.2681727.n2.nabble.com/Function-into-Array-beginner-tp7630058p7630062.html
>> Sent from the SuperCollider Users New (Use this!!!!) mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> sc-users mailing list
>>
>> info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
>> archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
>> search: http://www.listarc.bham.ac.uk/lists/sc-users/search/

_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Function into Array (beginner)

Eli Fieldsteel
Rather than a conditional loop (while, for, etc), I would prefer to use a method that iterates automatically (do, collect, select, reject, etc), especially if you're always going to be incrementing the index by 1 on each pass.

(
//original function
a = {
        arg x=10, y=100;
        var ar;
        ar = []; //variable ar initially an empty array
        {x < y}.while ({
                ar = ar.add(x * 8); //add the computed result to the array
                x = x + 1; //increment
        });
        ar; //when x ≥ y, return the result
};

//frequency "demodulating" function
b = {
        arg myArray = [];
        myArray.collect({
                arg val;
                case
                {val>12000}{val/2} //if too high
                {val<20}{val*10} //if too low
                {true}{val}; //else, do nothing
        });
};
)

~myArray = a.value; //default values

~myArray = a.value(1,10); //create an array with values too low
~myArray = ~fixFreqs.value(~myArray); //fixed;

~myArray = a.value(2000,2020); //array with values too high
~myArray = ~fixFreqs.value(~myArray); //fixed;

//note that you can chain these functions together in a single statement:
~myArray = ~fixFreqs.value(a.value(1,10));

//you could also build this "demodulating" functionality into the original function
//not necessarily better, just a different style/design choice
(
~combinedFunc = {
        arg x=10, y=100;
        var ar, val;
        ar = []; //variable ar initially an empty array
        {x < y}.while ({
                val = x * 8;
                if(val>12000, {val = val/2});
                if(val<20, {val = val * 10});
                ar = ar.add(val); //add the computed result to the array
                x = x + 1; //increment
        });
        ar; //when x ≥ y, return the result
};
)

~combinedFunc.value(1,10);
~combinedFunc.value(2000,2020);
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Function into Array (beginner)

Marigold Maripol
Just to clarify here:
You create the primary array in variable A and a processing function
in Variable B.
But then:
~myArray = a.value(1,10); //create an array with values too low
~myArray = ~fixFreqs.value(~myArray); //fixed;

Is ~fixFreq supposed to = b.value?





On Wed, Jan 11, 2017 at 11:47 AM, Eli Fieldsteel
<[hidden email]> wrote:

> Rather than a conditional loop (while, for, etc), I would prefer to use a
> method that iterates automatically (do, collect, select, reject, etc),
> especially if you're always going to be incrementing the index by 1 on each
> pass.
>
> (
> //original function
> a = {
>         arg x=10, y=100;
>         var ar;
>         ar = []; //variable ar initially an empty array
>         {x < y}.while ({
>                 ar = ar.add(x * 8); //add the computed result to the array
>                 x = x + 1; //increment
>         });
>         ar; //when x ≥ y, return the result
> };
>
> //frequency "demodulating" function
> b = {
>         arg myArray = [];
>         myArray.collect({
>                 arg val;
>                 case
>                 {val>12000}{val/2} //if too high
>                 {val<20}{val*10} //if too low
>                 {true}{val}; //else, do nothing
>         });
> };
> )
>
> ~myArray = a.value; //default values
>
> ~myArray = a.value(1,10); //create an array with values too low
> ~myArray = ~fixFreqs.value(~myArray); //fixed;
>
> ~myArray = a.value(2000,2020); //array with values too high
> ~myArray = ~fixFreqs.value(~myArray); //fixed;
>
> //note that you can chain these functions together in a single statement:
> ~myArray = ~fixFreqs.value(a.value(1,10));
>
> //you could also build this "demodulating" functionality into the original
> function
> //not necessarily better, just a different style/design choice
> (
> ~combinedFunc = {
>         arg x=10, y=100;
>         var ar, val;
>         ar = []; //variable ar initially an empty array
>         {x < y}.while ({
>                 val = x * 8;
>                 if(val>12000, {val = val/2});
>                 if(val<20, {val = val * 10});
>                 ar = ar.add(val); //add the computed result to the array
>                 x = x + 1; //increment
>         });
>         ar; //when x ≥ y, return the result
> };
> )
>
> ~combinedFunc.value(1,10);
> ~combinedFunc.value(2000,2020);
>
>
>
>
> --
> View this message in context: http://new-supercollider-mailing-lists-forums-use-these.2681727.n2.nabble.com/Function-into-Array-beginner-tp7630058p7630074.html
> Sent from the SuperCollider Users New (Use this!!!!) mailing list archive at Nabble.com.
>
> _______________________________________________
> sc-users mailing list
>
> info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
> archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
> search: http://www.listarc.bham.ac.uk/lists/sc-users/search/

_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Function into Array (beginner)

Eli Fieldsteel
Whoa, good catch. My bad. I named your second function ~fixFreqs to give it a more meaningful name, but then decided to be consistent with your original approach of using single alphabetic characters...but then forgot to do this to all instances of ~fixFreqs!

So, yes. ~fixFreqs.value should be b.value.

Eli
Loading...