def retry(noTimes: Int)(block: ⇒Future[T]): Future[T] = { val ns: Iterator[Int] = (1 to noTimes).iterator val attempts: Iterator[Future[T]] = ns.map(_⇒ ()⇒block) val failed = Future.failed(new Exception) attempts.foldLeft(failed) ((a,block) ⇒ a recoverWith { block() }) } retry(3) { block } = unfolds to ((failed recoverWith block1) recoverWith block2) recoverWith block3