Github user vanzin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/22911#discussion_r233541147
  
    --- Diff: 
resource-managers/kubernetes/core/src/main/scala/org/apache/spark/deploy/k8s/SparkPod.scala
 ---
    @@ -18,7 +18,30 @@ package org.apache.spark.deploy.k8s
     
     import io.fabric8.kubernetes.api.model.{Container, ContainerBuilder, Pod, 
PodBuilder}
     
    -private[spark] case class SparkPod(pod: Pod, container: Container)
    +private[spark] case class SparkPod(pod: Pod, container: Container) {
    +
    +  /**
    +   * Convenience method to apply a series of chained transformations to a 
pod.
    +   *
    +   * Use it like:
    +   *
    +   *     original.modify { case pod =>
    +   *       // update pod and return new one
    +   *     }.modify { case pod =>
    +   *       // more changes that create a new pod
    +   *     }.modify {
    +   *       case pod if someCondition => // new pod
    +   *     }
    +   *
    +   * This makes it cleaner to apply multiple transformations, avoiding 
having to create
    +   * a bunch of awkwardly-named local variables. Since the argument is a 
partial function,
    +   * it can do matching without needing to exhaust all the possibilities. 
If the function
    +   * is not applied, then the original pod will be kept.
    +   */
    +  def transform(fn: PartialFunction[SparkPod, SparkPod]): SparkPod = 
fn.lift(this).getOrElse(this)
    --- End diff --
    
    I added this because I started to get tired of code like the following:
    
    ```
    val someIntermediateName = someOption.map { blah =>
       // create the updated pod
    }.getOrElse(previousPodName)
    
    // lather, rinse, repeat
    ```
    
    To me that's hard to follow and brittle, and this pattern makes things 
clearer IMO.


---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to