« Return to Thread: idea: val x, y {... x = ... y = ...}

Re: idea: val x, y {... x = ... y = ...}

by Mark Harrah :: Rate this Message:

Reply to Author | View in Thread

Maybe I'm missing the point, but this looks equivalent and cleaner:

def complete(context: Context): Boolean = {
  val elapsedSecs = (System.currentTimeMillis - startMillis/1000).toInt
  if (elapsedSecs > flightSecs)
    true
  else {
    completeImpl(context, elapsedSecs)
    false
  }
}
private def completeImpl(context: Context, elapsedSecs: Int) {
  val progress = elapsedSecs.toDouble/flightSecs
  val d = range*progress
  val x = frX + (d*kX).toInt
  val y = frY + (d*kY).toInt
  val z = (4*range*(progress - progress*progress)).toInt
  context.driver.look(separation, x, y, z)
}


-Mark

On Thursday 21 May 2009, Rob Dickens wrote:

> Okay, here's something more realistic - a method that gets called
> periodically, in this case to make something 'look' at the next point in
> space:
>   // scala version
>   def complete(context: Context): Boolean = {
>     val (x, y, z) = {
>       val progress = {
>         val elapsedSecs = (System.currentTimeMillis -
> startMillis/1000).toInt
>         if (elapsedSecs > flightSecs) return true
>         elapsedSecs.toDouble/flightSecs
>       }
>       val (_x, _y) = {
>         val d = range*progress
>         (frX + (d*kX).toInt, frY + (d*kY).toInt)
>       }
>       val _z = (4*range*(progress - progress*progress)).toInt
>       (_x, _y, _z)
>     }
>     context.driver.look(separation, x, y, z)
>     false
>   }
>
>   // scalax version
>   def complete(context: Context): Boolean = {
>     val x, y, z {
>       val progress = {
>         val elapsedSecs = (System.currentTimeMillis -
> startMillis/1000).toInt
>         if (elapsedSecs > flightSecs) return true
>         elapsedSecs.toDouble/flightSecs
>       } {
>         val d = range*progress
>         x = frX + (d*kX).toInt
>         y = frY + (d*kY).toInt
>       }
>       z = (4*range*(progress - progress*progress)).toInt
>     }
>     context.driver.look(separation, x, y, z)
>     false
>   }
>
> 2009/5/21 Ricky Clarkson <ricky.clarkson@...>
>
> > point {
> >   val a = 1 + 2
> >  val b = 3 + 4
> >  (a + 1, a + b, a - b)
> > }
> >
> > I know it looks like I'm just being picky, but I'm encouraging you to
> > give a use case.  Perhaps this is one:
> >
> > val (x, y, z) = {
> >  val a = 1 + 2
> >   val _x = a + 5
> >  val _y = _x + 3
> >  val _z = _y * 4
> >  (_x, _y, _z)
> > }
> >
> > 2009/5/21 Rob Dickens <arctic.bob@...>:
> > > So maybe I should have written,
> > > def scalaVersion() {
> > >   val (x, y, z) = {
> > >     val a = 1 + 2
> > >     val _x = a + 1
> > >     val b = 3 + 4
> > >     val _y = a + b
> > >     val _z = a - b
> > >     (_x, _y, _z)
> > >   }
> > >   point(x, y, z)
> > > }
> > > 2009/5/21 Ricky Clarkson <ricky.clarkson@...>
> > >
> > >> val (x, y, z) = {
> > >>  val a = 1 + 2
> > >>  val b = 3 + 4
> > >>  (a + 1, a + b, a - b)
> > >> }
> > >>
> > >> 2009/5/21 Rob Dickens <arctic.bob@...>:
> > >> > Dear Martin and All,
> > >> > One good thing Java still has going for it IMO is its ability,
> > >> > thanks
> >
> > to
> >
> > >> > its
> > >> > 'blank finals', always to let you confine the scope of local
> > >> > variables to
> > >> > where they're used - thus resulting in more maintainable code.
> > >> > For example,
> > >> > void javaVersion() {
> > >> >   final int x, y, z; {
> > >> >     final int a = 1 + 2;
> > >> >     x = a + 1;
> > >> >     final int b = 3 + 4;
> > >> >     y = a + b;
> > >> >     z = a - b;
> > >> >   }
> > >> >   point(x, y, z);
> > >> > }
> > >> > To my knowledge, this is currently best translated as,
> > >> > def scalaVersion() {
> > >> >   val (x, y, z) = {
> > >> >     val a = 1 + 2
> > >> >     val _x = a + 1
> > >> >     val b = 3 + 4
> > >> >     (_x, a + b, a - b)
> > >> >   }
> > >> >   point(x, y, z)
> > >> > }
> > >> > Note the requirement for the shadow val, _x.
> > >> > Assuming it were feasible to support, and imagining cases with more
> > >> > complex
> > >> > expressions, wouldn't the following syntax be more agreeable?
> > >> > def scalaxVersion() {
> > >> >   val x, y, z {
> > >> >     val a = 1 + 2
> > >> >     x = a + 1
> > >> >     val b = 3 + 4
> > >> >     y = a + b
> > >> >     z = a - b
> > >> >   }
> > >> >   point(x, y, z)
> > >> > }
> > >> > --
> > >> > Rob, Lafros.com


 « Return to Thread: idea: val x, y {... x = ... y = ...}