Simplify parse function using flat_map

This commit is contained in:
Léo Gaspard 2018-03-16 02:42:56 +01:00
parent d473806fc4
commit 0c1b07c30c
No known key found for this signature in database
GPG key ID: 8A55848B6090F9CF

View file

@ -1,14 +1,11 @@
pub fn parse(text: &str) -> Option<Vec<Instruction>> {
let instructions: Vec<Instruction> = text.lines()
.map(|s| match parse_line(s) {
Some(instructions) => instructions,
None => vec![],
.flat_map(|s| match parse_line(s) {
Some(instructions) => instructions.into_iter(),
None => Vec::new().into_iter(),
})
.fold(vec![], |mut collector, mut inst| {
collector.append(&mut inst);
collector
});
.collect();
if instructions.len() == 0 {
return None;