Compare commits

..

2 Commits

Author SHA1 Message Date
f013d3320d rework: Remove asserts and reduced lets
Some checks failed
test / test (push) Has been cancelled
2025-10-17 23:41:21 +01:00
34d4bf1055 fix: Reordered to_list_loop parameter order 2025-10-17 23:29:32 +01:00

View File

@@ -76,29 +76,30 @@ pub fn insert(list: DLList(a), val: a) -> DLList(a) {
DLList(ref + 1, ref, dict.insert(list.mem, ref, node))
}
False -> {
let assert Ok(curr_node) = dict.get(list.mem, list.current)
let curr_node_2 = Node(..curr_node, right: ref)
let assert Ok(curr_node) = get_curr_node(list)
let next = curr_node.right
let next_node = case next == list.current {
True -> Ok(curr_node_2)
True -> Ok(Node(..curr_node, right: ref))
False -> dict.get(list.mem, next)
}
let new_mem = case next == list.current {
True -> list.mem
False -> dict.insert(list.mem, list.current, curr_node_2)
False ->
dict.insert(list.mem, list.current, Node(..curr_node, right: ref))
}
let new_mem = case next_node {
Ok(next_node) -> {
let next_node_2 = Node(..next_node, left: ref)
dict.insert(new_mem, next, next_node_2)
dict.insert(new_mem, next, Node(..next_node, left: ref))
}
Error(_) -> new_mem
}
let new_node = Node(val, list.current, next)
let new_mem = dict.insert(new_mem, ref, new_node)
DLList(list.counter + 1, list.counter, new_mem)
DLList(
counter: list.counter + 1,
current: list.counter,
mem: dict.insert(new_mem, ref, Node(val, list.current, next)),
)
}
}
}
@@ -211,8 +212,10 @@ fn take_loop(list: DLList(a), n_times: Int, acc: List(a)) -> List(a) {
case n_times {
0 -> list.reverse(acc)
n -> {
let assert Ok(item) = get(list)
take_loop(move_right(list), n - 1, [item, ..acc])
case get(list) {
Error(_) -> []
Ok(item) -> take_rev_loop(move_right(list), n - 1, [item, ..acc])
}
}
}
}
@@ -226,8 +229,10 @@ fn take_rev_loop(list: DLList(a), n_times: Int, acc: List(a)) -> List(a) {
case n_times {
0 -> list.reverse(acc)
n -> {
let assert Ok(item) = get(list)
take_rev_loop(move_left(list), n - 1, [item, ..acc])
case get(list) {
Error(_) -> []
Ok(item) -> take_rev_loop(move_left(list), n - 1, [item, ..acc])
}
}
}
}
@@ -236,32 +241,43 @@ pub fn to_list(list: DLList(a)) -> List(a) {
case is_empty(list) {
True -> []
False -> {
let assert Ok(a) = get(list)
[a, ..to_list_loop(list.current, do_move_right(list))]
case get(list) {
Error(_) -> []
Ok(val) -> [val, ..to_list_loop(do_move_right(list), list.current)]
}
}
}
}
fn to_list_loop(ref: Int, list: Result(DLList(a), Nil)) -> List(a) {
fn to_list_loop(list: Result(DLList(a), Nil), start_ref: Int) -> List(a) {
case list {
Error(_) -> []
Ok(DLList(_, current, _)) if current == ref -> []
Ok(DLList(_, current, _)) if current == start_ref -> []
Ok(list) -> {
let assert Ok(a) = get(list)
[a, ..to_list_loop(ref, do_move_right(list))]
case get(list) {
Error(_) -> []
Ok(val) -> [val, ..to_list_loop(do_move_right(list), start_ref)]
}
}
}
}
pub fn update(list: DLList(a), value: a) -> Result(DLList(a), Nil) {
use <- bool.guard(when: is_empty(list), return: Error(Nil))
let assert Ok(curr_node) = get_curr_node(list)
Ok(
DLList(
..list,
mem: dict.insert(list.mem, list.current, Node(..curr_node, val: value)),
),
)
case get_curr_node(list) {
Error(_) -> Error(Nil)
Ok(curr_node) ->
Ok(
DLList(
..list,
mem: dict.insert(
list.mem,
list.current,
Node(..curr_node, val: value),
),
),
)
}
}
fn update_dict_entry(