Filter: Difference between revisions

Content added Content deleted
Line 1,134: Line 1,134:
Idiomatic approach in Dy is to use non-strict iterators (which can be combined without intermedate data structures) and translate the result to an array if needed:
Idiomatic approach in Dy is to use non-strict iterators (which can be combined without intermedate data structures) and translate the result to an array if needed:


<lang Dyalect>func Iterator.Filter(pred) {
<lang Dyalect>var xs = [1..20]
var arr = xs.Iterate().Filter(x => x % 2 == 0).Map(x => x.ToString())
for x in this when pred(x) {
yield x
}
}

func Iterator.Select(proj) {
for x in this {
yield proj(x)
}
}

var xs = [1..20]
var arr = xs.Iterate().Filter(x => x % 2 == 0).Select(x => x.ToString())
print(arr.ToArray())</lang>
print(arr.ToArray())</lang>