Iterate manually inside TraversableOnce

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 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-user" 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

Oliver Ruebenacker

     Hello,

  TraversableOnce does not have methods next and hasNext. If this was an iterator, it would make perfect sense in this case to use these methods. If this was an Iterable, you could get an Iterator from it.

     Best, Oliver

On Tue, Dec 13, 2016 at 9:21 AM, 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-user" 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.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

--
You received this message because you are subscribed to the Google Groups "scala-user" 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
Thanks Oliver,

Then, how for(x <- y) works?

Thanks!
Fernando.

On Tue, Dec 13, 2016 at 4:20 PM, Oliver Ruebenacker <[hidden email]> wrote:

     Hello,

  TraversableOnce does not have methods next and hasNext. If this was an iterator, it would make perfect sense in this case to use these methods. If this was an Iterable, you could get an Iterator from it.

     Best, Oliver

On Tue, Dec 13, 2016 at 9:21 AM, 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-user" 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.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute


--
You received this message because you are subscribed to the Google Groups "scala-user" 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

Rex Kerr-2
for (x <- y) is shorthand for y.foreach(x => ).

So if you have for (x <- y) foo(x); it will be rewritten as y.foreach(x => foo(x)).  That means the closure x => foo(x) will be created and passed into y's foreach method, which can call it howevermany times it needs to using whichever internal process it desires.  In particular, there's no reason why it has to be next/hasNext.

  --Rex

On Tue, Dec 13, 2016 at 11:24 AM, Fernando Pelliccioni <[hidden email]> wrote:
Thanks Oliver,

Then, how for(x <- y) works?

Thanks!
Fernando.

On Tue, Dec 13, 2016 at 4:20 PM, Oliver Ruebenacker <[hidden email]> wrote:

     Hello,

  TraversableOnce does not have methods next and hasNext. If this was an iterator, it would make perfect sense in this case to use these methods. If this was an Iterable, you could get an Iterator from it.

     Best, Oliver

On Tue, Dec 13, 2016 at 9:21 AM, 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-user" 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.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute


--
You received this message because you are subscribed to the Google Groups "scala-user" 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-user" 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
Thanks Rex,

There are 2 things that I don't understand:


def foreach[U](f: A => U): Unit =
      for (x <- self)
        if (p(x)) f(x)

1. foreach function is defined in terms of for (x <- y) ... (¿?)
2. Where is p(x) defined?

Thanks and regards,
Fernando.



On Tue, Dec 13, 2016 at 4:33 PM, Rex Kerr <[hidden email]> wrote:
for (x <- y) is shorthand for y.foreach(x => ).

So if you have for (x <- y) foo(x); it will be rewritten as y.foreach(x => foo(x)).  That means the closure x => foo(x) will be created and passed into y's foreach method, which can call it howevermany times it needs to using whichever internal process it desires.  In particular, there's no reason why it has to be next/hasNext.

  --Rex

On Tue, Dec 13, 2016 at 11:24 AM, Fernando Pelliccioni <[hidden email]> wrote:
Thanks Oliver,

Then, how for(x <- y) works?

Thanks!
Fernando.

On Tue, Dec 13, 2016 at 4:20 PM, Oliver Ruebenacker <[hidden email]> wrote:

     Hello,

  TraversableOnce does not have methods next and hasNext. If this was an iterator, it would make perfect sense in this case to use these methods. If this was an Iterable, you could get an Iterator from it.

     Best, Oliver

On Tue, Dec 13, 2016 at 9:21 AM, 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-user" 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.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute


--
You received this message because you are subscribed to the Google Groups "scala-user" 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-user" 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

Rex Kerr-2
That's all within the WithFilter class, not the main body of TraversableLike.

https://github.com/scala/scala/blob/v2.12.0/src/library/scala/collection/TraversableLike.scala#L669

The TraversableLike foreach is abstract.  The `for` inside the `WithFilter` foreach desugars to use the TraversableLike foreach (which is filled in by the class that extends TraversableLike).

