Skip to content
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

Refactor list implementation and mark init_ as deprecated #1721

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions immut/list/deprecated.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,25 @@ pub fn T::from_iter[A](iter : Iter[A]) -> T[A] {
pub fn T::of[A](arr : FixedArray[A]) -> T[A] {
of(arr)
}

///|
/// Init of the list.
///
/// # Example
///
/// ```
/// assert_eq!(@list.of([1, 2, 3, 4, 5]).init_(), @list.of([1, 2, 3, 4]))
/// ```
/// FIXME: how to avoid warnings in self recursive call?
/// @alert deprecated "This API will be removed in the next major version"
pub fn init_[A](self : T[A]) -> T[A] {
fn aux(self : T[A]) -> T[A] {
match self {
Nil => Nil
Cons(_, Nil) => Nil
Cons(head, tail) => Cons(head, aux(tail))
}
}

aux(self)
}
34 changes: 9 additions & 25 deletions immut/list/list.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ pub fn all[A](self : T[A], f : (A) -> Bool) -> Bool {
///|
/// Test if any element of the list satisfies the predicate.
pub fn any[A](self : T[A], f : (A) -> Bool) -> Bool {
match self {
loop self {
Nil => false
Cons(head, tail) => f(head) || any(tail, f)
Cons(head, tail) => if f(head) { true } else { continue tail }
}
}

Expand Down Expand Up @@ -299,22 +299,6 @@ pub fn last[A](self : T[A]) -> A? {
}
}

///|
/// Init of the list.
///
/// # Example
///
/// ```
/// assert_eq!(@list.of([1, 2, 3, 4, 5]).init_(), @list.of([1, 2, 3, 4]))
/// ```
pub fn init_[A](self : T[A]) -> T[A] {
match self {
Nil => Nil
Cons(_, Nil) => Nil
Cons(head, tail) => Cons(head, init_(tail))
}
}

///|
/// Concatenate two lists.
///
Expand Down Expand Up @@ -369,9 +353,9 @@ pub fn rev[A](self : T[A]) -> T[A] {
/// assert_eq!(r, 15)
/// ```
pub fn fold[A, B](self : T[A], init~ : B, f : (B, A) -> B) -> B {
match self {
Nil => init
Cons(head, tail) => tail.fold(f, init=f(init, head))
loop self, init {
Nil, acc => acc
Cons(head, tail), acc => continue tail, f(acc, head)
}
}

Expand Down Expand Up @@ -594,11 +578,11 @@ pub fn nth[A](self : T[A], n : Int) -> A? {
/// assert_eq!(@list.repeat(5, 1), @list.from_array([1, 1, 1, 1, 1]))
/// ```
pub fn repeat[A](n : Int, x : A) -> T[A] {
if n == 0 {
Nil
} else {
Cons(x, repeat(n - 1, x))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old implementation is non tail recursion, when n is large wound encounter stackoverflow

let mut result = Nil
for i = 0; i < n; i = i + 1 {
result = Cons(x, result)
}
result
}

///|
Expand Down
2 changes: 1 addition & 1 deletion immut/list/list.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl T {
from_json[A : @json.FromJson](Json) -> Self[A][email protected] //deprecated
head[A](Self[A]) -> A?
head_exn[A](Self[A]) -> A //deprecated
init_[A](Self[A]) -> Self[A]
init_[A](Self[A]) -> Self[A] //deprecated
intercalate[A](Self[Self[A]], Self[A]) -> Self[A]
intersperse[A](Self[A], A) -> Self[A]
is_empty[A](Self[A]) -> Bool
Expand Down
Loading