chore: apply formatting

This commit is contained in:
Erica Marigold 2024-07-15 18:46:36 +05:30
parent 6d39f55e01
commit 2df6d06c41
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
7 changed files with 488 additions and 435 deletions

View file

@ -4,17 +4,17 @@ local Ok = result.Ok
local Err = result.Err
local function canError(): Result<number, string>
if math.round(math.random()) == 1 then
return Err("you DIED")
end
if math.round(math.random()) == 1 then
return Err("you DIED")
end
return Ok(69)
return Ok(69)
end
function main()
local val = canError():unwrap()
local val = canError():unwrap()
print("got value: ", val)
print("got value: ", val)
end
return main()

View file

@ -38,15 +38,15 @@ local Err = result.Err
@return Option<T>
]=]
function Result.ok<T, E>(self: Result<T, E>): Option<T>
if self:isOk() then
if self._value == nil then
return None()
end
if self:isOk() then
if self._value == nil then
return None()
end
return Some(self._value)
end
return Some(self._value)
end
return None()
return None()
end
--[=[
@ -68,11 +68,11 @@ end
@return Option<E>
]=]
function Result.err<T, E>(self: Result<T, E>): Option<E>
if self:isErr() then
return Option.new(self._error) :: Option<E>
end
if self:isErr() then
return Option.new(self._error) :: Option<E>
end
return None()
return None()
end
--[=[
@ -96,17 +96,19 @@ end
@return Option<Result<T, E>>
]=]
function Result.transpose<T, E>(self: Result<Option<T>, E>): Option<Result<T, E>>
if self._value == None() then
return None()
elseif self:isOkAnd(function(val): boolean
return val._optValue == nil
end) then
return Some(Ok(self._value._optValue))
elseif self:isErr() then
return Some(Err(self._error))
end
if self._value == None() then
return None()
elseif
self:isOkAnd(function(val): boolean
return val._optValue == nil
end)
then
return Some(Ok(self._value._optValue))
elseif self:isErr() then
return Some(Err(self._error))
end
error("`Result` is not transposable")
error("`Result` is not transposable")
end
--[=[
@ -131,11 +133,11 @@ end
@return Result<T, E>
]=]
function Option.okOr<T, E>(self: Option<T>, err: E): Result<T, E>
if self:isSome() then
return Ok(self._optValue)
end
if self:isSome() then
return Ok(self._optValue)
end
return Err(err)
return Err(err)
end
--[=[
@ -157,11 +159,11 @@ end
@return Result<T, E>
]=]
function Option.okOrElse<T, E>(self: Option<T>, err: () -> E): Result<T, E>
if self:isSome() then
return Ok(self._optValue :: T)
end
if self:isSome() then
return Ok(self._optValue :: T)
end
return Err(err())
return Err(err())
end
--[=[
@ -185,26 +187,29 @@ end
@return Result<Option<T>, E>
]=]
function Option.transpose<T, E>(self: Option<Result<T, E>>): Result<Option<T>, E>
if self:isSome() then
local inner = self._optValue
assert(self.typeId == "Option" and inner.typeId == "Result", "Only an `Option` of a `Result` can be transposed")
if self:isSome() then
local inner = self._optValue
assert(
self.typeId == "Option" and inner.typeId == "Result",
"Only an `Option` of a `Result` can be transposed"
)
if inner:isOk() then
return Some(Ok(inner._value))
elseif inner:isErr() then
return Some(Err(inner._error))
end
end
if inner:isOk() then
return Some(Ok(inner._value))
elseif inner:isErr() then
return Some(Err(inner._error))
end
end
return Ok(None())
return Ok(None())
end
return {
Ok = Ok,
Err = Err,
Result = Result,
Ok = Ok,
Err = Err,
Result = Result,
Some = Some,
None = None,
Option = Option,
Some = Some,
None = None,
Option = Option,
}

View file

@ -73,26 +73,26 @@ export type Status = "initialized" | "pending" | "cancelled" | "ready"
]=]
export type Future<T> = typeof(Future) & {
_ret: T,
_thread: thread,
_spawnEvt: Signal<()>,
_retEvt: Signal<T | Result<T, string>, Status>,
_status: Status,
_ret: T,
_thread: thread,
_spawnEvt: Signal<()>,
_retEvt: Signal<T | Result<T, string>, Status>,
_status: Status,
}
local function _constructor<T>(fn: (Signal<()>, Signal<T, Status>) -> ())
return setmetatable(
{
_thread = coroutine.create(fn),
return setmetatable(
{
_thread = coroutine.create(fn),
_spawnEvt = Signal.new(),
_retEvt = Signal.new(),
_status = "initialized",
} :: Future<T>,
{
__index = Future,
}
)
_spawnEvt = Signal.new(),
_retEvt = Signal.new(),
_status = "initialized",
} :: Future<T>,
{
__index = Future,
}
)
end
--[=[
@ -110,12 +110,14 @@ end
@return Future<T> -- The constructed future
]=]
function Future.new<T>(fn: (...any) -> T, args: { any })
return _constructor(function(spawnEvt: Signal<()>, retEvt: Signal<T, Status>)
spawnEvt:Fire()
return _constructor(
function(spawnEvt: Signal<()>, retEvt: Signal<T, Status>)
spawnEvt:Fire()
local ret = fn(table.unpack(args))
retEvt:Fire(ret, "ready")
end)
local ret = fn(table.unpack(args))
retEvt:Fire(ret, "ready")
end
)
end
--[=[
@ -128,13 +130,18 @@ end
@return Future<Result<T>> -- The constructed future
]=]
function Future.try<T>(fn: (...any) -> T, args: { any })
return _constructor(function(spawnEvt: Signal<()>, retEvt: Signal<Result<T, string>, Status>)
spawnEvt:Fire()
return _constructor(
function(
spawnEvt: Signal<()>,
retEvt: Signal<Result<T, string>, Status>
)
spawnEvt:Fire()
local ok, ret = pcall(fn, table.unpack(args))
local res: Result<T, string> = if ok then Ok(ret) else Err(ret)
retEvt:Fire(res, "ready")
end)
local ok, ret = pcall(fn, table.unpack(args))
local res: Result<T, string> = if ok then Ok(ret) else Err(ret)
retEvt:Fire(res, "ready")
end
)
end
--[=[
@ -146,33 +153,33 @@ end
@return (Status, Option<T>) -- Returns the [Status] and an optional return if completed
]=]
function Future.poll<T>(self: Future<T>): (Status, Option<T>)
if self._status == "initialized" then
self._retEvt:Connect(function(firedRet, status: Status)
self._status = status
self._ret = firedRet
if self._status == "initialized" then
self._retEvt:Connect(function(firedRet, status: Status)
self._status = status
self._ret = firedRet
-- Cleanup
coroutine.yield(self._thread)
coroutine.close(self._thread)
-- Cleanup
coroutine.yield(self._thread)
coroutine.close(self._thread)
self._spawnEvt:DisconnectAll()
self._retEvt:DisconnectAll()
end)
self._spawnEvt:DisconnectAll()
self._retEvt:DisconnectAll()
end)
self._spawnEvt:Connect(function()
self._status = "pending"
end)
self._spawnEvt:Connect(function()
self._status = "pending"
end)
coroutine.resume(self._thread, self._spawnEvt, self._retEvt)
end
coroutine.resume(self._thread, self._spawnEvt, self._retEvt)
end
if self._status == "pending" then
-- Just wait a bit more for the signal to fire
task.wait(0.01)
end
if self._status == "pending" then
-- Just wait a bit more for the signal to fire
task.wait(0.01)
end
local retOpt = if self._ret == nil then None() else Some(self._ret)
return self._status, retOpt
local retOpt = if self._ret == nil then None() else Some(self._ret)
return self._status, retOpt
end
--[=[
@ -183,8 +190,8 @@ end
@param self Future<T>
]=]
function Future.cancel<T>(self: Future<T>)
self._retEvt:Fire(nil :: any, "cancelled")
self._status = "cancelled"
self._retEvt:Fire(nil :: any, "cancelled")
self._status = "cancelled"
end
--[=[
@ -197,14 +204,14 @@ end
@return T -- The value returned by the function on completion
]=]
function Future.await<T>(self: Future<T>): T
while true do
local status: Status, ret: Option<T> = self:poll()
while true do
local status: Status, ret: Option<T> = self:poll()
if status == "ready" then
-- Safe to unwrap, we know it must not be nil
return ret:unwrap()
end
end
if status == "ready" then
-- Safe to unwrap, we know it must not be nil
return ret:unwrap()
end
end
end
return Future

View file

@ -27,8 +27,8 @@ local tableEq = require("util").tableEq
local Option = {}
export type Option<T> = typeof(Option) & {
_optValue: T?,
typeId: "Option",
_optValue: T?,
typeId: "Option",
}
--[=[
@ -37,7 +37,7 @@ export type Option<T> = typeof(Option) & {
No value.
]=]
function None<T>(): Option<T>
return Option.new(nil) :: Option<T>
return Option.new(nil) :: Option<T>
end
--[=[
@ -46,7 +46,7 @@ end
Some value of type `T`.
]=]
function Some<T>(val: T): Option<T>
return Option.new(val) :: Option<T>
return Option.new(val) :: Option<T>
end
--[=[
@ -58,46 +58,49 @@ end
@return Option<T>
]=]
function Option.new<T>(val: T?)
return setmetatable(
{
_optValue = val,
typeId = "Option",
} :: Option<T>,
{
__index = Option,
__tostring = function<T>(self: Option<T>)
if self._optValue == nil then
return `{self.typeId}::None`
else
return `{self.typeId}::Some({self._optValue})`
end
end,
return setmetatable(
{
_optValue = val,
typeId = "Option",
} :: Option<T>,
{
__index = Option,
__tostring = function<T>(self: Option<T>)
if self._optValue == nil then
return `{self.typeId}::None`
else
return `{self.typeId}::Some({self._optValue})`
end
end,
__eq = function<T>(self: Option<T>, other: Option<T>): boolean
if typeof(self._optValue) == "table" and typeof(other._optValue) == "table" then
return tableEq(self._optValue, other._optValue)
else
return self._optValue == other._optValue
end
end,
__lt = function<T>(self: Option<T>, other: Option<T>): boolean
if self:isSome() and other:isSome() then
return (self._optValue :: any) < (other._optValue :: any)
end
__eq = function<T>(self: Option<T>, other: Option<T>): boolean
if
typeof(self._optValue) == "table"
and typeof(other._optValue) == "table"
then
return tableEq(self._optValue, other._optValue)
else
return self._optValue == other._optValue
end
end,
__lt = function<T>(self: Option<T>, other: Option<T>): boolean
if self:isSome() and other:isSome() then
return (self._optValue :: any) < (other._optValue :: any)
end
return false
end,
__le = function<T>(self: Option<T>, other: Option<T>): boolean
if self:isSome() and other:isSome() then
return (self._optValue :: any) <= (other._optValue :: any)
end
return false
end,
__le = function<T>(self: Option<T>, other: Option<T>): boolean
if self:isSome() and other:isSome() then
return (self._optValue :: any) <= (other._optValue :: any)
end
return false
end,
}
return false
end,
}
-- TODO: Implement __iter, once iterators traits exist
)
-- TODO: Implement __iter, once iterators traits exist
)
end
--[=[
@ -109,7 +112,7 @@ end
@return boolean
]=]
function Option.isSome<T>(self: Option<T>): boolean
return self._optValue ~= nil
return self._optValue ~= nil
end
--[=[
@ -123,8 +126,8 @@ end
@return boolean
]=]
function Option.isSomeAnd<T>(self: Option<T>, op: (val: T) -> boolean): boolean
-- We know that it's not None, so this is fine
return self:isSome() and op(self._optValue :: T)
-- We know that it's not None, so this is fine
return self:isSome() and op(self._optValue :: T)
end
--[=[
@ -136,7 +139,7 @@ end
@return boolean
]=]
function Option.isNone<T>(self: Option<T>): boolean
return not self:isSome()
return not self:isSome()
end
--[=[
@ -161,11 +164,13 @@ end
@return boolean
]=]
function Option.expect<T>(self: Option<T>, msg: string): T | never
if self:isSome() then
return self._optValue :: T
end
if self:isSome() then
return self._optValue :: T
end
return error(`panic: {msg}; expected Option to be type Option::Some(T), got Option::None`)
return error(
`panic: {msg}; expected Option to be type Option::Some(T), got Option::None`
)
end
--[=[
@ -181,11 +186,11 @@ end
@return T | never
]=]
function Option.unwrap<T>(self: Option<T>): T | never
if self:isSome() then
return self._optValue :: T
end
if self:isSome() then
return self._optValue :: T
end
return error("called `Option:unwrap()` on a `None` value")
return error("called `Option:unwrap()` on a `None` value")
end
--[=[
@ -207,11 +212,11 @@ end
@return T
]=]
function Option.unwrapOr<T>(self: Option<T>, default: T): T
if self:isSome() then
return self._optValue :: T
end
if self:isSome() then
return self._optValue :: T
end
return default
return default
end
--[=[
@ -234,11 +239,11 @@ end
@return T
]=]
function Option.unwrapOrElse<T>(self: Option<T>, default: () -> T): T
if self:isSome() then
return self._optValue :: T
end
if self:isSome() then
return self._optValue :: T
end
return default()
return default()
end
--[=[
@ -266,17 +271,17 @@ end
@return Option<U>
]=]
function Option.map<T, U>(self: Option<T>, op: (x: T) -> U?): Option<U>
if self:isSome() then
local val = op(self._optValue :: T)
if self:isSome() then
local val = op(self._optValue :: T)
if val == nil then
return None()
end
if val == nil then
return None()
end
return Some(val)
end
return Some(val)
end
return None()
return None()
end
--[=[
@ -303,11 +308,11 @@ end
@return Option<T>
]=]
function Option.inspect<T>(self: Option<T>, op: (x: T) -> ()): Option<T>
if self:isSome() then
op(self._optValue :: T)
end
if self:isSome() then
op(self._optValue :: T)
end
return self
return self
end
--[=[
@ -333,11 +338,11 @@ end
@return Option<T>
]=]
function Option.mapOr<T, U>(self: Option<T>, default: U, op: (val: T) -> U)
if self:isSome() then
return op(self._optValue :: T)
end
if self:isSome() then
return op(self._optValue :: T)
end
return default
return default
end
--[=[
@ -371,17 +376,21 @@ end
@param op (val: T) -> U -- The function to apply
@return Option<T>
]=]
function Option.mapOrElse<T, U>(self: Option<T>, default: () -> U, op: (val: T) -> U): U
if self:isSome() then
return op(self._optValue :: T)
end
function Option.mapOrElse<T, U>(
self: Option<T>,
default: () -> U,
op: (val: T) -> U
): U
if self:isSome() then
return op(self._optValue :: T)
end
return default()
return default()
end
-- TODO: Iterator traits
function Option.iter(): never
return error("Unimplemented: `Option:iter()`")
return error("Unimplemented: `Option:iter()`")
end
--[=[
@ -415,11 +424,11 @@ end
@return Option<U>
]=]
function Option.and_<T, U>(self: Option<T>, optb: Option<U>): Option<U>
if self:isSome() then
return optb
end
if self:isSome() then
return optb
end
return None()
return None()
end
--[=[
@ -462,11 +471,11 @@ end
@return Option<U>
]=]
function Option.andThen<T, U>(self: Option<T>, op: (val: T) -> Option<U>): Option<U>
if self:isSome() then
return op(self._optValue :: T)
end
if self:isSome() then
return op(self._optValue :: T)
end
return None()
return None()
end
--[=[
@ -492,14 +501,17 @@ end
@param predicate (val: T) -> boolean -- The predicate function which must match an element
@return Option<T>
]=]
function Option.filter<T>(self: Option<T>, predicate: (val: T) -> boolean): Option<T>
if self:isSome() then
if predicate(self._optValue :: T) then
return self
end
end
function Option.filter<T>(
self: Option<T>,
predicate: (val: T) -> boolean
): Option<T>
if self:isSome() then
if predicate(self._optValue :: T) then
return self
end
end
return None()
return None()
end
--[=[
@ -534,11 +546,11 @@ end
@return Option<T>
]=]
function Option.or_<T>(self: Option<T>, optb: Option<T>): Option<T>
if self:isSome() then
return self
end
if self:isSome() then
return self
end
return optb
return optb
end
--[=[
@ -564,11 +576,11 @@ end
@return Option<T>
]=]
function Option.orElse<T>(self: Option<T>, op: () -> Option<T>): Option<T>
if self:isSome() then
return self
end
if self:isSome() then
return self
end
return op()
return op()
end
--[=[
@ -600,17 +612,17 @@ end
@return Option<T>
]=]
function Option.xor<T>(self: Option<T>, optb: Option<T>): Option<T>
if self:isSome() and optb:isSome() then
return None()
end
if self:isSome() and optb:isSome() then
return None()
end
if self:isSome() then
return self
elseif optb:isSome() then
return optb
end
if self:isSome() then
return self
elseif optb:isSome() then
return optb
end
return None()
return None()
end
--[=[
@ -638,8 +650,8 @@ end
@return T
]=]
function Option.insert<T>(self: Option<T>, val: T): T
self._optValue = val
return self._optValue :: T
self._optValue = val
return self._optValue :: T
end
--[=[
@ -667,11 +679,11 @@ end
@return T
]=]
function Option.getOrInsert<T>(self: Option<T>, val: T): T
if self:isNone() then
self._optValue = val
end
if self:isNone() then
self._optValue = val
end
return self._optValue :: T
return self._optValue :: T
end
--[=[
@ -695,13 +707,13 @@ end
@return Option<T>
]=]
function Option.take<T>(self: Option<T>): Option<T>
if self:isSome() then
local val = self._optValue :: T
self._optValue = nil
return Some(val)
end
if self:isSome() then
local val = self._optValue :: T
self._optValue = nil
return Some(val)
end
return None()
return None()
end
--[=[
@ -728,14 +740,14 @@ end
@return Option<T>
]=]
function Option.replace<T>(self: Option<T>, val: T): Option<T>
local current: Option<T> = self
self._optValue = val
local current: Option<T> = self
self._optValue = val
if current:isNone() then
return current
end
if current:isNone() then
return current
end
return Some(current._optValue :: T)
return Some(current._optValue :: T)
end
--[=[
@ -757,11 +769,11 @@ end
@return boolean
]=]
function Option.contains<T>(self: Option<T>, val: T): boolean
if self:isSome() then
return self._optValue == val
end
if self:isSome() then
return self._optValue == val
end
return false
return false
end
--[=[
@ -786,11 +798,11 @@ end
@return Option<{T | U}>
]=]
function Option.zip<T, U>(self: Option<T>, other: Option<U>): Option<{ T | U }>
if self:isSome() and other:isSome() then
return Some({ self._optValue, other._optValue })
end
if self:isSome() and other:isSome() then
return Some { self._optValue, other._optValue }
end
return None()
return None()
end
--[=[
@ -829,16 +841,20 @@ end
@param op (x: T, y: U) -> R?
@return Option<R>
]=]
function Option.zipWith<T, U, R>(self: Option<T>, other: Option<U>, op: (x: T, y: U) -> R?): Option<R>
if self:isSome() and other:isSome() then
local computed = op(self._optValue :: T, other._optValue :: U)
function Option.zipWith<T, U, R>(
self: Option<T>,
other: Option<U>,
op: (x: T, y: U) -> R?
): Option<R>
if self:isSome() and other:isSome() then
local computed = op(self._optValue :: T, other._optValue :: U)
if computed ~= nil then
return Some(computed :: R)
end
end
if computed ~= nil then
return Some(computed :: R)
end
end
return None()
return None()
end
--[=[
@ -861,13 +877,17 @@ end
@return (Option<A>, Option<B>)
]=]
function Option.unzip<T, A, B>(self: Option<T>): (Option<A>, Option<B>)
if self:isSome() then
if self:isSome() and typeof(self._optValue) == "table" and #self._optValue == 2 then
return Some(self._optValue[1] :: A), Some(self._optValue[2] :: B)
end
end
if self:isSome() then
if
self:isSome()
and typeof(self._optValue) == "table"
and #self._optValue == 2
then
return Some(self._optValue[1] :: A), Some(self._optValue[2] :: B)
end
end
return None(), None()
return None(), None()
end
--[=[
@ -887,7 +907,7 @@ end
@return T?
]=]
function Option.unwrapUnchecked<T>(self: Option<T>): T?
return self._optValue
return self._optValue
end
--[=[
@ -908,11 +928,11 @@ end
@return string
]=]
function Option.display<T>(self: Option<T>): string
return tostring(self)
return tostring(self)
end
return {
Option = Option,
Some = Some,
None = None,
Option = Option,
Some = Some,
None = None,
}

View file

@ -66,9 +66,9 @@ local tableEq = require("util").tableEq
local Result = {}
export type Result<T, E> = typeof(Result) & {
_value: T,
_error: E,
typeId: "Result",
_value: T,
_error: E,
typeId: "Result",
}
--[=[
@ -80,7 +80,7 @@ export type Result<T, E> = typeof(Result) & {
@return Result<T, E>
]=]
function Ok<T, E>(val: T): Result<T, E>
return Result.new(val, (nil :: unknown) :: E)
return Result.new(val, (nil :: unknown) :: E)
end
--[=[
@ -92,7 +92,7 @@ end
@return Result<T, E>
]=]
function Err<T, E>(err: E): Result<T, E>
return Result.new((nil :: unknown) :: T, err)
return Result.new((nil :: unknown) :: T, err)
end
--[=[
@ -107,42 +107,44 @@ end
@return Result<T, E>
]=]
function Result.new<T, E>(val: T, err: E)
return setmetatable(
{
_value = val,
_error = err,
typeId = "Result",
} :: Result<T, E>,
{
__index = Result,
__tostring = function<T, E>(self: Result<T, E>)
if self:isOk() then
return `{self.typeId}::Ok({self._value})`
end
return setmetatable(
{
_value = val,
_error = err,
typeId = "Result",
} :: Result<T, E>,
{
__index = Result,
__tostring = function<T, E>(self: Result<T, E>)
if self:isOk() then
return `{self.typeId}::Ok({self._value})`
end
if self:isErr() then
return `{self.typeId}::Err({self._error})`
end
if self:isErr() then
return `{self.typeId}::Err({self._error})`
end
return `{self.typeId}<T, E>`
end,
__eq = function<T, E>(self: Result<T, E>, other: Result<T, E>)
if
typeof(self._value) ~= "table"
and typeof(self._error) ~= "table"
and typeof(other._value) ~= "table"
and typeof(other._error) ~= "table"
then
return self._value == other._value and self._error == other._error
end
return `{self.typeId}<T, E>`
end,
__eq = function<T, E>(self: Result<T, E>, other: Result<T, E>)
if
typeof(self._value) ~= "table"
and typeof(self._error) ~= "table"
and typeof(other._value) ~= "table"
and typeof(other._error) ~= "table"
then
return self._value == other._value
and self._error == other._error
end
return tableEq(self._value, other._value) and tableEq(self._error, other._error)
end,
return tableEq(self._value, other._value)
and tableEq(self._error, other._error)
end,
-- TODO: Implement equality and arithmetic metamethods
-- TODO: Implement __iter, once iterators traits exist
}
)
-- TODO: Implement equality and arithmetic metamethods
-- TODO: Implement __iter, once iterators traits exist
}
)
end
--[=[
@ -162,11 +164,11 @@ end
@return boolean
]=]
function Result.isOk<T, E>(self: Result<T, E>): boolean
if self._value == nil then
return false
end
if self._value == nil then
return false
end
return true
return true
end
--[=[
@ -190,8 +192,11 @@ end
@param predicate (val: T?) -> boolean
@return boolean
]=]
function Result.isOkAnd<T, E>(self: Result<T, E>, predicate: (val: T?) -> boolean): boolean
return self:isOk() and predicate(self._value)
function Result.isOkAnd<T, E>(
self: Result<T, E>,
predicate: (val: T?) -> boolean
): boolean
return self:isOk() and predicate(self._value)
end
--[=[
@ -211,7 +216,7 @@ end
@return boolean
]=]
function Result.isErr<T, E>(self: Result<T, E>)
return not self:isOk()
return not self:isOk()
end
--[=[
@ -235,12 +240,15 @@ end
@param predicate (val: T?) -> boolean
@return boolean
]=]
function Result.isErrAnd<T, E>(self: Result<T, E>, predicate: (val: E) -> boolean): boolean
if self:isErr() then
return predicate(self._error)
end
function Result.isErrAnd<T, E>(
self: Result<T, E>,
predicate: (val: E) -> boolean
): boolean
if self:isErr() then
return predicate(self._error)
end
return false
return false
end
--[=[
@ -280,11 +288,11 @@ end
@return Result<U, E>
]=]
function Result.map<T, E, U>(self: Result<T, E>, op: (val: T) -> U): Result<U, E>
if self:isOk() then
return Result.new(op(self._value), self._error) :: Result<U, E>
end
if self:isOk() then
return Result.new(op(self._value), self._error) :: Result<U, E>
end
return Err(self._error)
return Err(self._error)
end
--[=[
@ -311,11 +319,11 @@ end
@return U
]=]
function Result.mapOr<T, E, U>(self: Result<T, E>, default: U, op: (val: T) -> U): U
if self:isOk() then
return op(self._value)
end
if self:isOk() then
return op(self._value)
end
return default
return default
end
--[=[
@ -339,12 +347,16 @@ end
@param op (val: T) -> U
@return U
]=]
function Result.mapOrElse<T, E, U>(self: Result<T, E>, default: (val: E) -> U, op: (val: T) -> U): U
if self:isOk() then
return op(self._value)
end
function Result.mapOrElse<T, E, U>(
self: Result<T, E>,
default: (val: E) -> U,
op: (val: T) -> U
): U
if self:isOk() then
return op(self._value)
end
return default(self._error)
return default(self._error)
end
--[=[
@ -373,11 +385,11 @@ end
@return Result<T, F>
]=]
function Result.mapErr<T, E, F>(self: Result<T, E>, op: (val: E) -> F): Result<T, F>
if self:isErr() then
return Result.new(self._value, op(self._error))
end
if self:isErr() then
return Result.new(self._value, op(self._error))
end
return Ok(self._value)
return Ok(self._value)
end
--[=[
@ -409,11 +421,11 @@ end
@return Result<T, E>
]=]
function Result.inspect<T, E>(self: Result<T, E>, op: (val: T) -> ()): Result<T, E>
if self:isOk() then
op(self._value)
end
if self:isOk() then
op(self._value)
end
return self
return self
end
--[=[
@ -445,17 +457,17 @@ end
@return Result<T, E>
]=]
function Result.inspectErr<T, E>(self: Result<T, E>, op: (val: E) -> ()): Result<T, E>
if self:isErr() then
op(self._error)
end
if self:isErr() then
op(self._error)
end
return self
return self
end
-- TODO: Iterator traits
-- selene: allow(unused_variable)
function Result.iter<T, E>(self: Result<T, E>): never
return error("Unimplemented: `Result:iter()`")
return error("Unimplemented: `Result:iter()`")
end
--[=[
@ -505,11 +517,11 @@ end
@return T | never
]=]
function Result.expect<T, E>(self: Result<T, E>, msg: string): T | never
if self:isOk() then
return self._value
end
if self:isOk() then
return self._value
end
return error(`panic: {msg}; {self._error}`)
return error(`panic: {msg}; {self._error}`)
end
--[=[
@ -533,17 +545,19 @@ end
@param self Result<T, E>
]=]
function Result.unwrap<T, E>(self: Result<T, E>): T | never
if self:isOk() then
return self._value
end
if self:isOk() then
return self._value
end
return error(`panic: \`Result:unwrap()\` called on an \`Err\` value: {self._error}`)
return error(
`panic: \`Result:unwrap()\` called on an \`Err\` value: {self._error}`
)
end
-- TODO: default values for types
-- selene: allow(unused_variable)
function Result.unwrapOrDefault<T, E>(self: Result<T, E>): never
return error("Unimplemented: `Result:unwrapOrDefault()`")
return error("Unimplemented: `Result:unwrapOrDefault()`")
end
--[=[
@ -563,11 +577,11 @@ end
@return E | never
]=]
function Result.expectErr<T, E>(self: Result<T, E>, msg: string): E | never
if self:isErr() then
return self._error
end
if self:isErr() then
return self._error
end
return error(`panic: {msg}; {self._error}`)
return error(`panic: {msg}; {self._error}`)
end
--[=[
@ -590,21 +604,23 @@ end
@return E | never
]=]
function Result.unwrapErr<T, E>(self: Result<T, E>): E | never
if self:isErr() then
return self._error
end
if self:isErr() then
return self._error
end
return error(`panic: \`Result:unwrapErr()\` called on an \`Ok\` value: {self._value}`)
return error(
`panic: \`Result:unwrapErr()\` called on an \`Ok\` value: {self._value}`
)
end
-- TODO: How the fuck do I implement this?
-- selene: allow(unused_variable)
function Result.intoOk<T, E>(self: Result<T, E>): never
return error("Unimplemented: `Result:intoOk()`")
return error("Unimplemented: `Result:intoOk()`")
end
-- selene: allow(unused_variable)
function Result.intoErr<T, E>(self: Result<T, E>): never
return error("Unimplemented: `Result:intoErr()`")
return error("Unimplemented: `Result:intoErr()`")
end
--[=[
@ -636,11 +652,11 @@ end
@return Result<U, E>
]=]
function Result.and_<T, E, U>(self: Result<T, E>, res: Result<U, E>): Result<U, E>
if self:isOk() then
return res
end
if self:isOk() then
return res
end
return Err(self._error)
return Err(self._error)
end
--[=[
@ -674,11 +690,11 @@ end
@return Result<U, E>
]=]
function Result.andThen<T, E, U>(self: Result<T, E>, op: (...any) -> any): Result<U, E>
if self:isOk() then
return op(self._value)
end
if self:isOk() then
return op(self._value)
end
return Err(self._error) :: Result<U, E>
return Err(self._error) :: Result<U, E>
end
--[=[
@ -713,15 +729,15 @@ end
@return Result<T, F> | never
]=]
function Result.or_<T, E, F>(self: Result<T, E>, res: Result<T, F>): Result<T, F> | never
if self:isErr() then
return res
end
if self:isErr() then
return res
end
if self:isOk() then
return Ok(self._value) :: Result<T, F>
end
if self:isOk() then
return Ok(self._value) :: Result<T, F>
end
return error("called `Result:or()` with an invalid value")
return error("called `Result:or()` with an invalid value")
end
--[=[
@ -752,12 +768,15 @@ end
@param op (x: E) -> Result<T, U>
@return Result<T, U>
]=]
function Result.orElse<T, E, U>(self: Result<T, E>, op: (x: E) -> Result<T, U>): Result<T, U>
if self:isErr() then
return op(self._error)
end
function Result.orElse<T, E, U>(
self: Result<T, E>,
op: (x: E) -> Result<T, U>
): Result<T, U>
if self:isErr() then
return op(self._error)
end
return Ok(self._value)
return Ok(self._value)
end
--[=[
@ -783,11 +802,11 @@ end
@return T
]=]
function Result.unwrapOr<T, E>(self: Result<T, E>, default: T): T
if self:isOk() then
return self._value
end
if self:isOk() then
return self._value
end
return default
return default
end
--[=[
@ -809,11 +828,11 @@ end
@return T
]=]
function Result.unwrapOrElse<T, E>(self: Result<T, E>, op: (x: E) -> T): T
if self:isOk() then
return self._value
end
if self:isOk() then
return self._value
end
return op(self._error)
return op(self._error)
end
--[=[
@ -837,11 +856,11 @@ end
@return boolean
]=]
function Result.contains<T, E>(self: Result<T, E>, val: T): boolean
if self:isOk() then
return self._value == val
end
if self:isOk() then
return self._value == val
end
return false
return false
end
--[=[
@ -865,11 +884,11 @@ end
@return boolean
]=]
function Result.containsErr<T, E>(self: Result<T, E>, err: E): boolean
if self:isErr() then
return self._error == err
end
if self:isErr() then
return self._error == err
end
return false
return false
end
--[=[
@ -890,11 +909,11 @@ end
@return string
]=]
function Result.display<T, E>(self: Result<T, E>): string
return tostring(self)
return tostring(self)
end
return {
Ok = Ok,
Err = Err,
Result = Result,
Ok = Ok,
Err = Err,
Result = Result,
}

View file

@ -1,38 +1,38 @@
-- From https://gist.github.com/sapphyrus/fd9aeb871e3ce966cc4b0b969f62f539
local function tableEq(tbl1, tbl2)
if tbl1 == tbl2 then
return true
elseif type(tbl1) == "table" and type(tbl2) == "table" then
for key1, value1 in pairs(tbl1) do
local value2 = tbl2[key1]
if tbl1 == tbl2 then
return true
elseif type(tbl1) == "table" and type(tbl2) == "table" then
for key1, value1 in pairs(tbl1) do
local value2 = tbl2[key1]
if value2 == nil then
-- avoid the type call for missing keys in tbl2 by directly comparing with nil
return false
elseif value1 ~= value2 then
if type(value1) == "table" and type(value2) == "table" then
if not tableEq(value1, value2) then
return false
end
else
return false
end
end
end
if value2 == nil then
-- avoid the type call for missing keys in tbl2 by directly comparing with nil
return false
elseif value1 ~= value2 then
if type(value1) == "table" and type(value2) == "table" then
if not tableEq(value1, value2) then
return false
end
else
return false
end
end
end
-- check for missing keys in tbl1
for key2, _ in pairs(tbl2) do
if tbl1[key2] == nil then
return false
end
end
-- check for missing keys in tbl1
for key2, _ in pairs(tbl2) do
if tbl1[key2] == nil then
return false
end
end
return true
end
return true
end
return false
return false
end
return {
tableEq = tableEq,
tableEq = tableEq,
}

View file

@ -1,14 +1,16 @@
local luau = require("@lune/luau")
local fs = require("@lune/fs")
local luau = require("@lune/luau")
local SIGNAL_PATH = "Packages/_Index/ffrostflame_luausignal@0.2.4/luausignal/src/init.luau"
local SIGNAL_PATH =
"Packages/_Index/ffrostflame_luausignal@0.2.4/luausignal/src/init.luau"
local _signal = require(SIGNAL_PATH)
export type Signal<T...> = _signal.luauSignal<T...>
local signal: {
new: <T...>() -> Signal<T...>,
} =
luau.load('local task = require("@lune/task")\n' .. fs.readFile(SIGNAL_PATH))()
new: <T...>() -> Signal<T...>,
} = luau.load(
'local task = require("@lune/task")\n' .. fs.readFile(SIGNAL_PATH)
)()
return {
signal = signal,
signal = signal,
}