Iterators and closures
Focus: one iterator pipeline that filters, transforms, limits, and collects a score list.
iterators.slx
entry top_scores(scores: u64[], min_score: u64, limit: u32) -> u64[] {
return scores
.iter()
.filter(|score: u64| { return score >= min_score })
.map(|score: u64| { return score * 10 })
.take(limit)
.collect()
}How it works
scores.iter()creates the lazy iterator pipeline.filterkeeps only values greater than or equal tomin_score.mapapplies a multiplier to each selected score.take(limit)bounds the amount of work beforecollect()materializes the array.collect()turns the lazy pipeline back into au64[]return value.
Last updated on