///|
test {
let empty_list : @list.List[Int] = @list.new()
assert_true(empty_list.is_empty())
let list = @list.List([1, 2, 3, 4, 5])
@debug.assert_eq(list, List([1, 2, 3, 4, 5]))
}///|
test {
let list = @list.List([2, 3, 4, 5]).prepend(1)
@debug.assert_eq(list, List([1, 2, 3, 4, 5]))
}///|
test {
let list = @list.List([1, 2, 3, 4, 5])
@test.assert_eq(list.length(), 5)
}///|
test {
let empty_list : @list.List[Int] = @list.new()
@test.assert_eq(empty_list.is_empty(), true)
}///|
test {
let list = @list.List([1, 2, 3, 4, 5])
assert_true(list.head() == Some(1))
}///|
test {
let list = @list.List([1, 2, 3, 4, 5])
@debug.assert_eq(list.unsafe_tail(), List([2, 3, 4, 5]))
}///|
test {
let list = @list.List([1, 2, 3, 4, 5])
assert_true(list.nth(2) == Some(3))
}///|
test {
let arr = []
@list.List([1, 2, 3, 4, 5]).each(x => arr.push(x))
@debug.assert_eq(arr, [1, 2, 3, 4, 5])
}///|
test {
let list = @list.List([1, 2, 3, 4, 5]).map(x => x * 2)
@debug.assert_eq(list, List([2, 4, 6, 8, 10]))
}///|
test {
let list = @list.List([1, 2, 3, 4, 5])
@debug.assert_eq(list.filter(fn(x) { x % 2 == 0 }), List([2, 4]))
let fm = list.filter_map(fn(x) { if x > 3 { Some(x * 10) } else { None } })
@debug.assert_eq(fm, List([40, 50]))
}///|
test {
let list = @list.List([1, 2, 3])
@test.assert_eq(list.fold(init=0, fn(acc, x) { acc + x }), 6)
let indexed = list.foldi(init="", fn(i, acc, x) {
acc + i.to_string() + ":" + x.to_string() + " "
})
inspect(indexed, content="0:1 1:2 2:3 ")
}///|
test {
let list = @list.List([1, 2, 3, 4, 5])
assert_true(list.find(fn(x) { x > 3 }) == Some(4))
assert_true(list.find_index(fn(x) { x == 3 }) == Some(2))
@test.assert_eq(list.any(fn(x) { x > 4 }), true)
@test.assert_eq(list.all(fn(x) { x > 0 }), true)
@test.assert_eq(list.contains(3), true)
@test.assert_eq(list.contains(9), false)
}///|
test {
let list = @list.List([1, 2, 3])
let result = list.flat_map(fn(x) { List([x, x * 10]) })
@debug.assert_eq(result, List([1, 10, 2, 20, 3, 30]))
}///|
test {
let list = @list.List([1, 2, 3, 4, 5])
@debug.assert_eq(list.take(3), List([1, 2, 3]))
@debug.assert_eq(list.drop(3), List([4, 5]))
@debug.assert_eq(list.take_while(fn(x) { x < 4 }), List([1, 2, 3]))
@debug.assert_eq(list.drop_while(fn(x) { x < 4 }), List([4, 5]))
}///|
test {
let list = @list.List([1, 2, 3, 2, 1])
@debug.assert_eq(list.remove(2), List([1, 3, 2, 1]))
@debug.assert_eq(list.remove_at(0), List([2, 3, 2, 1]))
}///|
test {
let list = @list.List([3, 1, 4, 1, 5])
assert_true(list.last() == Some(5))
assert_true(list.minimum() == Some(1))
assert_true(list.maximum() == Some(5))
}///|
test {
let list = @list.List([1, 2, 3, 4, 5]).rev()
@debug.assert_eq(list, List([5, 4, 3, 2, 1]))
}///|
test {
let list = @list.List([1, 2, 3]).concat(List([4, 5]))
@debug.assert_eq(list, List([1, 2, 3, 4, 5]))
}///|
test {
let list = @list.List([@list.List([1, 2]), List([3, 4])]).flatten()
@debug.assert_eq(list, List([1, 2, 3, 4]))
}///|
test {
let list = @list.List([3, 1, 4, 1, 5, 9]).sort()
@debug.assert_eq(list, List([1, 1, 3, 4, 5, 9]))
}///|
test {
let list = @list.List([1, 2, 3])
@debug.assert_eq(list.intersperse(0), List([1, 0, 2, 0, 3]))
let nested = @list.List([@list.List([1, 2]), List([3, 4]), List([5])])
let sep = @list.List([0])
@debug.assert_eq(nested.intercalate(sep), List([1, 2, 0, 3, 4, 0, 5]))
}///|
test {
let a = @list.List([1, 2, 3])
let b = @list.List(["a", "b", "c"])
let zipped = @list.zip(a, b)
@debug.assert_eq(zipped, List([(1, "a"), (2, "b"), (3, "c")]))
let (xs, ys) = zipped.unzip()
@debug.assert_eq(xs, List([1, 2, 3]))
@debug.assert_eq(ys, List(["a", "b", "c"]))
}///|
test {
let list = @list.List([1, 2, 3])
@debug.assert_eq(
list.scan_left(fn(acc, x) { acc + x }, init=0),
List([0, 1, 3, 6]),
)
}///|
test {
let list = @list.List([1, 2, 3, 4, 5])
@test.assert_eq(list.is_prefix(List([1, 2, 3])), true)
@test.assert_eq(list.is_suffix(List([4, 5])), true)
}///|
test {
let assoc = @list.List([("a", 1), ("b", 2), ("c", 3)])
assert_true(assoc.lookup("b") == Some(2))
assert_true(assoc.lookup("z") == None)
}///|
test {
let list = @list.unfold(init=1, fn(n) {
if n > 5 {
None
} else {
Some((n, n + 1))
}
})
@debug.assert_eq(list, List([1, 2, 3, 4, 5]))
}///|
test {
let list = @list.List([1, 2, 3])
debug_inspect(list.iter().to_array(), content="[1, 2, 3]")
let list2 = @list.from_iter([4, 5, 6].iter())
@debug.assert_eq(list2, List([4, 5, 6]))
}///|
test {
let list = @list.List([1, 2, 3, 4, 5])
@debug.assert_eq(list.to_array(), [1, 2, 3, 4, 5])
}///|
test {
let list = @list.List([1, 2, 3, 4, 5])
@debug.assert_eq(list, List([1, 2, 3, 4, 5]))
}///|
test {
let list1 = @list.List([1, 2, 3])
let list2 = @list.List([1, 2, 3])
@test.assert_eq(list1 == list2, true)
}///|
fn safe_head(list : @list.List[Int]) -> Int {
match list.head() {
Some(value) => value
None => 0 // Default value
}
}
///|
test {
let list = @list.List([1, 2, 3])
@test.assert_eq(safe_head(list), 1)
let empty_list : @list.List[Int] = @list.new()
@test.assert_eq(safe_head(empty_list), 0)
}test {
let a = @list.List([1, 2, 3])
let b = @list.List([4, 5, 6])
let result = a + b
@debug.assert_eq(result, List([1, 2, 3, 4, 5, 6]))
}test {
let list1 = @list.List([1, 2, 3])
let list2 = @list.List([1, 2, 4])
let list3 = @list.List([1, 2])
inspect(list1.compare(list2), content="-1") // list1 < list2
inspect(list1.compare(list3), content="1") // list1 > list3
inspect(list3.compare(list1), content="-1") // list3 < list1 (shorter)
inspect(list1.compare(list1), content="0") // list1 = list1
}test {
let lst = @list.List([1, 2, 3, 4, 5])
debug_inspect(
lst,
content=(
#|<List: [1, 2, 3, 4, 5]>
),
)
}test {
let ls = @list.List([2, 4, 6, 8])
@test.assert_eq(ls.all(x => x % 2 == 0), true)
let ls2 = @list.List([2, 3, 6, 8])
@test.assert_eq(ls2.all(x => x % 2 == 0), false)
}test {
let ls = @list.List([1, 3, 5, 6])
@test.assert_eq(ls.any(x => x % 2 == 0), true)
let ls2 = @list.List([1, 3, 5, 7])
@test.assert_eq(ls2.any(x => x % 2 == 0), false)
}test {
let ls = @list.List([1, 2, 3, 4, 5]).concat(List([6, 7, 8, 9, 10]))
@debug.assert_eq(ls, List([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
}test {
let tail = @list.List([2, 3, 4])
let ls = @list.cons(1, tail)
@debug.assert_eq(ls, List([1, 2, 3, 4]))
}test {
let ls = @list.List([1, 2, 3, 4, 5])
@test.assert_eq(ls.contains(3), true)
@test.assert_eq(ls.contains(6), false)
}test {
let ls = @list.List([1, 2, 3, 4, 5])
let r = ls.drop(3)
@debug.assert_eq(r, List([4, 5]))
}test {
let ls = @list.List([1, 2, 3, 4])
let r = ls.drop_while(x => x < 3)
@debug.assert_eq(r, List([3, 4]))
}test {
let arr = []
@list.List([1, 2, 3, 4, 5]).each(x => arr.push(x))
@debug.assert_eq(arr, [1, 2, 3, 4, 5])
}test {
let arr = []
@list.List([1, 2, 3, 4, 5]).eachi((i, x) => arr.push("(\{i},\{x})"))
@debug.assert_eq(arr, ["(0,1)", "(1,2)", "(2,3)", "(3,4)", "(4,5)"])
}test {
@debug.assert_eq(
@list.List([1, 2, 3, 4, 5]).filter(x => x % 2 == 0),
List([2, 4]),
)
}test {
let ls = @list.List([4, 2, 2, 6, 3, 1])
let r = ls.filter_map(x => if x >= 3 { Some(x) } else { None })
@debug.assert_eq(r, List([4, 6, 3]))
}test {
assert_true(
@list.List([1, 3, 5, 8]).find(element => element % 2 == 0) == Some(8),
)
assert_true(@list.List([1, 3, 5]).find(element => element % 2 == 0) == None)
}test {
let ls = @list.List([1, 2, 3, 4, 5])
debug_inspect(ls.find_index(x => x % 2 == 0), content="Some(1)")
debug_inspect(ls.find_index(x => x > 10), content="None")
}test {
assert_true(
@list.List([1, 3, 5, 8]).findi((element, index) => {
element % 2 == 0 && index == 3
}) ==
Some(8),
)
assert_true(
@list.List([1, 3, 8, 5]).findi((element, index) => {
element % 2 == 0 && index == 3
}) ==
None,
)
}test {
let ls = @list.List([1, 2, 3])
let r = ls.flat_map(x => List([x, x * 2]))
@debug.assert_eq(r, List([1, 2, 2, 4, 3, 6]))
}test {
let ls = @list.List([@list.List([1, 2, 3]), List([4, 5, 6]), List([7, 8, 9])]).flatten()
@debug.assert_eq(ls, List([1, 2, 3, 4, 5, 6, 7, 8, 9]))
}test {
let r = @list.List([1, 2, 3, 4, 5]).fold(init=0, (acc, x) => acc + x)
inspect(r, content="15")
}test {
let ls = @list.List([10, 20, 30])
let result = ls.foldi(init=0, (i, acc, x) => acc + x * i)
inspect(result, content="80") // 0*10 + 1*20 + 2*30 = 80
}test {
let ls = @list.List([1, 2, 3, 4, 5])
@debug.assert_eq(ls, List([1, 2, 3, 4, 5]))
}test {
let arr = [1, 2, 3, 4, 5]
let iter = arr.iter()
let ls = @list.from_iter(iter)
@debug.assert_eq(ls, List([1, 2, 3, 4, 5]))
}test {
let arr = [1, 2, 3, 4, 5]
let iter = arr.iter()
let ls = @list.from_iter_rev(iter)
@debug.assert_eq(ls, List([5, 4, 3, 2, 1]))
}test {
assert_true(@list.List([1, 2, 3, 4, 5]).head() == Some(1))
}test {
let ls = @list.List([@list.List([1, 2, 3]), List([4, 5, 6]), List([7, 8, 9])])
let r = ls.intercalate(List([0]))
@debug.assert_eq(r, List([1, 2, 3, 0, 4, 5, 6, 0, 7, 8, 9]))
}test {
let ls = @list.List(["1", "2", "3", "4", "5"]).intersperse("|")
@debug.assert_eq(ls, List(["1", "|", "2", "|", "3", "|", "4", "|", "5"]))
}test {
let empty_list : @list.List[Int] = @list.empty()
@test.assert_eq(empty_list.is_empty(), true)
let non_empty = @list.List([1, 2, 3])
@test.assert_eq(non_empty.is_empty(), false)
}test {
@test.assert_eq(@list.List([1, 2, 3, 4, 5]).is_prefix(List([1, 2, 3])), true)
}test {
@test.assert_eq(@list.List([1, 2, 3, 4, 5]).is_suffix(List([3, 4, 5])), true)
}test {
let ls = @list.List([1, 2, 3, 4, 5])
let iter = ls.iter()
let sum = iter.fold(init=0, (acc, x) => acc + x)
inspect(sum, content="15")
}test {
let ls = @list.List([10, 20, 30])
let iter = ls.iter2()
debug_inspect(iter.to_array(), content="[(0, 10), (1, 20), (2, 30)]")
}test {
assert_true(@list.List([1, 2, 3, 4, 5]).last() == Some(5))
}test {
let ls = @list.List([(1, "a"), (2, "b"), (3, "c")])
assert_true(ls.lookup(3) == Some("c"))
}test {
@debug.assert_eq(
@list.List([1, 2, 3, 4, 5]).map(x => x * 2),
List([2, 4, 6, 8, 10]),
)
}test {
let ls = @list.List([10, 20, 30])
let result = ls.mapi((i, x) => x + i)
@debug.assert_eq(result, List([10, 21, 32]))
}test {
let ls = @list.List([1, 3, 2, 5, 4])
assert_true(ls.maximum() == Some(5))
let empty : @list.List[Int] = @list.empty()
assert_true(empty.maximum() == None)
}test {
let ls = @list.List([1, 3, 2, 5, 4])
assert_true(ls.minimum() == Some(1))
let empty : @list.List[Int] = @list.empty()
assert_true(empty.minimum() == None)
}test {
let ls : @list.List[Int] = @list.new()
@debug.assert_eq(ls, @list.empty())
}test {
let ls = @list.List([1, 2, 3, 4, 5])
assert_true(ls.nth(2) == Some(3))
assert_true(ls.nth(10) == None)
}test {
let ls = @list.List([2, 3, 4]).prepend(1)
@debug.assert_eq(ls, List([1, 2, 3, 4]))
}test {
@debug.assert_eq(@list.List([1, 2, 3, 4, 5]).remove(3), List([1, 2, 4, 5]))
}test {
@debug.assert_eq(@list.List([1, 2, 3, 4, 5]).remove_at(2), List([1, 2, 4, 5]))
}test {
@debug.assert_eq(@list.repeat(5, 1), List([1, 1, 1, 1, 1]))
}test {
@debug.assert_eq(@list.List([1, 2, 3, 4, 5]).rev(), List([5, 4, 3, 2, 1]))
}test {
let ls = @list.List([1, 2, 3, 4, 5]).rev_concat(List([6, 7, 8, 9, 10]))
@debug.assert_eq(ls, List([5, 4, 3, 2, 1, 6, 7, 8, 9, 10]))
}test {
@debug.assert_eq(
@list.List([1, 2, 3, 4, 5]).rev_map(x => x * 2),
List([10, 8, 6, 4, 2]),
)
}test {
let r = @list.rev_unfold(
i => if i == 3 { None } else { Some((i, i + 1)) },
init=0,
)
@debug.assert_eq(r, List([2, 1, 0]))
}test {
let ls = @list.List([1, 2, 3, 4, 5])
let r = ls.scan_left((acc, x) => acc + x, init=0)
@debug.assert_eq(r, List([0, 1, 3, 6, 10, 15]))
}test {
let ls = @list.List([1, 2, 3, 4, 5])
let r = ls.scan_right((acc, x) => acc + x, init=0)
@debug.assert_eq(r, List([15, 14, 12, 9, 5, 0]))
}test {
let ls = @list.singleton(42)
@debug.assert_eq(ls, List([42]))
@test.assert_eq(ls.length(), 1)
}test {
let ls = @list.List([1, 123, 52, 3, 6, 0, -6, -76]).sort()
@debug.assert_eq(ls, List([-76, -6, 0, 1, 3, 6, 52, 123]))
}test {
let ls = @list.List([1, 2, 3, 4, 5])
let r = ls.take(3)
@debug.assert_eq(r, List([1, 2, 3]))
}test {
let ls = @list.List([1, 2, 3, 4])
let r = ls.take_while(x => x < 3)
@debug.assert_eq(r, List([1, 2]))
}test {
let ls = @list.List([1, 2, 3, 4, 5])
let arr = ls.to_array()
@debug.assert_eq(arr, [1, 2, 3, 4, 5])
}test {
let ls = @list.List([1, 2, 3])
let json = ls.to_json()
@debug.debug_inspect(json, content="Array([Number(1), Number(2), Number(3)])")
}test {
let r = @list.unfold(init=0, i => if i == 3 { None } else { Some((i, i + 1)) })
@debug.assert_eq(r, List([0, 1, 2]))
}test {
let ls = @list.List([1, 2, 3, 4, 5])
let tail = ls.unsafe_tail()
@debug.assert_eq(tail, List([2, 3, 4, 5]))
}test {
let (a, b) = @list.List([(1, 2), (3, 4), (5, 6)]).unzip()
@debug.assert_eq(a, List([1, 3, 5]))
@debug.assert_eq(b, List([2, 4, 6]))
}test {
let r = @list.zip(List([1, 2, 3, 4, 5]), List([6, 7, 8, 9, 10]))
@debug.assert_eq(r, List([(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]))
let r2 = @list.zip(List([1, 2]), List([6, 7, 8, 9, 10]))
@debug.assert_eq(r2, List([(1, 6), (2, 7)]))
}Install
Installed by default