Created
August 25, 2023 18:01
-
-
Save hughdbrown/e57321964d779cc9a0f6cb9f41c1f6da to your computer and use it in GitHub Desktop.
Group lines of text into paragraphs
This file contains hidden or 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
use itertools::Itertools; | |
pub fn split_paragraphs(lines: &Vec<&str>) -> Vec<String> { | |
let mut paragraphs: Vec<String> = vec![]; | |
for (key, group) in &lines.into_iter().group_by(|x| !x.is_empty()) { | |
if key { | |
paragraphs.push(group.map(|s: &&str| *s).collect::<Vec<&str>>().join(" ")); | |
} | |
} | |
paragraphs | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn split_paragraphs_1() { | |
let lines: Vec<&str> = vec![ | |
"asdasd asd", "zxc zxc", "", | |
"qwe qwe", "ert ert", "ty ty", "", | |
"sdf sdf", "iopio", | |
]; | |
let paragraphs: Vec<String> = split_paragraphs(&lines); | |
assert_eq!(paragraphs.len(), 3); | |
assert_eq!(paragraphs[0], lines[..2].to_vec().join(" ")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment