Class Prelude
- Namespace
- Rascal
- Assembly
- Rascal.dll
A class containing various utility methods, a 'prelude' to the rest of the library.
public static class Prelude
- Inheritance
-
Prelude
- Inherited Members
Remarks
This class is meant to be imported statically, eg. using static Rascal.Prelude;
.
Recommended to be imported globally via a global using statement.
Methods
Err<T>(Error)
Creates a result containing an error.
[Pure]
public static Result<T> Err<T>(Error error)
Parameters
error
ErrorThe error to create the result from.
Returns
- Result<T>
- A class containing various utility methods, a 'prelude' to the rest of the library.
Type Parameters
T
The type of an ok value in the result.
Iterate<T>(Result<T>, Func<T, Result<T>>)
Applies a transform function onto a value until the function returns an error.
[Pure]
public static IEnumerable<T> Iterate<T>(Result<T> start, Func<T, Result<T>> transform)
Parameters
start
Result<T>The starting value.
transform
Func<T, Result<T>>A function which transforms a value into either a new value or an error.
Returns
- IEnumerable<T>
A sequence of successful values produced by repeatedly applying
transform
onto the previous value until an error is returned. Includesstart
if it has a value, otherwise the sequence will be empty.
Type Parameters
T
The type of the value transform.
Iterate<T>(T, Func<T, Result<T>>)
Applies a transform function onto a value until the function returns an error.
[Pure]
public static IEnumerable<T> Iterate<T>(T start, Func<T, Result<T>> transform)
Parameters
start
TThe starting value.
transform
Func<T, Result<T>>A function which transforms a value into either a new value or an error.
Returns
- IEnumerable<T>
A sequence of successful values produced by repeatedly applying
transform
onto the previous value starting withstart
until an error is returned.start
will always be the first element.
Type Parameters
T
The type of the value transform.
Ok<T>(T)
Creates a result containing an ok value.
[Pure]
public static Result<T> Ok<T>(T value)
Parameters
value
TThe ok value to create the result from.
Returns
- Result<T>
- A class containing various utility methods, a 'prelude' to the rest of the library.
Type Parameters
T
The type of the ok value.
TryAsync<T>(Func<Task<T>>)
Tries to execute an asynchronous function and return the result. If the function throws an exception, the exception will be returned wrapped in an ExceptionError.
[Pure]
public static Task<Result<T>> TryAsync<T>(Func<Task<T>> function)
Parameters
Returns
- Task<Result<T>>
A result containing the return value of the function or an ExceptionError containing the exception thrown by the function.
Type Parameters
T
The type the function returns.
Try<T>(Func<T>)
Tries to execute a function and return the result. If the function throws an exception, the exception will be returned wrapped in an ExceptionError.
[Pure]
public static Result<T> Try<T>(Func<T> function)
Parameters
function
Func<T>The function to try execute.
Returns
- Result<T>
A result containing the return value of the function or an ExceptionError containing the exception thrown by the function.
Type Parameters
T
The type the function returns.