diff options
Diffstat (limited to 'std/default/results.pk')
-rw-r--r-- | std/default/results.pk | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/std/default/results.pk b/std/default/results.pk index 187ece9..4e4d27a 100644 --- a/std/default/results.pk +++ b/std/default/results.pk @@ -7,8 +7,6 @@ pub type Result[T, E] = union Okay: T Error: E -# todo: determine the difference between interfaces and types -# ErrorInterface? Errorable? Err? pub type Error = interface str(Self): str dbg(Self): str @@ -20,42 +18,42 @@ pub func is_ok[T, E](self: Result[T, E]): bool = pub func is_err[T, E](self: Result[T, E]): bool = not self.is_ok -## Converts from a Result[T, E] to an Option[T]. +## Converts from a `Result[T, E]` to an `Option[T]`. pub func ok[T, E](self: Result[T, E]): Option[T] = if self of Okay(x): Some(x) else: None() -## Converts from a Result[T, E] to an Option[E]. +## Converts from a `Result[T, E]` to an `Option[E]`. pub func err[T, E](self: Result[T, E]): Option[E] = if self of Error(x): Some(x) else: None() -## Applies a function to T, if self is Okay. -pub func map[T, E, U](self: Result[T, E], proc: T -> U): Result[U, E] = +## Applies a function to `T`, if self is `Okay`. +pub func map[T, E, U](self: Result[T, E], fn: T -> U): Result[U, E] = match self of Okay(x): - Okay(x.proc) + Okay(fn(x)) of Error(e): Error(e) -## Applies a function to E, if self is Error. -pub func map_err[T, E, F](self: Result[T, E], proc: E -> F): Result[T, F] = +## Applies a function to `E`, if self is `Error`. +pub func map_err[T, E, F](self: Result[T, E], fn: E -> F): Result[T, F] = match self of Error(e): - Error(e.proc) + Error(e.fn) of Okay(x): Okay(x) -## Applies a function to T, if it exists. Equivalent to .map(func).flatten. -pub func flatmap[T, E, U](self: Result[T, E], proc: T -> Result[U, E]): Result[U, E] = +## Applies a function to `T`, if it exists. Equivalent to `self.map(fn).flatten`. +pub func flatmap[T, E, U](self: Result[T, E], fn: T -> Result[U, E]): Result[U, E] = match self of Okay(x): - x.proc + fn(x) of Error(e): Error(e) -## Converts from a Result[Result[T, E], E] to a Result[T, E]. +## Converts from a `Result[Result[T, E], E]` to a `Result[T, E]`. pub func flatten[T, E](self: Result[Result[T, E], E]): Result[T, E] = match self of Okay(Okay(x)): @@ -63,14 +61,14 @@ pub func flatten[T, E](self: Result[Result[T, E], E]): Result[T, E] = of Okay(Error(e)), Error(e): Error(e) -## Transposes a Result[Option[T], E] to an Option[Result[T, E]]. +## Transposes a `Result[Option[T], E]` to an `Option[Result[T, E]]`. pub func transpose[T, E](self: Result[Option[T], E]): Option[Result[T, E]] = match self of Okay(Some(x)): Some(Okay(x)) of Okay(None()), Error(_): None() -## Transposes an Option[Result[T, E]] to a Result[Option[T], E]. Takes a default error. +## Transposes an `Option[Result[T, E]]` to a `Result[Option[T], E]`. Takes a default error. pub func transpose[T, E](self: Option[Result[T, E]], error: E): Result[Option[T], E] = match self of Some(Okay(x)): @@ -84,7 +82,7 @@ pub func transpose[T, E](self: Option[Result[T, E]], error: E): Result[Option[T] pub func get_or[T, E](self: Result[T, E], default: T): T = if self of Okay(x): x else: default -## Directly accesses the inner value. Throws an exception if Error(e). +## Directly accesses the inner value. Throws an exception if `Error`. pub yeet func `!`[T, E](self: Result[T, E]): T = match self of Okay(x): x @@ -94,14 +92,14 @@ pub yeet func get_err[T, E](self: Result[T, E]): E = of Error(e): e of Okay(x): raise Exception(x) # todo: syntax?? -## Indirect access. Propagates Error. +## Indirect access. Propagates `Error`. macro `?`[T, E](self: Result[T, E]) = quote: match self of Okay(x): x of Error(e): return Error(e) -## Overloads the == operation for use on Results. +## Overloads the `==` operation for use on Results. pub func `==`[T, E, F](a: Result[T, E], b: Result[T, F]): bool = match (a, b) of (Okay(x), Okay(y)): @@ -109,7 +107,7 @@ pub func `==`[T, E, F](a: Result[T, E], b: Result[T, F]): bool = of _: false -## Overloads the str() function for use on Results. +## Overloads the `str()` function for use on Results. pub func str[T, E](self: Result[T, E]): str = match self of Some(x): |