我怎样才能拉平这个未来[T]结构? [英] How can I flatten this Future[T] structure?
问题描述
考虑下面的例子:
VAL处理程序:连接=新DatabaseConnectionHandler()
VAL结果:未来[未来[未来[选项[结果集]]]] = handler.connect
.MAP((参数)=>处理)
.MAP(连接=> connection.sendQuery(BEGIN TRANSACTION SERIALIZABLE))
.MAP(未来=> future.map(查询=> query.rows))
.MAP(未来= GT; handler.sendQuery(COMMIT)的地图(查询=方式>将来))
是否有可能将其压平接收未来的[选项[结果集]
结尾,而不是这个将来未来几年内未来结构内部在Scala中<? / p>
我目前使用Scala的2.10 Future的,并承诺的,但我不能找到一个方法来这个。我知道我可以使用嵌套的回调,但我宁愿避免因为code将会惨不忍睹。
的连接
特征定义<一个href=\"https://github.com/mauricio/postgresql-netty/blob/master/src/main/scala/com/github/mauricio/postgresql/Connection.scala\">here.
当你地图
与类型的参数 A =&GT;未来[B]
你真的应该使用 flatMap
。
然后code会是这样的:
VAL连接:连接=新DatabaseConnectionHandler(DefaultConfiguration)
VAL结果:未来[QueryResult中] = connection.connect
.flatMap(_ =&GT; connection.sendQuery(BEGIN TRANSACTION ISOLATION LEVEL重复读))
.flatMap(_ =&GT; connection.sendQuery(SELECT 0))
.flatMap(_ =&GT; connection.sendQuery(COMMIT)的地图(值=方式&gt;查询))
另外,你可以使用泡沫prehension。 它使用 flatMap
你
VAL连接:连接=新DatabaseConnectionHandler(DefaultConfiguration)
VAL结果:未来[QueryResult中对于= {
_&LT; - connection.connect
_&LT; - connection.sendQuery(BEGIN TRANSACTION ISOLATION LEVEL重复读)
_&LT; - connection.sendQuery(SELECT 0)
QueryResult中&LT; - connection.sendQuery(提交)图(价值=&GT;查询)。
} {收益} QueryResult中
Given the following example:
val handler : Connection = new DatabaseConnectionHandler()
val result : Future[Future[Future[Option[ResultSet]]]] = handler.connect
.map( (parameters) => handler )
.map( connection => connection.sendQuery("BEGIN TRANSACTION SERIALIZABLE") )
.map( future => future.map( query => query.rows ) )
.map( future => handler.sendQuery("COMMIT").map( query => future ) )
Is it possible to flatten it to receive a Future[Option[ResultSet]]
at the end instead of this future inside a future inside a future structure in Scala?
I am currently using Scala's 2.10 Future's and Promise's, but I can't find a way to to this. I know I can use nested callbacks but I would rather avoid that since the code is going to look horrible.
The Connection
trait is defined here.
Whenever you map
with an argument of type A => Future[B]
you should really be using flatMap
.
The code would then be like this:
val connection : Connection = new DatabaseConnectionHandler( DefaultConfiguration )
val result: Future[QueryResult] = connection.connect
.flatMap( _ => connection.sendQuery("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ") )
.flatMap( _ => connection.sendQuery("SELECT 0") )
.flatMap( _ => connection.sendQuery("COMMIT").map( value => query ) )
Alternatively, you could use for-comprehension. It uses flatMap
for you.
val connection : Connection = new DatabaseConnectionHandler( DefaultConfiguration )
val result: Future[QueryResult] = for {
_ <- connection.connect
_ <- connection.sendQuery("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ")
_ <- connection.sendQuery("SELECT 0")
queryResult <- connection.sendQuery("COMMIT").map( value => query )
} yield { queryResult }
这篇关于我怎样才能拉平这个未来[T]结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!