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)) DLList(ref + 1, ref, dict.insert(list.mem, ref, node))
} }
False -> { False -> {
let assert Ok(curr_node) = dict.get(list.mem, list.current) let assert Ok(curr_node) = get_curr_node(list)
let curr_node_2 = Node(..curr_node, right: ref)
let next = curr_node.right let next = curr_node.right
let next_node = case next == list.current { 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) False -> dict.get(list.mem, next)
} }
let new_mem = case next == list.current { let new_mem = case next == list.current {
True -> list.mem 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 { let new_mem = case next_node {
Ok(next_node) -> { Ok(next_node) -> {
let next_node_2 = Node(..next_node, left: ref) dict.insert(new_mem, next, Node(..next_node, left: ref))
dict.insert(new_mem, next, next_node_2)
} }
Error(_) -> new_mem Error(_) -> new_mem
} }
let new_node = Node(val, list.current, next)
let new_mem = dict.insert(new_mem, ref, new_node) DLList(
DLList(list.counter + 1, list.counter, new_mem) 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 { case n_times {
0 -> list.reverse(acc) 0 -> list.reverse(acc)
n -> { n -> {
let assert Ok(item) = get(list) case get(list) {
take_loop(move_right(list), n - 1, [item, ..acc]) 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 { case n_times {
0 -> list.reverse(acc) 0 -> list.reverse(acc)
n -> { n -> {
let assert Ok(item) = get(list) case get(list) {
take_rev_loop(move_left(list), n - 1, [item, ..acc]) 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) { case is_empty(list) {
True -> [] True -> []
False -> { False -> {
let assert Ok(a) = get(list) case get(list) {
[a, ..to_list_loop(list.current, do_move_right(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 { case list {
Error(_) -> [] Error(_) -> []
Ok(DLList(_, current, _)) if current == ref -> [] Ok(DLList(_, current, _)) if current == start_ref -> []
Ok(list) -> { Ok(list) -> {
let assert Ok(a) = get(list) case get(list) {
[a, ..to_list_loop(ref, do_move_right(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) { pub fn update(list: DLList(a), value: a) -> Result(DLList(a), Nil) {
use <- bool.guard(when: is_empty(list), return: Error(Nil)) use <- bool.guard(when: is_empty(list), return: Error(Nil))
let assert Ok(curr_node) = get_curr_node(list) case get_curr_node(list) {
Ok( Error(_) -> Error(Nil)
DLList( Ok(curr_node) ->
..list, Ok(
mem: dict.insert(list.mem, list.current, Node(..curr_node, val: value)), DLList(
), ..list,
) mem: dict.insert(
list.mem,
list.current,
Node(..curr_node, val: value),
),
),
)
}
} }
fn update_dict_entry( fn update_dict_entry(