Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
schurhammer committed May 25, 2023
1 parent 48c68e3 commit 3f20b05
Show file tree
Hide file tree
Showing 11 changed files with 248 additions and 142 deletions.
4 changes: 2 additions & 2 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ version = "0.1.0"
# links = [{ title = "Website", href = "https://gleam.run" }]

[dependencies]
gleam_stdlib = "~> 0.25"
gleam_stdlib = "~> 0.29.0"

[dev-dependencies]
gleeunit = "~> 0.7"
gleeunit = "~> 0.10.1"
8 changes: 4 additions & 4 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# You typically do not need to edit this file

packages = [
{ name = "gleam_stdlib", version = "0.25.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "AD0F89928E0B919C8F8EDF640484633B28DBF88630A9E6AE504617A3E3E5B9A2" },
{ name = "gleeunit", version = "0.8.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "A1170754BF54F5DD6E9EF392FB1DC612528B007CCBE41B52F0C5453254708490" },
{ name = "gleam_stdlib", version = "0.29.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "DB981FB670AAC6392C0694AF639C49ADF1C2E42664D5F90BBF573102667B8E53" },
{ name = "gleeunit", version = "0.10.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "ECEA2DE4BE6528D36AFE74F42A21CDF99966EC36D7F25DEB34D47DD0F7977BAF" },
]

[requirements]
gleam_stdlib = "~> 0.25"
gleeunit = "~> 0.7"
gleam_stdlib = "~> 0.29.0"
gleeunit = "~> 0.10.1"
136 changes: 0 additions & 136 deletions src/gleamy_structures.gleam

This file was deleted.

86 changes: 86 additions & 0 deletions src/map.gleam
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
}
4 changes: 4 additions & 0 deletions src/non_empty_list.gleam
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))
}
54 changes: 54 additions & 0 deletions src/priority_queue.gleam
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(_) -> []
}
}
82 changes: 82 additions & 0 deletions src/set.gleam
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) })
}
Loading

0 comments on commit 3f20b05

Please sign in to comment.