Skip to content

Commit f6d3a94

Browse files
authored
refactor: apply functional style for NodeTable fns (#377)
* NodeTable::samples_as_vector * NodeTable::create_node_id_vector
1 parent aaac26a commit f6d3a94

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

src/node_table.rs

Lines changed: 7 additions & 18 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,28 +405,16 @@ 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> {
422-
let mut samples: Vec<NodeId> = vec![];
423-
for row in self.iter() {
424-
if f(&row) {
425-
samples.push(row.id);
426-
}
427-
}
428-
samples
413+
pub fn create_node_id_vector(&self, mut f: impl FnMut(&NodeTableRow) -> bool) -> Vec<NodeId> {
414+
self.iter()
415+
.filter(|row| f(row))
416+
.map(|row| row.id)
417+
.collect::<Vec<_>>()
429418
}
430419
}
431420

0 commit comments

Comments
 (0)