Last active
September 10, 2017 02:53
-
-
Save xevix/2c88b26032e7f4418aa0e9fcd2d398d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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