[sc-users] Programming question

William Brent william.brent at gmail.com
Tue Feb 19 09:43:22 PST 2008


Aha.  Thanks for the response.  I'll have to keep this in mind.

But just for my understanding then - the function specified by SystemClock
won't actually do n.poll until it's time to run the function?  I thought it
might store all the data immediately (and hence poll) when the .sched is
called rather than when the function is executed.

I've got another simple programming question too, but it's off topic, so
I'll start a new post...



On Feb 19, 2008 6:07 AM, James Harkins <jamshark70 at gmail.com> wrote:

> No, it's not the same. When 'n' changes, the behavior of your scheduled
> function will also change. If you want the scheduled function to track only
> the a[n] that was created during the specific iteration of the loop, there
> must be a local variable inside the loop.
> Global or larger scope variables are generally considered a less than
> ideal programming practice, for essentially the same reason. Any part of the
> code can touch a global variable. If one part of your program changes a
> global variable inappropriately, and another part is not expecting that
> change, it will introduce very strange bugs that are hard to troubleshoot.
> One of the benefits of "structured programming" -
> http://en.wikipedia.org/wiki/Structured_programming - is that it gives you
> syntactically clean ways to avoid overusing global variables.
>
> I think it's one of the trickier things in learning programming. When a
> program is fairly simple, you can get by without controlling variable scope
> and you might think it's just an "annoyance," as you put it. Then, when your
> requirements get more complicated, they are more difficult to deal with.
>
> But in the end, it's all about what's practical for you. You might never
> need to concern yourself much with variable scope, just as many users never
> write a class. Just pointing out that this is there to solve a real problem.
>
> hjh
>
>
> On Feb 19, 2008, at 8:41 AM, William Brent wrote:
>
> I'm glad this came up.  I've been turning larger scope variables into
> local copies inside of functions, and now I'm starting to be annoyed by the
> extra variable declarations.  Now I'm using a different approach, and have
> been meaning to post about whether or not it's a good idea.  I guess it
> doesn't matter, but lately I've just started using .poll to latch onto a
> value for scheduling if it might be changing:
>
> (
>     var a, n=0;
>     a = Array.newClear(100);
>
>     Routine({
>         loop({
>             a.wrapPut(n, anObject.new);      // add somekind of new object
> to the array (probably a synth)
>             SystemClock.sched(5, { a[n.poll].dosometask )});     //
> schedule the clock to do some task on that object
>             1.0.wait;
>             n = (n+1)%100;  // increase the array's index
>         })
>     }).play;
> )
>
>
> Is that functionally equivalent?  I think I like Dan's solution better for
> this particular case, though.  The scope of |n| makes a lot of sense.
>
>
> William
>
>
>
>
>
> On Feb 18, 2008 1:11 AM, Eduard Resina Bertran <eduard_resina at hotmail.com>
> wrote:
>
> > Works great. Thanks Dan!
> >
> >
> >
> > ------------------------------
> > > Date: Mon, 18 Feb 2008 08:07:48 +0000
> > > From: danstowell at gmail.com
> > > To: sc-users at create.ucsb.edu
> > > Subject: Re: [sc-users] Programming question
> >
> > >
> > > Hi -
> > >
> > > There are various ways to approach this but here's one way I'd do it:
> > >
> > > (
> > > var a;
> > > a = Array.newClear(100);
> > >
> > > Routine({
> > > loop({
> > > 100.do{|n|
> > > a.wrapPut(n, anObject.new);
> > > SystemClock.sched(5, { a[n].dosometask )});
> > > 1.0.wait;
> > > }
> > > })
> > > }).play;
> > > )
> > >
> > > In this approach, since "n" is not a global variable, you never need
> > > to worry about it getting modified outside of your function. I've used
> > > "100.do" but you might even prefer "a.size.do" for more generality.
> > >
> > > Dan
> > >
> > >
> > > 2008/2/18, Eduard Resina Bertran <eduard_resina at hotmail.com>:
> > > >
> > > > Hi all,
> > > >
> > > > this is some kind of programming generic issue.
> > > > In the following exemple, since the sched time for the clock is
> > larger than
> > > > the routine wait time, by the time the clock tries to do the
> > scheduled task
> > > > the array's index has increased and it does not apply to the
> > originally
> > > > intended object.
> > > >
> > > > Which could be a way around this, independently of the type of
> > object and
> > > > task desired?
> > > > Thanks.
> > > >
> > > >
> > > > (
> > > > var a, n=0;
> > > > a = Array.newClear(100);
> > > >
> > > > Routine({
> > > > loop({
> > > > a.wrapPut(n, anObject.new); // add somekind of new object
> > > > to the array (probably a synth)
> > > > SystemClock.sched(5, { a[n].dosometask )}); // schedule the
> > > > clock to do some task on that object
> > > > 1.0.wait;
> > > > n = (n+1)%100; // increase the array's index
> > > > })
> > > > }).play;
> > > > )
> > > >
> > > >
> > > >
> > > > ________________________________
> > > > Sigue los principales acontecimientos deportivos en directo. MSN
> > Motor
> > > > _______________________________________________
> > > > sc-users mailing list
> > > > sc-users at create.ucsb.edu
> > > > http://lists.create.ucsb.edu/mailman/listinfo/sc-users
> > > >
> > > >
> > >
> > >
> > > --
> > > http://www.mcld.co.uk
> > > _______________________________________________
> > > sc-users mailing list
> > > sc-users at create.ucsb.edu
> > > http://lists.create.ucsb.edu/mailman/listinfo/sc-users
> >
> > ------------------------------
> > Tecnología, moda, motor, viajes,.suscríbete a nuestros boletines para
> > estar siempre a la última MSN Newsletters<http://newsletters.msn.com/hm/maintenanceeses.asp?L=ES&C=ES&P=WCMaintenance&Brand=WL&RU=http%3a%2f%2fmail.live.com>
> >
> > _______________________________________________
> > sc-users mailing list
> > sc-users at create.ucsb.edu
> > http://lists.create.ucsb.edu/mailman/listinfo/sc-users
> >
> >
>
>
> --
> William Brent
>
> "Great minds flock together"
> Conflations: conversational idiom for the 21st century
>
> www.conflations.com_______________________________________________
> sc-users mailing list
> sc-users at create.ucsb.edu
> http://lists.create.ucsb.edu/mailman/listinfo/sc-users
>
>
>
> : H. James Harkins
>
> : jamshark70 at dewdrop-world.net
>
> : http://www.dewdrop-world.net
>
> .::!:.:.......:.::........:..!.::.::...:..:...:.:.:.:..:
>
>
> "Come said the Muse,
>
> Sing me a song no poet has yet chanted,
>
> Sing me the universal."  -- Whitman
>
>
> _______________________________________________
> sc-users mailing list
> sc-users at create.ucsb.edu
> http://lists.create.ucsb.edu/mailman/listinfo/sc-users
>
>


-- 
William Brent

"Great minds flock together"
Conflations: conversational idiom for the 21st century

www.conflations.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.create.ucsb.edu/pipermail/sc-users/attachments/20080219/e8374f40/attachment-0001.html 


More information about the sc-users mailing list