Skip to content

Commit 27de75d

Browse files
committed
refactor: apply functional style for NodeTable fns
* NodeTable::samples_as_vector * NodeTable::create_node_id_vector
1 parent 90642a9 commit 27de75d

File tree

1 file changed

+3
-11
lines changed

1 file changed

+3
-11
lines changed

src/node_table.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ impl NodeTable {
189189
/// flag.remove(tskit::NodeFlags::IS_SAMPLE);
190190
/// }
191191
/// assert!(!tables.nodes_mut().flags_array_mut().iter().any(|f| f.is_sample()));
192+
/// assert!(tables.nodes().samples_as_vector().is_empty());
192193
/// ```
193194
///
194195
/// ```
@@ -404,21 +405,12 @@ impl NodeTable {
404405
/// of all nodes for which [`crate::TSK_NODE_IS_SAMPLE`]
405406
/// is `true`.
406407
pub fn samples_as_vector(&self) -> Vec<NodeId> {
407-
let mut samples: Vec<NodeId> = vec![];
408-
for row in self.iter() {
409-
if row.flags.contains(NodeFlags::IS_SAMPLE) {
410-
samples.push(row.id);
411-
}
412-
}
413-
samples
408+
self.create_node_id_vector(|row| row.flags.contains(NodeFlags::IS_SAMPLE))
414409
}
415410

416411
/// Obtain a vector containing the indexes ("ids") of all nodes
417412
/// satisfying a certain criterion.
418-
pub fn create_node_id_vector(
419-
&self,
420-
mut f: impl FnMut(&crate::NodeTableRow) -> bool,
421-
) -> Vec<NodeId> {
413+
pub fn create_node_id_vector(&self, mut f: impl FnMut(&NodeTableRow) -> bool) -> Vec<NodeId> {
422414
self.iter()
423415
.filter(|row| f(row))
424416
.map(|row| row.id)

0 commit comments

Comments
 (0)