1

Access the previous exit when the operator's supervisor in Scala

 2 years ago
source link: https://www.codesd.com/item/access-the-previous-exit-when-the-operator-s-supervisor-in-scala.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Access the previous exit when the operator's supervisor in Scala

advertisements

How to access the resulting output value to perform an upcoming operation for example:

scala> List(1,4,3,4,4,5,6,7)
res0: List[Int] = List(1, 4, 3, 4, 4, 5, 6, 7)

scala> res0.removeDuplicates.slice(0, ???.size -2)

In the above line, i need to perform slice operation after removing duplicates. To do this, how to access output of .removeDuplicate(), so that i can use it to find size for slice operation.

I need to perform this in a single step. Not in multiple steps like:

scala> res0.removeDuplicates
res1: List[Int] = List(1, 4, 3, 5, 6, 7)

scala> res1.slice(0, res1.size -2)
res2: List[Int] = List(1, 4, 3, 5)

I want to access intermediate results in the final operation. removeDuplicates() is just an example.

list.op1().op2().op3().finalop() here i want to access: output of op1,op2,op3 in finalop


Wrapping into into an Option may be one option (no pun intended):

val finalResult = Some(foo).map { foo =>
  foo.op1(foo.stuff)
}.map { foo =>
  foo.op2(foo.stuff)
}.map { foo =>
  foo.op3(foo.stuff)
}.get.finalOp

You can make the wrapping part implicit to make it a little nicer:

object Tapper {
  implicit class Tapped[T] extends AnyVal(val v: T) {
     def tap[R](f: T => R) = f(v)
  }
}

import Tapper._
val finalResult = foo
 .tap(f => f.op1(f.stuff))
 .tap(f => f.op2(f.stuff))
 .tap(f => f.finalOp(f.stuff))


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK