Skip to content

Instantly share code, notes, and snippets.

@335g
Created November 25, 2017 02:38
Show Gist options
  • Save 335g/878572aaf14cbc362d99813611fc2380 to your computer and use it in GitHub Desktop.
Save 335g/878572aaf14cbc362d99813611fc2380 to your computer and use it in GitHub Desktop.
memo
let numbers = ["1", "2", "3", "a", "5"];
let only_numbers = numbers.iter()
.scan((), |_, &item| item.parse::<i32>().ok())
.collect::<Vec<i32>>(); // [1, 2, 3]
let numbers = ["1", "2", "3", "a", "5"]
let onlyNumbers = numbers.flatMap{ Int($0) } // [1, 2, 3, 5]
var failed = false
let onlyNumbers2 = numbers.reduce(into: [Int]()){ res, item in
if !failed, let num = Int(item) {
res.append(num)
} else {
failed = true
}
} // [1, 2, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment