spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Composing Futures with For Comprehension


It looks like you're trying to compose Futures with Vector. For comprehensions in scala have to all be of the same higher type, which in your case is Future. When you unroll the 'sugar' of the for comprehension, it's just calling flatMap on everything.

for {
  categories <- categoriesFuture
  // I'm not sure what the return type is here,
but I'm guessing it's a future as well
  categoryIdsWithoutPerson <-
findCategoryIdsWithoutPerson(categories.map(_.id),
personId)
  // Here we use = to get around need to flatMap
  categoriesWithoutPerson =
categories.filter(category =>
categoryIdsWithoutPerson.contains(category.id))
} yield categoryIdsWithoutPerson

Your code de-sugared:

categoriesFuture.flatMap(categories
=> 
 
findCategoryIdsWithoutPerson(categories.map(_.id),
personId).
    flatMap(categoryIdsWithoutPerson => 
       categories.filter(category =>
categoryIdsWithoutPerson.contains(category.id)).
          map(_ => categoryIdsWithoutPerson))

Categories : Scala

Related to : Composing Futures with For Comprehension
Scala, Composing Function with two values
You're attempting to compose a Function2 (adder) with a Function1, hence the issue. One workaround is to change your definition of Adder to a curried version: def adder(a: Int)(b: Int):Int = a + b Then doubleAdd to partially apply adder like this: def doubleAdd(x: Int) = doubler _ compose adder(x) What's happening under the hood is transforming adder from a Function2 (Int, Int) => Int, to

Categories : Scala
Futures in Scala.js
There are 2 existing and working ExecutionContexts in scala.scalajs.concurrent.JSExecutionContext, with implicit versions in the inner object Implicits. Simply import the one that makes sense for you (probably queue, the other one not being actually asynchronous). import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue // now you get to play with Futures

Categories : Javascript
Keep track of completed Futures
I would suggest using the standard Java AtomicInteger. You can increment it using the incrementAndGet() method, and obtain the current value via its get() method. import java.util.concurrent.atomic.AtomicInteger ... val completed = new AtomicInteger() val futures = for(i <- 0 until nSteps) yield future { ... val content = blocking { ... http request ... } process(content) compl

Categories : Scala
Sequencing Scala Futures with bounded parallelism (without messing around with ExecutorContexts)
I have example how to do it with scalaz-stream. It's quite a lot of code because it's required to convert scala Future to scalaz Task (abstraction for deferred computation). However it's required to add it to project once. Another option is to use Task for defining 'doWork'. I personally prefer task for building async programs. import scala.concurrent.{Future => SFuture} import scala.util.

Categories : Scala
How does this list comprehension work?
Quoting the doc: The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned. The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned. Because of precedence rules, isinstance(g, str) and g.lower() or g is actually e

Categories : Python
Recently Add
Reverse list of n elements
scalac: Error: object CharRef in intelliJ 14
Scala - trouble with type inference in lambda expression
Exception on spark test
How can I emulate Haskell's typeclasses in Scala?
Slick: CRUD extension: How to encapsulate implicit mapping:BaseColumnType[T]
Can't find Traverse for sequencing Seq[ValidationNel[String, MyCaseClass]] => ValidationNel[String, Seq[MyCaseClass]]
Tail recursion: internal "loop" function or default values for accumulators
Scala - Add element:MyType to Array = option[MyType] expected
Scala, Composing Function with two values
Using dependent type to generate compile error
How to match all words in a sentence with scala combinators?
Parser Alternative Operator | Fails
ScalaTest assertion mismatch due to Physical Address
Scala implicit parameter and japanese smiley 'foldLeft'
Is it possible to user reduceByKey((x, y, z) => ...)?
How to implement security Authorization using scala and play?
SSO login using scala script
Sum elements based on a predicate
Keep track of completed Futures
API Observable with dynamic caching
java.io.IOException: Remotely closed in gatling
Scala permutations using two lists
Is Scala Either really a Monad
Spark: Use of distinct
Identifying two type wildcards as identical
how to package spark scala application
Pattern Match on Scala `class`
Type mismatch when using higher-kinded types
Scala List match last element
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.