且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Dart中的收益示例

更新时间:2023-12-04 16:21:58


for(e<-a,如果e> 2)的表达式e 表达式是 表达式 在Scala中。
评估为数组。 Dart没有将其作为直接表达式,但是您可以使函数以几乎相同的方式返回可迭代对象,然后立即调用它们以创建表达式:

The for (e <- a if e > 2) yield e expression is an expression in Scala. It evaluates to an array. Dart does not have that as a direct expression, but you can make functions that return iterables in much the same way, and then call them immediately to create an expression:

var a = [1, 2, 3, 4, 5]
var res = () sync* { for (var v in a) if (v > 2) yield v; } ();

在这里,我介绍一个函数()sync * {for(var v in a)如果(v> 2)产生v; } 并立即调用它。该函数返回 Iterable 。可迭代的元素是主体 yield 的值,在这种情况下为3、4和5。

Here I introduce a function () sync* { for (var v in a) if (v > 2) yield v; } and calls it immediately. That function returns an Iterable. The elements of that iterable are the values yield'ed by the body, in this case 3, 4 and 5.