The p is the predicate that was filtered on.

  --Rex


On Tue, Dec 13, 2016 at 12:13 PM, Fernando Pelliccioni <[hidden email]> wrote:
Thanks Rex,

There are 2 things that I don't understand:


def foreach[U](f: A => U): Unit =
      for (x <- self)
        if (p(x)) f(x)

1. foreach function is defined in terms of for (x <- y) ... (¿?)
2. Where is p(x) defined?

Thanks and regards,
Fernando.



On Tue, Dec 13, 2016 at 4:33 PM, Rex Kerr <[hidden email]> wrote:
for (x <- y) is shorthand for y.foreach(x => ).

So if you have for (x <- y) foo(x); it will be rewritten as y.foreach(x => foo(x)).  That means the closure x => foo(x) will be created and passed into y's foreach method, which can call it howevermany times it needs to using whichever internal process it desires.  In particular, there's no reason why it has to be next/hasNext.

  --Rex

On Tue, Dec 13, 2016 at 11:24 AM, Fernando Pelliccioni <[hidden email]> wrote:
Thanks Oliver,

Then, how for(x <- y) works?

Thanks!
Fernando.

On Tue, Dec 13, 2016 at 4:20 PM, Oliver Ruebenacker <[hidden email]> wrote:

     Hello,

  TraversableOnce does not have methods next and hasNext. If this was an iterator, it would make perfect sense in this case to use these methods. If this was an Iterable, you could get an Iterator from it.

     Best, Oliver

On Tue, Dec 13, 2016 at 9:21 AM, 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-user" 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.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute


--
You received this message because you are subscribed to the Google Groups "scala-user" 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-user" 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 thanks, I didn't see the class declaration, the problem of checking code at github page ;)
Thanks!

On Tue, Dec 13, 2016 at 5:17 PM, Rex Kerr <[hidden email]> wrote:
That's all within the WithFilter class, not the main body of TraversableLike.

https://github.com/scala/scala/blob/v2.12.0/src/library/scala/collection/TraversableLike.scala#L669

The TraversableLike foreach is abstract.  The `for` inside the `WithFilter` foreach desugars to use the TraversableLike foreach (which is filled in by the class that extends TraversableLike).

The p is the predicate that was filtered on.

  --Rex


On Tue, Dec 13, 2016 at 12:13 PM, Fernando Pelliccioni <[hidden email]> wrote:
Thanks Rex,

There are 2 things that I don't understand:


def foreach[U](f: A => U): Unit =
      for (x <- self)
        if (p(x)) f(x)

1. foreach function is defined in terms of for (x <- y) ... (¿?)
2. Where is p(x) defined?

Thanks and regards,
Fernando.



On Tue, Dec 13, 2016 at 4:33 PM, Rex Kerr <[hidden email]> wrote:
for (x <- y) is shorthand for y.foreach(x => ).

So if you have for (x <- y) foo(x); it will be rewritten as y.foreach(x => foo(x)).  That means the closure x => foo(x) will be created and passed into y's foreach method, which can call it howevermany times it needs to using whichever internal process it desires.  In particular, there's no reason why it has to be next/hasNext.

  --Rex

On Tue, Dec 13, 2016 at 11:24 AM, Fernando Pelliccioni <[hidden email]> wrote:
Thanks Oliver,

Then, how for(x <- y) works?

Thanks!
Fernando.

On Tue, Dec 13, 2016 at 4:20 PM, Oliver Ruebenacker <[hidden email]> wrote:

     Hello,

  TraversableOnce does not have methods next and hasNext. If this was an iterator, it would make perfect sense in this case to use these methods. If this was an Iterable, you could get an Iterator from it.

     Best, Oliver

On Tue, Dec 13, 2016 at 9:21 AM, 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-user" 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.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute


--
You received this message because you are subscribed to the Google Groups "scala-user" 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-user" 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.