8000 fix(list): deprecate @list.tail by illusory0x0 · Pull Request #2373 · moonbitlang/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(list): deprecate @list.tail #2373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion list/README.mbt.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Get the list without its first element.
```moonbit
test {
let list = @list.of([1, 2, 3, 4, 5])
assert_eq(list.tail(), @list.of([2, 3, 4, 5]))
assert_eq(list.unsafe_tail(), @list.of([2, 3, 4, 5]))
}
```

Expand Down
7 changes: 7 additions & 0 deletions list/deprecated.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ pub fn[A, B] rev_foldi(self : T[A], init~ : B, f : (Int, B, A) -> B) -> B {
}

///|
#deprecated("use `unsafe_tail` instead")
pub fn[A] tail(self : T[A]) -> T[A] {
match self {
Empty => Empty
More(_, tail~) => tail
}
}
23 changes: 8 additions & 15 deletions list/list.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -282,21 +282,6 @@ pub fn[A] any(self : T[A], f : (A) -> Bool raise?) -> Bool raise? {
}
}

///|
/// Tail of the list.
///
/// # Example
///
/// ```mbt
/// assert_eq(@list.of([1, 2, 3, 4, 5]).tail(), @list.of([2, 3, 4, 5]))
/// ```
pub fn[A] tail(self : T[A]) -> T[A] {
match self {
Empty => Empty
More(_, tail~) => tail
}
}

///|
/// Get first element of the list.
#internal(unsafe, "Panic if the list is empty")
Expand All @@ -307,6 +292,14 @@ pub fn[A] unsafe_head(self : T[A]) -> A {
}
}

///|
pub fn[A] unsafe_tail(self : T[A]) -> T[A] {
match self {
Empty => abort("tail of empty list")
More(_, tail~) => tail
}
}

///|
/// Get first element of the list.
///
Expand Down
2 changes: 2 additions & 0 deletions list/list.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fn[A, B] T::rev_map(Self[A], (A) -> B raise?) -> Self[B] raise?
fn[A, E] T::scan_left(Self[A], (E, A) -> E raise?, init~ : E) -> Self[E] raise?
fn[A, B] T::scan_right(Self[A], (B, A) -> B raise?, init~ : B) -> Self[B] raise?
fn[A : Compare] T::sort(Self[A]) -> Self[A]
#deprecated
fn[A] T::tail(Self[A]) -> Self[A]
fn[A] T::take(Self[A], Int) -> Self[A]
fn[A] T::take_while(Self[A], (A) -> Bool raise?) -> Self[A] raise?
Expand All @@ -96,6 +97,7 @@ fn[A] T::unsafe_last(Self[A]) -> A
fn[A : Compare] T::unsafe_maximum(Self[A]) -> A
fn[A : Compare] T::unsafe_minimum(Self[A]) -> A
fn[A] T::unsafe_nth(Self[A], Int) -> A
fn[A] T::unsafe_tail(Self[A]) -> Self[A]
fn[A, B] T::unzip(Self[(A, B)]) -> (Self[A], Self[B])
fn[A, B] T::zip(Self[A], Self[B]) -> Self[(A, B)]
impl[A] Add for T[A]
Expand Down
12 changes: 8 additions & 4 deletions list/list_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,15 @@ test "any" {
}

///|
test "tail" {
test "unsafe_tail" {
let ls = @list.of([1, 2, 3, 4, 5])
let el : @list.T[Int] = @list.empty()
inspect(ls.tail(), content="@list.of([2, 3, 4, 5])")
inspect(el.tail(), content="@list.of([])")
inspect(ls.unsafe_tail(), content="@list.of([2, 3, 4, 5])")
}

///|
test "panic unsafe_tail" {
let ls : @list.T[Int] = @list.empty()
inspect(ls.unsafe_tail())
}

///|
Expand Down
0