Skip to content

Commit

Permalink
feat(array): Add JanetArray::pop_if
Browse files Browse the repository at this point in the history
  • Loading branch information
GrayJack committed Apr 12, 2024
1 parent 5bd3806 commit 88e6dcd
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/types/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,32 @@ impl<'data> JanetArray<'data> {
}
}

/// Removes and returns the last element in a vector if the predicate
/// returns `true`, or [`None`] if the predicate returns false or the vector
/// is empty.
///
/// # Examples
///
/// ```
/// use janetrs::{array, DeepEq, Janet, JanetArray};
/// # let _client = janetrs::client::JanetClient::init().unwrap();
///
/// let mut array = array![1, 2, 3, 4];
/// let pred = |x: &mut Janet| match x.try_unwrap::<i32>() {
/// Ok(x) => x % 2 == 0,
/// Err(_) => false,
/// };
///
/// assert_eq!(array.pop_if(pred), Some(Janet::from(4)));
/// assert!(array.deep_eq(&array![1, 2, 3]));
/// assert_eq!(array.pop_if(pred), None);
/// ```
pub fn pop_if<F>(&mut self, f: F) -> Option<Janet>
where F: FnOnce(&mut Janet) -> bool {
let last = self.last_mut()?;
if f(last) { self.pop() } else { None }
}

/// Returns a copy of the last element in the array without modifying it.
///
/// # Examples
Expand Down Expand Up @@ -2779,6 +2805,24 @@ mod tests {
Ok(())
}

#[test]
fn pop_if() -> Result<(), crate::client::Error> {
let _client = JanetClient::init()?;


let mut array = array![1, 2, 3, 4];
let pred = |x: &mut Janet| match x.try_unwrap::<i32>() {
Ok(x) => x % 2 == 0,
Err(_) => false,
};

assert_eq!(array.pop_if(pred), Some(Janet::from(4)));
assert!(array.deep_eq(&array![1, 2, 3]));
assert_eq!(array.pop_if(pred), None);

Ok(())
}

#[test]
fn set_length() -> Result<(), crate::client::Error> {
let _client = JanetClient::init()?;
Expand Down

0 comments on commit 88e6dcd

Please sign in to comment.