我无法理解如何在Future中使用flatMap。 '为'看起来很好。
object ConcurrencyExample extends App {
val gpf= Future {gpf operations}
val ccf = Future{ccf operations}
//how can I convert this for to flatMap?
val atbf = for {g <- gpf
c <- ccf } yield {atbf operations}
Await.result(atbf,1000 millis )
}
答案 0 :(得分:1)
可以去除flatMap
,filter
和for {
foo <- FooF
bar <- BarF(foo)
baz <- BazF if bar > 0
bow <- BowF(baz)
} yield (baz + 1)
。
这是你如何做到的。
FooF.flatMap { foo => //inner bindings become flatMap
BarF(foo).flatMap { bar =>
BazF.filter { baz => baz > 0 } //guards become filter
.flatMap { baz =>
BowF(baz)
}.map { baz => baz + 1 } // yield becomes map
}
}
以上内容可以转换为
gpf.flatMap { _ => ccf }.map { _ => atbf }
所以你的理解力变得
if(!empty($additional_features)){
foreach($additional_features as $additional_feature){
$data = [
'name' => $additional_feature,
];
if (!Feature::where('name', '=', $additional_feature)->exists()) {
$additional = Feature::firstOrCreate($data);
$additional_ids[] = $additional->id;
}
else{
return redirect()->back()->withFlashMessage($additional_feature . ' exists!');
}
}
}