Iterate manually inside TraversableOnce

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Iterate manually inside TraversableOnce

Fernando Pelliccioni
Hello!

I am new to the forum and to Scala. 
Excuse me in advance if I ask an obvious question.

I was looking at the following code: 


  def reduceLeft[B >: A](op: (B, A) => B): B = {
    if (isEmpty)
      throw new UnsupportedOperationException("empty.reduceLeft")

    var first = true
    var acc: B = 0.asInstanceOf[B]

    for (x <- self) {
      if (first) {
        acc = x
        first = false
      }
      else acc = op(acc, x)
    }
    acc
  }

And I was wondering if it is possible to iterate manually, using next() and hasNext (instead of using for( <-)), in the context of TraversableOnce.

Thanks and regards,
Fernando Pelliccioni.

--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Iterate manually inside TraversableOnce

Sébastien Doeraene
Hello,

Looks like a question for scala-user (https://groups.google.com/forum/#!forum/scala-user) instead.

Cheers,
Sébastien

On Tue, Dec 13, 2016 at 3:05 PM, Fernando Pelliccioni <[hidden email]> wrote:
Hello!

I am new to the forum and to Scala. 
Excuse me in advance if I ask an obvious question.

I was looking at the following code: 


  def reduceLeft[B >: A](op: (B, A) => B): B = {
    if (isEmpty)
      throw new UnsupportedOperationException("empty.reduceLeft")

    var first = true
    var acc: B = 0.asInstanceOf[B]

    for (x <- self) {
      if (first) {
        acc = x
        first = false
      }
      else acc = op(acc, x)
    }
    acc
  }

And I was wondering if it is possible to iterate manually, using next() and hasNext (instead of using for( <-)), in the context of TraversableOnce.

Thanks and regards,
Fernando Pelliccioni.

--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Iterate manually inside TraversableOnce

Fernando Pelliccioni
Oh Sorry!
Thanks!

On Tue, Dec 13, 2016 at 11:12 AM, Sébastien Doeraene <[hidden email]> wrote:
Hello,

Looks like a question for scala-user (https://groups.google.com/forum/#!forum/scala-user) instead.

Cheers,
Sébastien

On Tue, Dec 13, 2016 at 3:05 PM, Fernando Pelliccioni <[hidden email]> wrote:
Hello!

I am new to the forum and to Scala. 
Excuse me in advance if I ask an obvious question.

I was looking at the following code: 


  def reduceLeft[B >: A](op: (B, A) => B): B = {
    if (isEmpty)
      throw new UnsupportedOperationException("empty.reduceLeft")

    var first = true
    var acc: B = 0.asInstanceOf[B]

    for (x <- self) {
      if (first) {
        acc = x
        first = false
      }
      else acc = op(acc, x)
    }
    acc
  }

And I was wondering if it is possible to iterate manually, using next() and hasNext (instead of using for( <-)), in the context of TraversableOnce.

Thanks and regards,
Fernando Pelliccioni.

--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.