Skip to content

Commit

Permalink
feat: utils for lazy-list
Browse files Browse the repository at this point in the history
  • Loading branch information
CAIMEOX committed Dec 28, 2024
1 parent 62c4693 commit c763c8c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 23 deletions.
6 changes: 1 addition & 5 deletions src/lib/driver.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ type! TestError {

///|
enum TestSuccess {
Success(
num_tests~ : Int,
coverage~ : Coverage,
output~ : String
)
Success(num_tests~ : Int, coverage~ : Coverage, output~ : String)
}

///|
Expand Down
6 changes: 3 additions & 3 deletions src/lib/driver_wbtest.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ test "modify max tests" {
}

test "large max tests" {
quick_check!(Arrow(prop_rev) |> with_max_success(10000))
quick_check!(() |> with_max_success(10000))
}

test "larger max tests" {
quick_check!(Arrow(prop_rev) |> with_max_success(100000))
quick_check!(() |> with_max_success(100000))
}

test "add assoc double (expect fail)" {
Expand Down Expand Up @@ -177,7 +177,7 @@ test "prop length is not greater" {

test "prop remove not presence with max success" {
quick_check!(
Arrow(prop_remove_not_presence) |> with_max_success(10000) |> expect_fail,
Arrow(prop_remove_not_presence) |> with_max_success(1000) |> expect_fail,
)
}

Expand Down
76 changes: 61 additions & 15 deletions src/lib/lazy/lazy_list.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@ pub fn default[X : Default]() -> LazyList[X] {

///|
pub impl[T : Show] Show for LazyList[T] with output(self, logger) {
match self {
Nil => logger.write_string("Nil")
Cons(x, xs) =>
logger
..write_string("Cons(")
..write_object(x)
..write_string(", ")
..write_object(force(xs))
.write_char(')')
logger.write_string("[")
loop self {
Nil => break
Cons(x, xs) => {
let next = xs.force()
logger.write_object(x)
match next {
Nil => break
_ => {
logger.write_string(", ")
continue next
}
}
}
}
logger.write_string("]")
}

///|
Expand Down Expand Up @@ -88,13 +94,21 @@ pub fn map[T, U](self : LazyList[T], f : (T) -> U) -> LazyList[U] {

///|
pub fn split_at[T](self : LazyList[T], i : Int) -> (LazyList[T], LazyList[T]) {
match (self, i) {
(Cons(_), 0) => (Nil, self)
(Cons(x, xs), i) => {
let (l, r) = xs.force().split_at(i - 1)
(Cons(x, from_thunk(fn() { l })), r)
if i <= 0 {
(Nil, self)
} else {
fn split_at_prime(m, xs) {
match (m, xs) {
(_, Nil) => (Nil, Nil)
(1, Cons(x, xs)) => (Cons(x, from_value(Nil)), xs.force())
(m, Cons(x, xs)) => {
let (xs1, xs2) = split_at_prime(m - 1, xs.force())
(Cons(x, xs1 |> from_value), xs2)
}
}
}
(Nil, _) => (Nil, Nil)

split_at_prime(i, self)
}
}

Expand Down Expand Up @@ -166,6 +180,30 @@ pub fn take_while[T](self : LazyList[T], p : (T) -> Bool) -> LazyList[T] {
}
}

///|
pub fn take[T](self : LazyList[T], n : Int) -> LazyList[T] {
if n <= 0 {
Nil
} else {
match self {
Nil => Nil
Cons(x, xs) => Cons(x, from_value(take(xs.force(), n - 1)))
}
}
}

///|
pub fn drop[T](self : LazyList[T], n : Int) -> LazyList[T] {
if n <= 0 {
self
} else {
match self {
Nil => Nil
Cons(_, xs) => drop(xs.force(), n - 1)
}
}
}

///|
pub fn drop_while[T](self : LazyList[T], p : (T) -> Bool) -> LazyList[T] {
match self {
Expand Down Expand Up @@ -215,3 +253,11 @@ pub fn unfold[T](
Some((x, y)) => Cons(x, from_thunk(fn() { y.unfold(f) }))
}
}

///|
pub fn from_list[T](ls : @immut/list.T[T]) -> LazyList[T] {
match ls {
Nil => Nil
Cons(x, xs) => Cons(x, from_value(from_list(xs)))
}
}

0 comments on commit c763c8c

Please sign in to comment.