Future composibility in Clojure.

5 views
Skip to first unread message

Ed Tretriluxana

unread,
Aug 8, 2018, 5:09:50 PM8/8/18
to clj-la
Hi there. I'm pretty new to Clojure. In other languages like Scala and Java, there's notion of composing futures together so I can process the future values as soon as they are realized, all without blocking myself. For example,

val f = Future { /* compute result, can fail */ }
f.onComplete {
  case Success(result) => …
  case Failure(ex) => …
}

or

val f1 = Future { /* compute r1, can fail */ }
val f2 = Future { /* compute r2, can fail */ }
val f3 = f1
  .flatMap { r1 => f2.map(r2 => (r1, r2)) } // if both are good, put both result into a tuple
  .recover { // if something bad (either one of them), substitute the result depending on failure
    case _: IOException => (0, 0)
    case _ => (-1, -1)
  }


What's the idiomatic way to do the same in Clojure - using just the core libraries? I don't see how I can access the value of a Clojure's future without being blocked or having to constantly polling the state.

Thanks,
Ed
Reply all
Reply to author
Forward
0 new messages