This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partial Dictionary reimplementations and new Dictionary methods
- Loading branch information
Showing
42 changed files
with
539 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#! use("wally.toml") | ||
name = "cxmeel/sift" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
local INF = newproxy(false) | ||
local INF = newproxy() | ||
|
||
--[=[ | ||
@within Array | ||
|
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,30 @@ | ||
--[=[ | ||
@within Dictionary | ||
Returns the number of elements in the dictionary. If a `predicate` is provided, it will only count the elements that satisfy the predicate. | ||
```lua | ||
local dictionary = { a = 1, b = 2, c = 3 } | ||
count(dictionary) -- 3 | ||
count(dictionary, function(value) | ||
return value > 1 | ||
end) -- 2 | ||
``` | ||
]=] | ||
local function count<K, V>( | ||
dictionary: { [K]: V }, | ||
predicate: ((value: V, key: K) -> boolean)? | ||
): number | ||
local counter = 0 | ||
|
||
for key, value in dictionary do | ||
if not predicate or predicate(value, key) then | ||
counter += 1 | ||
end | ||
end | ||
|
||
return counter | ||
end | ||
|
||
return count |
Empty file.
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,21 @@ | ||
--[=[ | ||
@within Dictionary | ||
Returns an array of key-value pairs from a dictionary. | ||
```lua | ||
entries({ a = 1, b = 2, c = 3 }) | ||
-- { { "a", 1 }, { "b", 2 }, { "c", 3 } } | ||
``` | ||
]=] | ||
local function entries<K, V>(dictionary: { [K]: V }): { { K | V } } | ||
local out = {} | ||
|
||
for key, value in dictionary do | ||
table.insert(out, { key, value :: any }) | ||
end | ||
|
||
return out | ||
end | ||
|
||
return entries |
Empty file.
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,22 @@ | ||
--[=[ | ||
@within Dictionary | ||
Returns `true` if all values in the dictionary pass the test implemented by the provided function. | ||
```lua | ||
every({ a = 1, b = 3, c = 5 }, function(value) | ||
return value % 2 == 0 | ||
}) -- false | ||
``` | ||
]=] | ||
local function every<K, V>(dictionary: { [K]: V }, predicate: (value: V, key: K) -> boolean): boolean | ||
for key, value in dictionary do | ||
if not predicate(value, key) then | ||
return false | ||
end | ||
end | ||
|
||
return true | ||
end | ||
|
||
return every |
Empty file.
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,45 @@ | ||
type ExpandedDictionary<V> = { [string]: ExpandedDictionary<V> | V } | ||
|
||
--[=[ | ||
@within Dictionary | ||
Expands a dictionary with keys that contain a separator into a nested dictionary. | ||
```lua | ||
local dictionary = { | ||
["a"] = 1, | ||
["b.c"] = 2, | ||
["b.d"] = 3, | ||
} | ||
expandKeys(dictionary) -- { a = 1, b = { c = 2, d = 3 } } | ||
``` | ||
]=] | ||
local function expandKeys<V>( | ||
dictionary: { [string]: V }, | ||
separator: string? | ||
): ExpandedDictionary<V> | ||
local withSeparator = separator or "." | ||
local out = {} | ||
|
||
for key, value in dictionary do | ||
local parts = key:split(withSeparator) | ||
local current = out | ||
|
||
while #parts > 1 do | ||
local part = table.remove(parts, 1) | ||
|
||
if not current[part] then | ||
current[part] = {} | ||
end | ||
|
||
current = current[part] | ||
end | ||
|
||
current[parts[1]] = value :: any | ||
end | ||
|
||
return out :: any | ||
end | ||
|
||
return expandKeys |
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,27 @@ | ||
--[=[ | ||
@within Dictionary | ||
Filters out elements from a dictionary based on a filter function. The filter function should return `true` to keep the element, and `false` to remove it. | ||
```lua | ||
filter({ a = 1, b = 2, c = 3 }, function(value) | ||
return value > 1 | ||
end) -- { b = 2, c = 3 } | ||
``` | ||
]=] | ||
local function filter<K, V>( | ||
dictionary: { [K]: V }, | ||
filterer: (value: V, key: K) -> boolean | ||
): { [K]: V } | ||
local out = {} | ||
|
||
for key, value in dictionary do | ||
if filterer(value, key) then | ||
out[key] = value | ||
end | ||
end | ||
|
||
return out | ||
end | ||
|
||
return filter |
Empty file.
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,30 @@ | ||
local INF = newproxy() | ||
|
||
--[=[ | ||
@within Dictionary | ||
Flattens nested dictionaries into a single dictionary. The `depth` parameter specifies the depth of the flattening. If `depth` is not specified, the dictionary is flattened as much as possible. | ||
]=] | ||
local function flatten<K, V>(dictionary: { [any]: any }, depth: number?): { [K]: V } | ||
local withDepth = depth or INF | ||
local out = {} | ||
|
||
for key, value in dictionary do | ||
if typeof(value) == "table" and (withDepth == INF or withDepth > 0) then | ||
local useDepth = withDepth == INF and INF or withDepth - 1 | ||
local nested = flatten(value, useDepth) | ||
|
||
for nestedKey, nestedValue in nested do | ||
out[nestedKey] = nestedValue | ||
end | ||
|
||
continue | ||
end | ||
|
||
out[key] = value | ||
end | ||
|
||
return out | ||
end | ||
|
||
return flatten |
Empty file.
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,36 @@ | ||
local INF = newproxy() | ||
|
||
type FlattenableDictionary<V> = { [string]: FlattenableDictionary<V> | V } | ||
|
||
--[=[ | ||
@within Dictionary | ||
]=] | ||
local function flattenKeys<V>( | ||
dictionary: FlattenableDictionary<any>, | ||
depth: number?, | ||
separator: string? | ||
): { [string]: V } | ||
local withSeparator = separator or "." | ||
local withDepth = depth or INF | ||
|
||
local out: { [string]: V } = {} | ||
|
||
for key, value in dictionary do | ||
if typeof(value) == "table" and depth > 0 then | ||
local useDepth = withDepth == INF and INF or withDepth - 1 | ||
local nested = flattenKeys(value, useDepth, withSeparator) | ||
|
||
for nestedKey, nestedValue in nested do | ||
out[`{key}{withSeparator}{nestedKey}`] = nestedValue | ||
end | ||
|
||
continue | ||
end | ||
|
||
out[key] = value :: V | ||
end | ||
|
||
return out | ||
end | ||
|
||
return flattenKeys |
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,34 @@ | ||
--[=[ | ||
@within Dictionary | ||
Recursively freezes a dictionary and all of its nested dictionaries. Using [Dictionary.freeze] will only freeze the top level of the dictionary, leaving nested dictionaries mutable. | ||
```lua | ||
local frozen = freezeDeep({ | ||
a = "apple", | ||
b = { c = "carrot" }, | ||
}) | ||
frozen.a = "avocado" -- error! | ||
frozen.b.c = "cabbage" -- error! | ||
frozen.c = "coconut" -- error! | ||
``` | ||
]=] | ||
local function freezeDeep<K, V>(dictionary: { [K]: V }): { [K]: V } | ||
local out = {} | ||
|
||
for key, value in dictionary do | ||
if typeof(value) == "table" then | ||
local nested: any = freezeDeep(value) | ||
out[key] = nested | ||
|
||
continue | ||
end | ||
|
||
out[key] = value | ||
end | ||
|
||
return table.freeze(out) | ||
end | ||
|
||
return freezeDeep |
Empty file.
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,23 @@ | ||
--[=[ | ||
@within Dictionary | ||
Constructs a dictionary from two arrays, one containing the keys and the other containing the values. | ||
```lua | ||
local keys = { "a", "b", "c" } | ||
local values = { 1, 2, 3 } | ||
fromArrays(keys, values) -- { a = 1, b = 2, c = 3 } | ||
``` | ||
]=] | ||
local function fromArrays<K, V>(keys: { K }, values: { V }): { [K]: V } | ||
local out = {} | ||
|
||
for index, key in keys do | ||
out[key] = values[index] | ||
end | ||
|
||
return out | ||
end | ||
|
||
return fromArrays |
Empty file.
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,21 @@ | ||
--[=[ | ||
@within Dictionary | ||
Constructs a dictionary from an array of key-value pairs. | ||
```lua | ||
fromEntries({ { "a", 1 }, { "b", 2 }, { "c", 3 } }) | ||
-- { a = 1, b = 2, c = 3 } | ||
``` | ||
]=] | ||
local function fromEntries<K, V>(entries: { { K | V } }): { [K]: V } | ||
local out = {} | ||
|
||
for _, entry in entries do | ||
out[entry[1] :: K] = entry[2] :: V | ||
end | ||
|
||
return out | ||
end | ||
|
||
return fromEntries |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
local None = require("@Sift/None") | ||
|
||
--[=[ | ||
@within Dictionary | ||
Processes each value and key of a dictionary with a mapper function and returns a new dictionary with the results. If no key is returned by the mapper, the original key is used. | ||
```lua | ||
map({ hello = "world" }, function(value, key) | ||
return `{value}!`, key:upper() | ||
end) -- { HELLO = "world!" } | ||
map({ hello = "world" }, function(value) | ||
return value:upper() | ||
end) -- { hello = "WORLD" } | ||
``` | ||
]=] | ||
local function map<K, V, X, Y>(dictionary: { [K]: V }, mapper: (value: V, key: K) -> (Y, X)): { [X]: Y } | ||
local out: { [X]: Y } = {} | ||
|
||
for key, value in dictionary do | ||
local mappedValue, mappedKey = mapper(value, key) | ||
|
||
if mappedKey == None or mappedValue == None then | ||
continue | ||
end | ||
|
||
out[mappedKey or key :: any] = mappedValue | ||
end | ||
|
||
return out | ||
end | ||
|
||
return map |
Empty file.
Oops, something went wrong.