-
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
3f20b05
commit bd54c8e
Showing
12 changed files
with
1,031 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ jobs: | |
- uses: erlef/[email protected] | ||
with: | ||
otp-version: "25.2" | ||
gleam-version: "0.25.3" | ||
gleam-version: "0.29.0" | ||
rebar3-version: "3" | ||
# elixir-version: "1.14.2" | ||
- run: gleam format --check src test | ||
|
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 |
---|---|---|
@@ -1,86 +1,85 @@ | ||
import gleam/option.{Option} | ||
import gleam/order | ||
import gleam/order.{Order} | ||
import gleam/list | ||
import structures/red_black_tree as tree | ||
import structures/red_black_tree_kv as tree | ||
|
||
type Map(a, b) = | ||
tree.Tree(#(a, b)) | ||
type Map(k, v) = | ||
tree.Tree(k, v) | ||
|
||
// 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 new(compare: fn(k, k) -> Order) -> Map(k, v) { | ||
tree.new(compare) | ||
} | ||
|
||
pub fn has_key(map: Map(a, b), key: a) -> Bool { | ||
todo | ||
pub fn insert(into map: Map(k, v), key key: k, value value: v) -> Map(k, v) { | ||
tree.insert(map, key, value) | ||
} | ||
|
||
pub fn insert(into map: Map(a, b), for key: a, insert value: b) -> Map(a, b) { | ||
todo | ||
pub fn find(in map: Map(k, v), key key: k) -> Result(#(k, v), Nil) { | ||
tree.find(map, key) | ||
} | ||
|
||
pub fn keys(map: Map(a, b)) -> List(a) { | ||
todo | ||
pub fn has_key(in map: Map(k, v), key key: k) -> Bool { | ||
case tree.find(map, key) { | ||
Ok(_) -> True | ||
Error(_) -> False | ||
} | ||
} | ||
|
||
pub fn map_values(in map: Map(a, b), with fun: fn(a, b) -> c) -> Map(a, c) { | ||
todo | ||
pub fn delete(from map: Map(k, v), this key: k) -> Map(k, v) { | ||
tree.delete(map, key) | ||
} | ||
|
||
pub fn merge(into map: Map(a, b), from new_entries: Map(a, b)) -> Map(a, b) { | ||
todo | ||
pub fn count(map: Map(k, v)) -> Int { | ||
tree.fold(map, 0, fn(a, _, _) { a + 1 }) | ||
} | ||
|
||
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 | ||
pub fn fold( | ||
over map: Map(k, v), | ||
from initial: a, | ||
with reducer: fn(a, k, v) -> a, | ||
) -> a { | ||
tree.fold(map, initial, reducer) | ||
} | ||
|
||
pub fn filter(in map: Map(k, v), for property: fn(k, v) -> Bool) -> Map(k, v) { | ||
tree.fold( | ||
map, | ||
map, | ||
fn(map, k, v) { | ||
case property(k, v) { | ||
True -> map | ||
False -> tree.delete(map, k) | ||
} | ||
}, | ||
) | ||
} | ||
|
||
pub fn merge(this first: Map(k, v), and second: Map(k, v)) -> Map(k, v) { | ||
tree.fold(first, second, fn(a, k, v) { tree.insert(a, k, v) }) | ||
} | ||
|
||
// return the map keeping only keys in the list | ||
pub fn take(from map: Map(k, v), keeping desired: List(k)) -> Map(k, v) { | ||
case desired { | ||
[x, ..xs] -> | ||
case tree.find(map, x) { | ||
Ok(x) -> tree.insert(take(map, xs), x.0, x.1) | ||
Error(_) -> take(map, xs) | ||
} | ||
[] -> tree.clear(map) | ||
} | ||
} | ||
|
||
pub fn from_list( | ||
members: List(#(k, v)), | ||
compare: fn(k, k) -> Order, | ||
) -> Map(k, v) { | ||
list.fold( | ||
members, | ||
tree.new(compare), | ||
fn(tree, i) { tree.insert(tree, i.0, i.1) }, | ||
) | ||
} | ||
|
||
pub fn to_list(map: Map(k, v)) -> List(#(k, v)) { | ||
tree.foldr(map, [], fn(a, k, v) { [#(k, v), ..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 |
---|---|---|
@@ -1,4 +1,63 @@ | ||
import gleam/list | ||
|
||
pub type NonEmptyList(a) { | ||
End(first: a) | ||
Item(first: a, rest: NonEmptyList(a)) | ||
Next(first: a, rest: NonEmptyList(a)) | ||
} | ||
|
||
pub fn fold( | ||
over list: NonEmptyList(a), | ||
from initial: b, | ||
with fun: fn(b, a) -> b, | ||
) -> b { | ||
case list { | ||
End(item) -> fun(initial, item) | ||
Next(x, xs) -> fold(xs, fun(initial, x), fun) | ||
} | ||
} | ||
|
||
pub fn count(list: NonEmptyList(a)) -> Int { | ||
fold(list, 0, fn(acc, _) { acc + 1 }) | ||
} | ||
|
||
pub fn map(list: NonEmptyList(a), transform: fn(a) -> b) -> NonEmptyList(b) { | ||
case list { | ||
End(x) -> End(transform(x)) | ||
Next(x, xs) -> | ||
fold(xs, End(transform(x)), fn(acc, item) { Next(transform(item), acc) }) | ||
|> reverse | ||
} | ||
} | ||
|
||
pub fn filter(list: NonEmptyList(a), predicate: fn(a) -> Bool) -> List(a) { | ||
fold( | ||
list, | ||
[], | ||
fn(acc, item) { | ||
case predicate(item) { | ||
True -> [item, ..acc] | ||
False -> acc | ||
} | ||
}, | ||
) | ||
|> list.reverse() | ||
} | ||
|
||
pub fn to_list(list: NonEmptyList(a)) -> List(a) { | ||
fold(list, [], fn(acc, item) { [item, ..acc] }) | ||
|> list.reverse() | ||
} | ||
|
||
pub fn from_list(list: List(a)) -> Result(NonEmptyList(a), Nil) { | ||
case list { | ||
[] -> Error(Nil) | ||
[x, ..xs] -> Ok(list.fold(xs, End(x), fn(acc, item) { Next(item, acc) })) | ||
} | ||
} | ||
|
||
pub fn reverse(list: NonEmptyList(a)) -> NonEmptyList(a) { | ||
case list { | ||
End(_) -> list | ||
Next(x, xs) -> fold(xs, End(x), fn(acc, x) { Next(x, acc) }) | ||
} | ||
} |
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 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
Oops, something went wrong.