From f013d3320de95c31a6588082a2565f51b32d2921 Mon Sep 17 00:00:00 2001 From: Gareth Pendleton Date: Fri, 17 Oct 2025 23:41:21 +0100 Subject: [PATCH] rework: Remove asserts and reduced lets --- src/dllist.gleam | 64 ++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/src/dllist.gleam b/src/dllist.gleam index 71be07b..4af3a3d 100644 --- a/src/dllist.gleam +++ b/src/dllist.gleam @@ -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,8 +241,10 @@ 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(do_move_right(list), list.current)] + case get(list) { + Error(_) -> [] + Ok(val) -> [val, ..to_list_loop(do_move_right(list), list.current)] + } } } } @@ -247,21 +254,30 @@ fn to_list_loop(list: Result(DLList(a), Nil), start_ref: Int) -> List(a) { Error(_) -> [] Ok(DLList(_, current, _)) if current == start_ref -> [] Ok(list) -> { - let assert Ok(a) = get(list) - [a, ..to_list_loop(do_move_right(list), start_ref)] + 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(