From ca2f130995b2669fa7878be7922364077e6a113a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 9 Mar 2019 12:30:36 +0100 Subject: [PATCH] Rewrite: Diary::get_youngest_entry_id() Rewrite to collect not so often internally. This removes one collect() from the implementation. One is still there in the sorted_by() call, though. Signed-off-by: Matthias Beyer --- lib/domain/libimagdiary/src/diary.rs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/lib/domain/libimagdiary/src/diary.rs b/lib/domain/libimagdiary/src/diary.rs index d8f08352..8d58c5a1 100644 --- a/lib/domain/libimagdiary/src/diary.rs +++ b/lib/domain/libimagdiary/src/diary.rs @@ -99,24 +99,14 @@ impl Diary for Store { } /// get the id of the youngest entry - /// - /// TODO: We collect internally here. We shouldn't do that. Solution unclear. fn get_youngest_entry_id(&self, diary_name: &str) -> Option> { match Diary::entries(self, diary_name) { Err(e) => Some(Err(e)), Ok(entries) => { - let mut sorted_entries = vec![]; - - for entry in entries { - let entry = match entry { - Ok(e) => DiaryId::from_storeid(&e), - Err(e) => return Some(Err(e)), - }; - - sorted_entries.push(entry); - } - - sorted_entries.into_iter().sorted_by(|a, b| { + entries.map(|ent| { + ent.and_then(|id| DiaryId::from_storeid(&id)) + }) + .sorted_by(|a, b| { match (a, b) { (&Ok(ref a), &Ok(ref b)) => { let a : NaiveDateTime = a.clone().into();