This commit is contained in:
@@ -16,10 +16,10 @@ pub type DLList(a) {
|
|||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let l = new()
|
let l = new()
|
||||||
let assert Ok(l2) = insert_right(l, 123) |> echo
|
let l2 = insert(l, 123) |> echo
|
||||||
let assert Ok(l3) = insert_right(l2, 456) |> echo
|
let l3 = insert(l2, 456) |> echo
|
||||||
let assert Ok(l4) = insert_right(l3, 789)
|
let l4 = insert(l3, 789)
|
||||||
let assert Ok(l5) = insert_right(l4, 0)
|
let l5 = insert(l4, 0)
|
||||||
|
|
||||||
get(l5) |> echo
|
get(l5) |> echo
|
||||||
l5 |> echo
|
l5 |> echo
|
||||||
@@ -35,11 +35,11 @@ pub fn main() {
|
|||||||
|
|
||||||
from_list([1, 2, 3, 4]) |> echo
|
from_list([1, 2, 3, 4]) |> echo
|
||||||
|
|
||||||
let assert Ok(l1) = new() |> insert_right(1)
|
let l1 = new() |> insert(1)
|
||||||
l1 |> echo
|
l1 |> echo
|
||||||
let l1t = take(l1, 5)
|
let l1t = take(l1, 5)
|
||||||
l1t |> echo
|
l1t |> echo
|
||||||
let assert Ok(l2) = insert_right(l1, 2)
|
let l2 = insert(l1, 2)
|
||||||
l2 |> echo
|
l2 |> echo
|
||||||
let l2t = take(l2, 5)
|
let l2t = take(l2, 5)
|
||||||
l2t |> echo
|
l2t |> echo
|
||||||
@@ -61,15 +61,15 @@ pub fn is_empty(list: DLList(a)) -> Bool {
|
|||||||
dict.is_empty(list.mem)
|
dict.is_empty(list.mem)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_right(list: DLList(a), val: a) -> Result(DLList(a), Nil) {
|
pub fn insert(list: DLList(a), val: a) -> DLList(a) {
|
||||||
let ref = list.counter
|
let ref = list.counter
|
||||||
case is_empty(list) {
|
case is_empty(list) {
|
||||||
True -> {
|
True -> {
|
||||||
let node = Node(val, ref, ref)
|
let node = Node(val, ref, ref)
|
||||||
Ok(DLList(ref + 1, ref, dict.insert(list.mem, ref, node)))
|
DLList(ref + 1, ref, dict.insert(list.mem, ref, node))
|
||||||
}
|
}
|
||||||
False -> {
|
False -> {
|
||||||
use curr_node <- result.try(dict.get(list.mem, list.current))
|
let assert Ok(curr_node) = dict.get(list.mem, list.current)
|
||||||
let curr_node_2 = Node(..curr_node, right: ref)
|
let curr_node_2 = Node(..curr_node, right: ref)
|
||||||
let next = curr_node.right
|
let next = curr_node.right
|
||||||
|
|
||||||
@@ -91,7 +91,7 @@ pub fn insert_right(list: DLList(a), val: a) -> Result(DLList(a), Nil) {
|
|||||||
let new_node = Node(val, list.current, next)
|
let new_node = Node(val, list.current, next)
|
||||||
|
|
||||||
let new_mem = dict.insert(new_mem, ref, new_node)
|
let new_mem = dict.insert(new_mem, ref, new_node)
|
||||||
Ok(DLList(list.counter + 1, list.counter, new_mem))
|
DLList(list.counter + 1, list.counter, new_mem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -191,8 +191,8 @@ fn do_move_left(list: DLList(a)) -> Result(DLList(a), Nil) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_list(list: List(a)) -> Result(DLList(a), Nil) {
|
pub fn from_list(list: List(a)) -> DLList(a) {
|
||||||
list.try_fold(list, new(), insert_right)
|
list.fold(list, new(), insert)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn take(list: DLList(a), n_times: Int) -> List(a) {
|
pub fn take(list: DLList(a), n_times: Int) -> List(a) {
|
||||||
|
|||||||
Reference in New Issue
Block a user