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