-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48c68e3
commit 3f20b05
Showing
11 changed files
with
248 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import gleam/option.{Option} | ||
import gleam/order | ||
import gleam/list | ||
import structures/red_black_tree as tree | ||
|
||
type Map(a, b) = | ||
tree.Tree(#(a, b)) | ||
|
||
// TODO need a new tree structure designed to compare only keys | ||
// old structure just compares the whole value | ||
|
||
pub fn delete(from map: Map(a, b), delete key: a) -> Map(a, b) { | ||
todo | ||
} | ||
|
||
pub fn drop(from map: Map(a, b), drop disallowed_keys: List(a)) -> Map(a, b) { | ||
todo | ||
} | ||
|
||
pub fn filter(in map: Map(a, b), for property: fn(a, b) -> Bool) -> Map(a, b) { | ||
todo | ||
} | ||
|
||
pub fn fold( | ||
over map: Map(a, b), | ||
from initial: c, | ||
with fun: fn(c, a, b) -> c, | ||
) -> c { | ||
todo | ||
} | ||
|
||
pub fn from_list(list: List(#(a, b))) -> Map(a, b) { | ||
todo | ||
} | ||
|
||
pub fn get(from: Map(a, b), get: a) -> Result(b, Nil) { | ||
todo | ||
} | ||
|
||
pub fn has_key(map: Map(a, b), key: a) -> Bool { | ||
todo | ||
} | ||
|
||
pub fn insert(into map: Map(a, b), for key: a, insert value: b) -> Map(a, b) { | ||
todo | ||
} | ||
|
||
pub fn keys(map: Map(a, b)) -> List(a) { | ||
todo | ||
} | ||
|
||
pub fn map_values(in map: Map(a, b), with fun: fn(a, b) -> c) -> Map(a, c) { | ||
todo | ||
} | ||
|
||
pub fn merge(into map: Map(a, b), from new_entries: Map(a, b)) -> Map(a, b) { | ||
todo | ||
} | ||
|
||
pub fn new() -> Map(a, b) { | ||
todo | ||
} | ||
|
||
pub fn count(map: Map(a, b)) -> Int { | ||
todo | ||
} | ||
|
||
pub fn take(from map: Map(a, b), keeping desired_keys: List(a)) -> Map(a, b) { | ||
todo | ||
} | ||
|
||
pub fn to_list(map: Map(a, b)) -> List(#(a, b)) { | ||
todo | ||
} | ||
|
||
pub fn update( | ||
in map: Map(a, b), | ||
update key: a, | ||
with fun: fn(Option(b)) -> b, | ||
) -> Map(a, b) { | ||
todo | ||
} | ||
|
||
pub fn values(map: Map(a, b)) -> List(b) { | ||
todo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub type NonEmptyList(a) { | ||
End(first: a) | ||
Item(first: a, rest: NonEmptyList(a)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import gleam/order.{Order} | ||
import gleam/list | ||
import structures/pairing_heap as heap | ||
|
||
type Queue(a) = | ||
heap.Heap(a) | ||
|
||
pub fn from_list(list: List(a), compare: fn(a, a) -> Order) -> Queue(a) { | ||
list.fold(list, new(compare), heap.insert) | ||
} | ||
|
||
pub fn is_empty(queue: Queue(a)) -> Bool { | ||
case heap.find_min(queue) { | ||
Ok(_) -> True | ||
Error(_) -> False | ||
} | ||
} | ||
|
||
pub fn count(queue: Queue(a)) -> Int { | ||
case heap.delete_min(queue) { | ||
Ok(#(_, q)) -> count(q) + 1 | ||
Error(_) -> 0 | ||
} | ||
} | ||
|
||
pub fn new(compare: fn(a, a) -> Order) -> Queue(a) { | ||
heap.new(compare) | ||
} | ||
|
||
pub fn pop(from queue: Queue(a)) -> Result(#(a, Queue(a)), Nil) { | ||
heap.delete_min(queue) | ||
} | ||
|
||
pub fn peek(from queue: Queue(a)) -> Result(a, Nil) { | ||
heap.find_min(queue) | ||
} | ||
|
||
pub fn push(onto queue: Queue(a), this item: a) -> Queue(a) { | ||
heap.insert(queue, item) | ||
} | ||
|
||
pub fn reorder(queue: Queue(a), compare: fn(a, a) -> Order) -> Queue(a) { | ||
case heap.delete_min(queue) { | ||
Ok(#(x, q)) -> heap.insert(reorder(q, compare), x) | ||
Error(_) -> heap.new(compare) | ||
} | ||
} | ||
|
||
pub fn to_list(queue: Queue(a)) -> List(a) { | ||
case heap.delete_min(queue) { | ||
Ok(#(x, q)) -> [x, ..to_list(q)] | ||
Error(_) -> [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import gleam/order.{Order} | ||
import gleam/list | ||
import structures/red_black_tree as tree | ||
|
||
type Set(a) = | ||
tree.Tree(a) | ||
|
||
pub fn contains(in set: Set(a), this member: a) -> Bool { | ||
case tree.find(set, member) { | ||
Ok(_) -> True | ||
Error(_) -> False | ||
} | ||
} | ||
|
||
pub fn delete(from set: Set(a), this member: a) -> Set(a) { | ||
tree.delete(set, member) | ||
} | ||
|
||
pub fn filter(in set: Set(a), for property: fn(a) -> Bool) -> Set(a) { | ||
tree.fold( | ||
set, | ||
set, | ||
fn(set, i) { | ||
case property(i) { | ||
True -> set | ||
False -> tree.delete(set, i) | ||
} | ||
}, | ||
) | ||
} | ||
|
||
pub fn fold(over set: Set(a), from initial: b, with reducer: fn(b, a) -> b) -> b { | ||
tree.fold(set, initial, reducer) | ||
} | ||
|
||
pub fn from_list(members: List(a), compare: fn(a, a) -> Order) -> Set(a) { | ||
list.fold(members, tree.new(compare), tree.insert) | ||
} | ||
|
||
pub fn insert(into set: Set(a), this member: a) -> Set(a) { | ||
tree.insert(set, member) | ||
} | ||
|
||
pub fn intersection(of first: Set(a), and second: Set(a)) -> Set(a) { | ||
tree.fold( | ||
second, | ||
tree.clear(first), | ||
fn(a, i) { | ||
case tree.find(first, i) { | ||
Ok(_) -> tree.insert(a, i) | ||
Error(_) -> a | ||
} | ||
}, | ||
) | ||
} | ||
|
||
pub fn new(compare: fn(a, a) -> Order) -> Set(a) { | ||
tree.new(compare) | ||
} | ||
|
||
pub fn count(set: Set(a)) -> Int { | ||
tree.fold(set, 0, fn(a, _) { a + 1 }) | ||
} | ||
|
||
pub fn take(from set: Set(a), keeping desired: List(a)) -> Set(a) { | ||
case desired { | ||
[x, ..xs] -> | ||
case tree.find(set, x) { | ||
Ok(x) -> tree.insert(take(set, xs), x) | ||
Error(_) -> take(set, xs) | ||
} | ||
[] -> tree.clear(set) | ||
} | ||
} | ||
|
||
pub fn to_list(set: Set(a)) -> List(a) { | ||
tree.fold(set, [], fn(a, i) { [i, ..a] }) | ||
} | ||
|
||
pub fn union(of first: Set(a), and second: Set(a)) -> Set(a) { | ||
tree.fold(first, second, fn(a, i) { tree.insert(a, i) }) | ||
} |
Oops, something went wrong.