[rust] Use collect for traversing!
Thanks to https://floss.social/@janriemer/111511019839280544
This commit is contained in:
parent
a2637587e9
commit
8dad919572
|
@ -95,9 +95,8 @@ impl FromStr for Revelation {
|
||||||
type Err = String;
|
type Err = String;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
let revelation_results: Vec<_> = s.split(", ").map(ColourRevelation::from_str).collect();
|
let revelation_result: Result<Vec<_>, _> = s.split(", ").map(ColourRevelation::from_str).collect();
|
||||||
let revelations_result = traverse_results(revelation_results);
|
revelation_result.and_then(|revelations| {
|
||||||
revelations_result.and_then(|revelations| {
|
|
||||||
let reds = extract_colour(revelations.clone(), String::from("red"));
|
let reds = extract_colour(revelations.clone(), String::from("red"));
|
||||||
let greens = extract_colour(revelations.clone(), String::from("green"));
|
let greens = extract_colour(revelations.clone(), String::from("green"));
|
||||||
let blues = extract_colour(revelations, String::from("blue"));
|
let blues = extract_colour(revelations, String::from("blue"));
|
||||||
|
@ -135,10 +134,10 @@ impl FromStr for Game {
|
||||||
.replace("Game ", "")
|
.replace("Game ", "")
|
||||||
.parse()
|
.parse()
|
||||||
.map_err(|e| format!("Invalid game id: {}", e));
|
.map_err(|e| format!("Invalid game id: {}", e));
|
||||||
let revelations_results: Vec<Result<Revelation, _>> =
|
let revelations_result: Result<Vec<Revelation>, _> =
|
||||||
revelations_str.split("; ").map(|x| x.parse()).collect();
|
revelations_str.split("; ").map(|x| x.parse()).collect();
|
||||||
game_id_result.and_then(|id| {
|
game_id_result.and_then(|id| {
|
||||||
traverse_results(revelations_results)
|
revelations_result
|
||||||
.and_then(|revelations| Ok(Game { id, revelations }))
|
.and_then(|revelations| Ok(Game { id, revelations }))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -148,23 +147,5 @@ impl FromStr for Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_games(s: &str) -> Result<Vec<Game>, String> {
|
fn parse_games(s: &str) -> Result<Vec<Game>, String> {
|
||||||
traverse_results(s.split('\n').map(Game::from_str).collect())
|
s.split('\n').map(Game::from_str).collect()
|
||||||
}
|
|
||||||
|
|
||||||
// Me: Mom, can we have travese.
|
|
||||||
// Mom: We got traverse at home.
|
|
||||||
// Traverse at home:
|
|
||||||
fn traverse_results<A, B>(results: Vec<Result<A, B>>) -> Result<Vec<A>, B>
|
|
||||||
where
|
|
||||||
Vec<A>: Clone,
|
|
||||||
{
|
|
||||||
results.into_iter().fold(Ok(Vec::new()), |acc, res| {
|
|
||||||
acc.and_then(|vec| {
|
|
||||||
res.and_then(|r| {
|
|
||||||
let mut vectemp = vec.clone();
|
|
||||||
vectemp.push(r);
|
|
||||||
Ok(vectemp)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue