diff options
Diffstat (limited to 'std')
-rw-r--r-- | std/default/format.pk | 3 | ||||
-rw-r--r-- | std/default/options.pk | 26 | ||||
-rw-r--r-- | std/default/results.pk | 38 |
3 files changed, 33 insertions, 34 deletions
diff --git a/std/default/format.pk b/std/default/format.pk index c7d7ab8..ec573ef 100644 --- a/std/default/format.pk +++ b/std/default/format.pk @@ -13,10 +13,11 @@ pub type Debug = interface dbg(Self): str ## Prints all of its arguments to the command line. -pub func print(params: varargs[Display]) = +pub proc print(params: varargs[Display]) = stdout.write(params.map(x => x.str).join(" "), "\n") ## Prints all of its arguments to the command line, in Debug form. +## Note: this function is special! does not count as a side effect pub func dbg(params: varargs[Debug]) = stdout.write(params.map(x => x.dbg).join(" "), "\n") diff --git a/std/default/options.pk b/std/default/options.pk index 3aaea49..bbe0d9c 100644 --- a/std/default/options.pk +++ b/std/default/options.pk @@ -12,30 +12,30 @@ pub func is_some[T](self: Option[T]): bool = pub func is_none[T](self: Option[T]): bool = not self.is_some -## Converts an Option[T] to a Result[T, E] given a user-provided error. +## Converts an `Option[T]` to a `Result[T, E]` given a user-provided error. pub func err[T, E](self: Option[T], error: E): Result[T, E] = if self of Some(x): Okay(x) else: Error(error) -## Applies a function to T, if it exists. -pub func map[T, U](self: Option[T], proc: T -> U): Option[U] = +## Applies a function to `T`, if it exists. +pub func map[T, U](self: Option[T], fn: T -> U): Option[U] = if self of Some(x): - Some(x.proc) + Some(fn(x)) else: None -## Converts T to a None, if proc returns false and it exists. -pub func filter[T](self: Option[T], proc: T -> bool): Option[T] = - if self of Some(x) and proc(x): +## Converts `T` to a `None`, if `fn` returns false and it exists. +pub func filter[T](self: Option[T], fn: T -> bool): Option[T] = + if self of Some(x) and fn(x): Some(x) else: None -## Applies a function to T, if it exists. Equivalent to .map(func).flatten. -pub func flatmap[T, U](self: Option[T], proc: T -> Option[U]): Option[U] = +## Applies a function to T, if it exists. Equivalent to `self.map(fn).flatten`. +pub func flatmap[T, U](self: Option[T], fn: T -> Option[U]): Option[U] = if self of Some(x): - x.proc + fn(x) else: None ## Converts from Option[Option[T]] to Option[T]. @@ -55,14 +55,14 @@ pub yeet func `!`[T](self: Option[T]): T = if self of Some(x): x else: raise Exception # todo: syntax?? -## Indirect access. Propagates None. +## Indirect access. Propagates `None`. pub macro `?`[T](self: Option[T]) = quote: match self of Some(x): x of None: return None -## Overloads the == operation for use on Options. +## Overloads the `==` operation for use on Options. pub func `==`[T](a, b: Option[T]): bool = match (a, b) of (Some(x), Some(y)): @@ -70,7 +70,7 @@ pub func `==`[T](a, b: Option[T]): bool = of _: false -## Overloads the str() function for use on Options. +## Overloads the `str()` function for use on Options. pub func str[T](self: Option[T]): str = if self of Some(x): fmt("some({})", x.str) 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): |