Skip to content

Commit

Permalink
Merge pull request #12 from sun-jacobi/split-first
Browse files Browse the repository at this point in the history
Add `List::split_first` and `List::split_last`
  • Loading branch information
RalfJung authored Jul 30, 2024
2 parents 1386718 + 74863e1 commit 043ecaf
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions libspecr/src/list/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ impl<T: Obj> List<T> {
List(GcCow::new(v))
}

/// Returns the first element and the rest of the list.
/// Returns `None` if the list is empty.
pub fn split_first(&self) -> Option<(T, List<T>)> {
if let Some(first) = self.first() {
let rest = self.subslice_with_length(Int::ONE, self.len() - Int::ONE);
Some((first, rest))
} else {
None
}
}

/// Returns the last element and the rest of the list.
/// Returns `None` if the list is empty.
pub fn split_last(&self) -> Option<(T, List<T>)> {
if let Some(last) = self.last() {
let rest = self.subslice_with_length(Int::ZERO, self.len() - Int::ONE);
Some((last, rest))
} else {
None
}
}

/// Conceptually equivalent to `self[start..src.len()] = src;`
pub fn write_subslice_at_index(&mut self, start: Int, src: List<T>) {
// exclusive end
Expand Down

0 comments on commit 043ecaf

Please sign in to comment.