Skip to content

Commit

Permalink
Merge pull request #14 from calcit-lang/dissoc-empty
Browse files Browse the repository at this point in the history
fix dissoc of singleton to empty
  • Loading branch information
soyaine authored Mar 10, 2022
2 parents c1b5b7d + 0579ac3 commit 82d146a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

[package]
name = "im_ternary_tree"
version = "0.0.5"
version = "0.0.6"
edition = "2021"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
license = "MIT"
Expand Down
16 changes: 10 additions & 6 deletions examples/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ pub fn main() -> Result<(), String> {
// }
// }

let mut data: Vec<usize> = vec![];
for idx in 0..110 {
data.push(idx);
let tree = TernaryTreeList::from(&data);
println!("{}", tree.format_inline());
}
// let mut data: Vec<usize> = vec![];
// for idx in 0..110 {
// data.push(idx);
// let tree = TernaryTreeList::from(&data);
// println!("{}", tree.format_inline());
// }

let mut data: TernaryTreeList<usize> = TernaryTreeList::Empty;
data = data.push_right(0);
let _e = data.dissoc(0)?;

Ok(())
}
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,13 @@ where
pub fn dissoc(&self, idx: usize) -> Result<Self, String> {
match self {
Empty => Err(String::from("calling dissoc from empty")),
Tree(t) => Ok(TernaryTreeList::Tree(t.dissoc(idx)?)),
Tree(t) => {
if idx == 0 && t.len() == 1 {
Ok(TernaryTreeList::Empty)
} else {
Ok(TernaryTreeList::Tree(t.dissoc(idx)?))
}
}
}
}
pub fn rest(&self) -> Result<Self, String> {
Expand Down
2 changes: 1 addition & 1 deletion src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ where
return Err(format!("Index too large {} for {}", idx, self.len()));
} else if self.len() == 1 {
// idx already == 0
return Err(String::from("Cannot remove from singleton list"));
return Err(String::from("Cannot remove from singleton list to empty"));
}

match self {
Expand Down
9 changes: 9 additions & 0 deletions tests/list_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,12 @@ fn take_skip() -> Result<(), String> {

Ok(())
}

#[test]
fn dissoc_empty() -> Result<(), String> {
let data = TernaryTreeList::from(&[1]);

assert_eq!(data.dissoc(0).unwrap(), TernaryTreeList::Empty);

Ok(())
}

0 comments on commit 82d146a

Please sign in to comment.