Scala Future with predicate in for comprehension

    0

    0

    Jose Romero

    comprehensionpredicatefutureβ€’β€’β€’

    Idiomatic solution to have filter, conditionals, predicates in Futures' for-comprehension

    Example of usage:

    val f2 = for {
      i <- f1
      _ <- predicate( i == 2 )(new Exception("Test2"))
      j <- f3  // f3 will only run if the predicate is true
    } yield "Test1"
    
    import scala.concurrent.Future
    
    def predicate(condition: Boolean)(fail: Exception): Future[Unit] = 
        if (condition) Future( () ) else Future.failed(fail)
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.