Skip to content

Instantly share code, notes, and snippets.

class ListingProcessor(transform: RawListing => FullListing, 
                       save: Sink[Seq[FullListing]]) {

    def process(listings: Seq[RawListing]): Async[Unit] = {
      val fullListings = listings.map(transform) 
      // ...logic, etc
      save(fullListings)
    }
}
trait Sink[A] {
  def apply(a: A): Async[Unit]
}
val input: Seq[RawListing] = //... fixture
def transform(in: RawListing): FullListing = //... trivial
var savedResult: String = null
def save(json: String): Async[Unit] = Async {
  insertResultHere = json
}
val expectedResult = //... actually generate some sort of JSON

val processor = new ListingProcessor(transform, save)
class ListingProcessor(transform: RawListing => FullListing, 
                       save: String => Async[Unit]) {

    def process(listings: Seq[RawListing]): Async[Unit] = {
      val fullListings = listings.map(transform) 
      // ...logic, etc
      save(fullListings.toJson)
    }
}
val f: Int => Future[Person] = ???
val ids = List(1,2,3,4,5)
val persons: Future[List[Person]] = Future.traverse(ids, f) // parallel computation using Applicative
val persons2: List[Future[Person]] = ids map (id => f(id)) // sequential computation
persons2.sequence : Future[List[Person]] // now you can wait on one Future instead of many Futures