Is it possible to have this configurable change in TraversableOnce.reduceLeft

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

Is it possible to have this configurable change in TraversableOnce.reduceLeft

Raster

Am a newbie in scala. Please ignore if this does not make sense. Please refer to this code. If there is any exception thrown in line - "acc = op(acc, x)" due to any bad data this will break the whole for loop. Is it possible to have some configurable exception suppresser in this code that just notes the exception when needed and uses some other replacement data for such bad data. (This code is from : TraversableOnce.reduceLeft)


 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
  }

Thanks.

--
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: Is it possible to have this configurable change in TraversableOnce.reduceLeft

Viktor Klang
Hi Raster,

You can wrap 'op' in a function which calls op and return whatever B you want if some exception occurs.

--
Cheers,

On Jan 19, 2017 5:29 PM, "Raster" <[hidden email]> wrote:

Am a newbie in scala. Please ignore if this does not make sense. Please refer to this code. If there is any exception thrown in line - "acc = op(acc, x)" due to any bad data this will break the whole for loop. Is it possible to have some configurable exception suppresser in this code that just notes the exception when needed and uses some other replacement data for such bad data. (This code is from : TraversableOnce.reduceLeft)


 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
  }

Thanks.

--
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: Is it possible to have this configurable change in TraversableOnce.reduceLeft

Vlad Patryshev
In reply to this post by Raster
The short answer is: NO.
What you can do is use some monad (e.g. Try or Either), and fold your collection so that you can deal properly with bad cases - it's up to you what you choose, maybe you collect all the exceptions in a List.

And it's a good question, by the way. In my class Result, I never thought about folding, but yes, it's all pretty legit. 

Thanks,
-Vlad

On Thu, Jan 19, 2017 at 5:24 AM, Raster <[hidden email]> wrote:

Am a newbie in scala. Please ignore if this does not make sense. Please refer to this code. If there is any exception thrown in line - "acc = op(acc, x)" due to any bad data this will break the whole for loop. Is it possible to have some configurable exception suppresser in this code that just notes the exception when needed and uses some other replacement data for such bad data. (This code is from : TraversableOnce.reduceLeft)


 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
  }

Thanks.

--
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.