def withUnsafeFlatMap()

in src/main/scala/com/spotify/bdrc/scala/FilterMessyData.scala [53:72]


  def withUnsafeFlatMap(input: Seq[MessyData]): Seq[String] =
    input
      .flatMap(x => Try(compute(x)).toOption)

  /**
   * Smart approach that throws any failed records away.
   *
   * Try/catch block returns a Seq of one item if compute succeeds and Nil if it fails.
   * This approach is safer since you have control over what exceptions to expect.
   */
  def withSafeFlatMap(input: Seq[MessyData]): Seq[String] = {
    input
      .flatMap { x =>
        try {
          Seq(compute(x))
        } catch {
          case _: NullPointerException => Nil
        }
      }
  }