Skip to content

Instantly share code, notes, and snippets.

@xevix
Last active September 10, 2017 02:53
Show Gist options
  • Save xevix/2c88b26032e7f4418aa0e9fcd2d398d0 to your computer and use it in GitHub Desktop.
Save xevix/2c88b26032e7f4418aa0e9fcd2d398d0 to your computer and use it in GitHub Desktop.
pub fn combine_all_option<T>(xs: &Vec<T>) -> Option<T>
where
T: Semigroup + Clone,
{
match xs.first() {
Some(head) => {
// Dear lord this reads horribly
xs[1..].iter().fold(Some((*head).clone()), |acc, x| acc.combine(&Some((*x).clone())))
}
_ => None
}
// match xs.first() {
// Some(ref head) => {
// let tail = xs[1..].to_vec();
// // TODO figure out how to write this as a fold
// let mut x = (*head).clone();
// for i in tail {
// x = x.combine(&i)
// }
// Some(x)
// }
// _ => None,
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment