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

@ -98,9 +98,11 @@ end
function Result.transpose<T, E>(self: Result<Option<T>, E>): Option<Result<T, E>> function Result.transpose<T, E>(self: Result<Option<T>, E>): Option<Result<T, E>>
if self._value == None() then if self._value == None() then
return None() return None()
elseif self:isOkAnd(function(val): boolean elseif
self:isOkAnd(function(val): boolean
return val._optValue == nil return val._optValue == nil
end) then end)
then
return Some(Ok(self._value._optValue)) return Some(Ok(self._value._optValue))
elseif self:isErr() then elseif self:isErr() then
return Some(Err(self._error)) return Some(Err(self._error))
@ -187,7 +189,10 @@ end
function Option.transpose<T, E>(self: Option<Result<T, E>>): Result<Option<T>, E> function Option.transpose<T, E>(self: Option<Result<T, E>>): Result<Option<T>, E>
if self:isSome() then if self:isSome() then
local inner = self._optValue local inner = self._optValue
assert(self.typeId == "Option" and inner.typeId == "Result", "Only an `Option` of a `Result` can be transposed") assert(
self.typeId == "Option" and inner.typeId == "Result",
"Only an `Option` of a `Result` can be transposed"
)
if inner:isOk() then if inner:isOk() then
return Some(Ok(inner._value)) return Some(Ok(inner._value))

View file

@ -110,12 +110,14 @@ end
@return Future<T> -- The constructed future @return Future<T> -- The constructed future
]=] ]=]
function Future.new<T>(fn: (...any) -> T, args: { any }) function Future.new<T>(fn: (...any) -> T, args: { any })
return _constructor(function(spawnEvt: Signal<()>, retEvt: Signal<T, Status>) return _constructor(
function(spawnEvt: Signal<()>, retEvt: Signal<T, Status>)
spawnEvt:Fire() spawnEvt:Fire()
local ret = fn(table.unpack(args)) local ret = fn(table.unpack(args))
retEvt:Fire(ret, "ready") retEvt:Fire(ret, "ready")
end) end
)
end end
--[=[ --[=[
@ -128,13 +130,18 @@ end
@return Future<Result<T>> -- The constructed future @return Future<Result<T>> -- The constructed future
]=] ]=]
function Future.try<T>(fn: (...any) -> T, args: { any }) function Future.try<T>(fn: (...any) -> T, args: { any })
return _constructor(function(spawnEvt: Signal<()>, retEvt: Signal<Result<T, string>, Status>) return _constructor(
function(
spawnEvt: Signal<()>,
retEvt: Signal<Result<T, string>, Status>
)
spawnEvt:Fire() spawnEvt:Fire()
local ok, ret = pcall(fn, table.unpack(args)) local ok, ret = pcall(fn, table.unpack(args))
local res: Result<T, string> = if ok then Ok(ret) else Err(ret) local res: Result<T, string> = if ok then Ok(ret) else Err(ret)
retEvt:Fire(res, "ready") retEvt:Fire(res, "ready")
end) end
)
end end
--[=[ --[=[

View file

@ -74,7 +74,10 @@ function Option.new<T>(val: T?)
end, end,
__eq = function<T>(self: Option<T>, other: Option<T>): boolean __eq = function<T>(self: Option<T>, other: Option<T>): boolean
if typeof(self._optValue) == "table" and typeof(other._optValue) == "table" then if
typeof(self._optValue) == "table"
and typeof(other._optValue) == "table"
then
return tableEq(self._optValue, other._optValue) return tableEq(self._optValue, other._optValue)
else else
return self._optValue == other._optValue return self._optValue == other._optValue
@ -165,7 +168,9 @@ function Option.expect<T>(self: Option<T>, msg: string): T | never
return self._optValue :: T return self._optValue :: T
end 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 end
--[=[ --[=[
@ -371,7 +376,11 @@ end
@param op (val: T) -> U -- The function to apply @param op (val: T) -> U -- The function to apply
@return Option<T> @return Option<T>
]=] ]=]
function Option.mapOrElse<T, U>(self: Option<T>, default: () -> U, op: (val: T) -> U): U function Option.mapOrElse<T, U>(
self: Option<T>,
default: () -> U,
op: (val: T) -> U
): U
if self:isSome() then if self:isSome() then
return op(self._optValue :: T) return op(self._optValue :: T)
end end
@ -492,7 +501,10 @@ end
@param predicate (val: T) -> boolean -- The predicate function which must match an element @param predicate (val: T) -> boolean -- The predicate function which must match an element
@return Option<T> @return Option<T>
]=] ]=]
function Option.filter<T>(self: Option<T>, predicate: (val: T) -> boolean): Option<T> function Option.filter<T>(
self: Option<T>,
predicate: (val: T) -> boolean
): Option<T>
if self:isSome() then if self:isSome() then
if predicate(self._optValue :: T) then if predicate(self._optValue :: T) then
return self return self
@ -787,7 +799,7 @@ end
]=] ]=]
function Option.zip<T, U>(self: Option<T>, other: Option<U>): Option<{ T | U }> function Option.zip<T, U>(self: Option<T>, other: Option<U>): Option<{ T | U }>
if self:isSome() and other:isSome() then if self:isSome() and other:isSome() then
return Some({ self._optValue, other._optValue }) return Some { self._optValue, other._optValue }
end end
return None() return None()
@ -829,7 +841,11 @@ end
@param op (x: T, y: U) -> R? @param op (x: T, y: U) -> R?
@return Option<R> @return Option<R>
]=] ]=]
function Option.zipWith<T, U, R>(self: Option<T>, other: Option<U>, op: (x: T, y: U) -> R?): 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 if self:isSome() and other:isSome() then
local computed = op(self._optValue :: T, other._optValue :: U) local computed = op(self._optValue :: T, other._optValue :: U)
@ -862,7 +878,11 @@ end
]=] ]=]
function Option.unzip<T, A, B>(self: Option<T>): (Option<A>, Option<B>) function Option.unzip<T, A, B>(self: Option<T>): (Option<A>, Option<B>)
if self:isSome() then if self:isSome() then
if self:isSome() and typeof(self._optValue) == "table" and #self._optValue == 2 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) return Some(self._optValue[1] :: A), Some(self._optValue[2] :: B)
end end
end end

View file

@ -133,10 +133,12 @@ function Result.new<T, E>(val: T, err: E)
and typeof(other._value) ~= "table" and typeof(other._value) ~= "table"
and typeof(other._error) ~= "table" and typeof(other._error) ~= "table"
then then
return self._value == other._value and self._error == other._error return self._value == other._value
and self._error == other._error
end end
return tableEq(self._value, other._value) and tableEq(self._error, other._error) return tableEq(self._value, other._value)
and tableEq(self._error, other._error)
end, end,
-- TODO: Implement equality and arithmetic metamethods -- TODO: Implement equality and arithmetic metamethods
@ -190,7 +192,10 @@ end
@param predicate (val: T?) -> boolean @param predicate (val: T?) -> boolean
@return boolean @return boolean
]=] ]=]
function Result.isOkAnd<T, E>(self: Result<T, E>, predicate: (val: T?) -> boolean): boolean function Result.isOkAnd<T, E>(
self: Result<T, E>,
predicate: (val: T?) -> boolean
): boolean
return self:isOk() and predicate(self._value) return self:isOk() and predicate(self._value)
end end
@ -235,7 +240,10 @@ end
@param predicate (val: T?) -> boolean @param predicate (val: T?) -> boolean
@return boolean @return boolean
]=] ]=]
function Result.isErrAnd<T, E>(self: Result<T, E>, predicate: (val: E) -> boolean): boolean function Result.isErrAnd<T, E>(
self: Result<T, E>,
predicate: (val: E) -> boolean
): boolean
if self:isErr() then if self:isErr() then
return predicate(self._error) return predicate(self._error)
end end
@ -339,7 +347,11 @@ end
@param op (val: T) -> U @param op (val: T) -> U
@return U @return U
]=] ]=]
function Result.mapOrElse<T, E, U>(self: Result<T, E>, default: (val: E) -> U, op: (val: T) -> U): U function Result.mapOrElse<T, E, U>(
self: Result<T, E>,
default: (val: E) -> U,
op: (val: T) -> U
): U
if self:isOk() then if self:isOk() then
return op(self._value) return op(self._value)
end end
@ -537,7 +549,9 @@ function Result.unwrap<T, E>(self: Result<T, E>): T | never
return self._value return self._value
end 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 end
-- TODO: default values for types -- TODO: default values for types
@ -594,7 +608,9 @@ function Result.unwrapErr<T, E>(self: Result<T, E>): E | never
return self._error return self._error
end 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 end
-- TODO: How the fuck do I implement this? -- TODO: How the fuck do I implement this?
@ -752,7 +768,10 @@ end
@param op (x: E) -> Result<T, U> @param op (x: E) -> Result<T, U>
@return 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> function Result.orElse<T, E, U>(
self: Result<T, E>,
op: (x: E) -> Result<T, U>
): Result<T, U>
if self:isErr() then if self:isErr() then
return op(self._error) return op(self._error)
end end

View file

@ -1,13 +1,15 @@
local luau = require("@lune/luau")
local fs = require("@lune/fs") 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) local _signal = require(SIGNAL_PATH)
export type Signal<T...> = _signal.luauSignal<T...> export type Signal<T...> = _signal.luauSignal<T...>
local signal: { local signal: {
new: <T...>() -> Signal<T...>, new: <T...>() -> Signal<T...>,
} = } = luau.load(
luau.load('local task = require("@lune/task")\n' .. fs.readFile(SIGNAL_PATH))() 'local task = require("@lune/task")\n' .. fs.readFile(SIGNAL_PATH)
)()
return { return {
signal = signal, signal = signal,