override def simplify()

in src/main/scala/com/twitter/stitch/Stitch.scala [2048:2082]


    override def simplify(pending: Pending): Stitch[R] = {
      if (forwarded ne null) return forwarded

      var satisfied = true
      var i = 0
      while (i < ss.length) {
        ss(i) = ss(i).simplify(pending)
        ss(i) match {
          // this won't short-circuit immediately if any of the remaining stitches are Const(Throw(_))
          // but it will eventually return when it gets to the failed stitch and if we can't move forward
          // based on the current stitches.
          case t @ Const(Throw(_)) =>
            return forward(t.asInstanceOf[Stitch[R]])
          case Const(Return(v)) => buf(i) = Some(v)
          case _ => satisfied = false
        }

        // invoke k and return if we can move forward or run into exceptions
        try {
          k(buf) match {
            case Some(r) => return forward(Stitch.value(r))
            case scala.None =>
          }
        } catch {
          case NonFatal(e) => return forward(Stitch.exception[R](e))
        }
        i += 1
      }
      if (satisfied) {
        // if all stitches are satisfied and the function returns None, we return StitchInvalidState
        forward(Stitch.exception[R](new StitchInvalidState))
      } else {
        this
      }
    }