A collection of extension methods and helpers which extend the use of LanguageExt
.
This project is not affiliated with LanguageExt and asserts no claims upon its intellectual property.
Install LanguageExt.Extras as a NuGet package, via an IDE package manager, or using the command-line instructions at nuget.org.
LanguageExt.Extras extends existing LanguageExt types. As such, you'll need both LanguageExt and LanguageExt.Extras in scope.
All API examples shown below assume the following using
statements:
using LanguageExt;
using LanguageExt.Common;
using Jds.LanguageExt.Extras;
Either<TLeft, TRight>.BindLeftAsync<TLeft, TRight, TLeft2>(Func<TLeft, Task<Either<TLeft2, TRight>>> func)
- Executes
func
, which returns aTask<Either<TLeft2, TRight>>
, when the either is aTLeft
.
- Executes
Either<TLeft, TRight>.Filter<TLeft, TRight>(Func<TRight, bool> filter, Func<TRight, TLeft> onFalse)
- Filters right values by executing
filter
. If it returnstrue
, the existing value is returned. If it returnsfalse
, then it executesonFalse
to create aTLeft
.
- Filters right values by executing
Either<TLeft, TRight>.FilterAsync<TLeft, TRight>(Func<TRight, Task<bool>> filter, Func<TRight, TLeft> onFalse)
- Filters right values by executing
filter
. If it returnstrue
, the existing value is returned. If it returnsfalse
, then it executesonFalse
to create aTLeft
.
- Filters right values by executing
Either<TLeft, TRight>.FilterAsync<TLeft, TRight>(Func<TRight, Task<bool>> filter, Func<TRight, Task<TLeft>> onFalse)
- Filters right values by executing
filter
. If it returnstrue
, the existing value is returned. If it returnsfalse
, then it executesonFalse
to create aTLeft
.
- Filters right values by executing
Either<TLeft, TRight>.MapLeftAsync<TLeft, TRight, TLeft2>(Func<TLeft, Task<TLeft2>> func)
- Executes
func
, which returns aTask<TLeft2>
, when the either is aTLeft
.
- Executes
Either<TLeft, TRight>.Tap<TLeft, TRight>(Action<TRight> onRight, Action<TLeft> onLeft)
- Executes a side effect, e.g., logging, based upon the state of the either. The current value is returned unchanged.
Either<TLeft, TRight>.TapAsync<TLeft, TRight>(Func<TRight, Task> onSuccess, Func<TLeft, Task> onFailure)
- Executes an asynchronous side effect, e.g., dispatching a status notification via
HttpClient
, based upon the state of the either. The current value is returned unchanged.
- Executes an asynchronous side effect, e.g., dispatching a status notification via
Either<TLeft, TRight>.TapLeft<TLeft, TRight>(Action<TLeft> onFailure)
- Executes a side effect, e.g., logging, when the either is a left. The current value is returned unchanged.
Either<TLeft, TRight>.TapLeftAsync<TLeft, TRight>(Func<TLeft, Task> onFailure)
- Executes an asynchronous side effect, e.g., dispatching a status notification via
HttpClient
, when the either is a left. The current value is returned unchanged.
- Executes an asynchronous side effect, e.g., dispatching a status notification via
Either<TLeft, TRight>.TapRight<TLeft, TRight>(Action<TRight> onSuccess)
- Executes a side effect, e.g., logging, when the either is a right. The current value is returned unchanged.
Either<TLeft, TRight>.TapRightAsync<TLeft, TRight>(Func<TRight, Task> onSuccess)
- Executes an asynchronous side effect, e.g., dispatching a status notification via
HttpClient
, when the either is a right. The current value is returned unchanged.
- Executes an asynchronous side effect, e.g., dispatching a status notification via
Either<TLeft, TRight>.ToResult<TLeft, TRight>(Func<TLeft, Exception> ifLeft)
- Converts the
Either<TLeft, TRight>
into aResult<TRight>
.
- Converts the
Either<TLeft, TRight>.ToResult<TLeft, TRight>() where TLeft : Exception
- Converts the
Either<TLeft, TRight>
into aResult<TRight>
.
- Converts the
Option<TSome>.Tap(Action<TSome> ifSome, Action ifNone)
- Executes a side effect, e.g., logging, based upon the state of the option. The current value is returned unchanged.
Option<TSome>.TapAsync(Func<TSome,Task> ifSome, Func<Task> ifNone)
- Executes an asynchronous side effect, e.g., dispatching a status notification via
HttpClient
, based upon the state of the option. The current value is returned unchanged.
- Executes an asynchronous side effect, e.g., dispatching a status notification via
Option<TSome>.TapNone(Action ifNone)
- Executes a side effect, e.g., logging, if the option is None. The current value is returned unchanged.
Option<TSome>.TapNoneAsync(Func<Task> ifNone)
- Executes an asynchronous side effect, e.g., dispatching a status notification via
HttpClient
, if the option is None. The current value is returned unchanged.
- Executes an asynchronous side effect, e.g., dispatching a status notification via
Option<TSome>.TapSome(Action<TSome> ifSome)
- Executes a side effect, e.g., logging, if the option is Some. The current value is returned unchanged.
Option<TSome>.TapSomeAsync(Func<TSome,Task> ifSome)
- Executes an asynchronous side effect, e.g., dispatching a status notification via
HttpClient
, if the option is Some. The current value is returned unchanged.
- Executes an asynchronous side effect, e.g., dispatching a status notification via
Option<TSome?>
.ToNonNullable()- Converts the nullable
TSome
generic argument to a non-nullableTSome
generic argument, e.g.,Option<string?>
returnsOption<string>
. The existing value is maintained unchanged. - This method assumes execution within a
#nullable
context (i.e., in a project where nullable reference types are enabled).
- Converts the nullable
Result<TSuccess>.Bind<TSuccess, TNewSuccess>(Func<TSuccess, Result<TNewSuccess>> func)
- Executes
func
, which returnsResult<TNewSuccess>
, when the result is a success.
- Executes
Result<TSuccess>.BindAsync<TSuccess, TNewSuccess>(Func<TSuccess, Task<Result<TNewSuccess>>> func)
- Executes
func
, which returnsTask<Result<TNewSuccess>>
, when the result is a success.
- Executes
Result<TSuccess>.BindFailure<TSuccess>(Func<Exception, Result<TSuccess>> func)
- Executes
func
, which returnsResult<TSuccess>
, when the result is a failure.
- Executes
Result<TSuccess>.BindFailureAsync<TSuccess>(Func<Exception, Task<Result<TSuccess>>> func)
- Executes
func
, which returnsTask<Result<TSuccess>>
, when the result is a failure.
- Executes
Result<TSuccess>.Filter<TSuccess>(Func<TSuccess, bool> filter, Func<TSuccess, Exception> onFalse)
- Filters
TSuccess
values by executingfilter
. If it returnstrue
, the existing value is returned. If it returnsfalse
, then it executesonFalse
to create anException
.
- Filters
Result<TSuccess>.FilterAsync<TSuccess>(Func<TSuccess, Task<bool>> filter, Func<TSuccess, Exception> onFalse)
- Filters
TSuccess
values by executingfilter
. If it returnstrue
, the existing value is returned. If it returnsfalse
, then it executesonFalse
to create anException
.
- Filters
Result<TSuccess>.FilterAsync<TSuccess>(Func<TSuccess, Task<bool>> filter, Func<TSuccess, Task<Exception>> onFalse)
- Filters
TSuccess
values by executingfilter
. If it returnstrue
, the existing value is returned. If it returnsfalse
, then it executesonFalse
to create anException
.
- Filters
Result<TSucess>.IfFailThrow<TSuccess>()
- Throws an
InvalidOperationException
if theResult<TSucess>
is anException
, returningTSuccess
upon success.
- Throws an
Result<TSuccess>.MapFailure<TSuccess, TFailure>(Func<Exception, TFailure> func) where TFailure : Exception
- Executes
func
, which returns anException
, when the result is a failure.
- Executes
Result<TSuccess>.MapFailureAsync<TSuccess, TFailure>(Func<Exception, Task<TFailure>> func) where TFailure : Exception
- Executes
func
, which returns aTask<Exception>
, when the result is a failure.
- Executes
Result<TSuccess>.MapSafe<TSuccess, TNewSuccess>(Func<TSuccess, TNewSuccess> func)
- Executes
func
, which returnsTNewSuccess
, when the result is a success. Exceptions are caught and returned as a failure. (I.e., this simplifiesresult.Bind(value => Prelude.Try(() => func(value)).Try())
)
- Executes
Result<TSuccess>.MapSafeAsync<TSuccess, TNewSuccess>(Func<TSuccess, Task<TNewSuccess>> func)
- Executes
func
, which returnsTask<TNewSuccess>
, when the result is a success. Exceptions are caught and returned as a failure. (I.e., this simplifiesresult.BindAsync(value => Prelude.TryAsync(() => func(value)).Try())
)
- Executes
Result<TSuccess>.Tap<TSuccess>(Action<TSuccess> onSuccess, Action<Exception> onFailure)
- Executes a side effect, e.g., logging, based upon the state of the result. The current value is returned unchanged.
Result<TSuccess>.TapAsync<TSuccess>(Func<TSuccess, Task> onSuccess, Func<Exception, Task> onFailure)
- Executes an asynchronous side effect, e.g., dispatching a status notification via
HttpClient
, based upon the state of the result. The current value is returned unchanged.
- Executes an asynchronous side effect, e.g., dispatching a status notification via
Result<TSuccess>.TapFailure<TSuccess>(Action<Exception> onFailure)
- Executes a side effect, e.g., logging, when the result is a failure. The current value is returned unchanged.
Result<TSuccess>.TapFailureAsync<TSuccess>(Func<Exception, Task> onFailure)
- Executes an asynchronous side effect, e.g., dispatching a status notification via
HttpClient
, when the result is a failure. The current value is returned unchanged.
- Executes an asynchronous side effect, e.g., dispatching a status notification via
Result<TSuccess>.TapSuccess<TSuccess>(Action<TSuccess> onSuccess)
- Executes a side effect, e.g., logging, when the result is a success. The current value is returned unchanged.
Result<TSuccess>.TapSuccessAsync<TSuccess>(Func<TSuccess, Task> onSuccess)
- Executes an asynchronous side effect, e.g., dispatching a status notification via
HttpClient
, when the result is a success. The current value is returned unchanged.
- Executes an asynchronous side effect, e.g., dispatching a status notification via
Result<TSuccess>.ToEither<TSuccess>()
- Converts the
Result<TSuccess>
into anEither<Exception, TSuccess>
.
- Converts the
Try<TSuccess>.Filter<TSuccess>(Func<TSuccess, bool> filter, Func<TSuccess, Exception> onFalse)
- Filters
TSuccess
results by executingfilter
. If it returnstrue
, the existing value is returned. If it returnsfalse
, then it executesonFalse
to create anException
. Memoizes successful results.
- Filters
Try<TSuccess>.Tap<TSuccess>(Action<TSuccess> onSuccess, Action<Exception> onFailure)
- Executes a side effect, e.g., logging, based upon the state of the result. The current value is returned unchanged.
Try<TSuccess>.TapFailure<TSuccess>(Action<Exception> onFailure)
- Executes a side effect, e.g., logging, when the result is a failure. The current value is returned unchanged.
Try<TSuccess>.TapSuccess<TSuccess>(Action<TSuccess> onSuccess)
- Executes a side effect, e.g., logging, when the result is a success. The current value is returned unchanged.
TryAsync<TSuccess>.Filter<TSuccess>(Func<TSuccess, bool> filter, Func<TSuccess, Exception> onFalse)
- Filters
TSuccess
results by executingfilter
. If it returnstrue
, the existing value is returned. If it returnsfalse
, then it executesonFalse
to create anException
. Memoizes successful results.
- Filters
TryAsync<TSuccess>.FilterAsync<TSuccess>(Func<TSuccess, Task<bool>> filter, Func<TSuccess, Exception> onFalse)
- Filters
TSuccess
results by executingfilter
. If it returnstrue
, the existing value is returned. If it returnsfalse
, then it executesonFalse
to create anException
. Memoizes successful results.
- Filters
TryAsync<TSuccess>.Filter<TSuccess>(Func<TSuccess, Exception> ifCanceled, CancellationToken cancellationToken)
- Filters based upon
cancellationToken
. If it is not cancelled, the existing value is returned. If it is cancelled, then it executesifCanceled
to create anException
.
- Filters based upon
TryAsync<TSuccess>.Filter<TSuccess>(CancellationToken cancellationToken)
- Filters based upon
cancellationToken
. If it is not cancelled, the existing value is returned. If it is cancelled, then it creates anOperationCanceledException
withcancellationToken
.
- Filters based upon
TryAsync<TSuccess>.Tap<TSuccess>(Action<TSuccess> onSuccess, Action<Exception> onFailure)
- Executes a side effect, e.g., logging, based upon the state of the result. The current value is returned unchanged.
TryAsync<TSuccess>.Tap<TSuccess>(Func<TSuccess, Task> onSuccess, Func<Exception, Task> onFailure)
- Executes an asynchronous side effect, e.g., dispatching a status notification via
HttpClient
, based upon the state of the result. The current value is returned unchanged.
- Executes an asynchronous side effect, e.g., dispatching a status notification via
TryAsync<TSuccess>.TapFailure<TSuccess>(Action<Exception> onFailure)
- Executes a side effect, e.g., logging, when the result is a failure. The current value is returned unchanged.
TryAsync<TSuccess>.TapFailure<TSuccess>(Func<Exception, Task> onFailure)
- Executes an asynchronous side effect, e.g., dispatching a status notification via
HttpClient
, when the result is a failure. The current value is returned unchanged.
- Executes an asynchronous side effect, e.g., dispatching a status notification via
TryAsync<TSuccess>.TapSuccess<TSuccess>(Action<TSuccess> onSuccess)
- Executes a side effect, e.g., logging, when the result is a success. The current value is returned unchanged.
TryAsync<TSuccess>.TapSuccess<TSuccess>(Func<TSuccess, Task> onSuccess)
- Executes an asynchronous side effect, e.g., dispatching a status notification via
HttpClient
, when the result is a success. The current value is returned unchanged.
- Executes an asynchronous side effect, e.g., dispatching a status notification via
HttpClient.TryDeleteAsync(Uri? uri, CancellationToken cancellationToken = default)
- Tries to asynchronously execute
.DeleteAsync()
, returning aTryAsync<HttpResponseMessage>
.
- Tries to asynchronously execute
HttpClient.TryGetAsync(Uri? uri, HttpCompletionOption completionOption, CancellationToken cancellationToken = default)
HttpClient.TryGetAsync(Uri? uri, CancellationToken cancellationToken = default)
- Tries to asynchronously execute
.GetAsync()
, returning aTryAsync<HttpResponseMessage>
.
- Tries to asynchronously execute
HttpClient.TryGetForJsonAsync(Uri? uri, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
- Tries to asynchronously execute
.GetAsync()
and deserialize the response body as JSON, returning aTryAsync<DeserializedHttpResponseMessage<T>>
.
- Tries to asynchronously execute
HttpClient.TryPostAsync(Uri? uri, HttpContent? content, CancellationToken cancellationToken = default)
- Tries to asynchronously execute
.PostAsync()
, returning aTryAsync<HttpResponseMessage>
.
- Tries to asynchronously execute
HttpClient.TryPostForJsonAsync(Uri? uri, HttpContent? content, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
- Tries to asynchronously execute
.PostAsync()
and deserialize the response body as JSON, returning aTryAsync<DeserializedHttpResponseMessage<T>>
.
- Tries to asynchronously execute
HttpClient.TryPutAsync(Uri? uri, HttpContent? content, CancellationToken cancellationToken = default)
- Tries to asynchronously execute
.PutAsync()
, returning aTryAsync<HttpResponseMessage>
.
- Tries to asynchronously execute
HttpClient.TryPutForJsonAsync<T>(Uri? uri, HttpContent? content, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
- Tries to asynchronously execute
.PutAsync()
and deserialize the response body as JSON, returning aTryAsync<DeserializedHttpResponseMessage<T>>
.
- Tries to asynchronously execute
HttpClient.TrySendAsync(HttpRequestMessage requestMessage, HttpCompletionOption completionOption, CancellationToken cancellationToken = default)
HttpClient.TrySendAsync(HttpRequestMessage requestMessage, CancellationToken cancellationToken = default)
- Tries to asynchronously execute
.SendAsync()
, returning aTryAsync<HttpResponseMessage>
.
- Tries to asynchronously execute
HttpClient.TrySendForJsonAsync<T>(HttpRequestMessage requestMessage, JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default)
- Tries to asynchronously execute
.SendAsync()
and deserialize the response body as JSON, returning aTryAsync<DeserializedHttpResponseMessage<T>>
.
- Tries to asynchronously execute
HttpContent.TryReadAsStringAsync(CancellationToken cancellationToken = default)
- Tries to asynchronously read the
HttpContent
as astring
, returning aTryAsync<string>
- Tries to asynchronously read the
HttpResponseMessage.TryReadContentAsStringAsync(CancellationToken cancellationToken = default)
- Tries to asynchronously read the message's
.Content
as astring
, returning aTryAsync<string>
.
- Tries to asynchronously read the message's
HttpResponseMessage.TryDeserializeContentAsJsonAsync<T>(JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) where T : notnull
- Tries to asynchronously read the message's
.Content
as a JSONstring
and deserialize it asT
, returning aTryAsync<T>
.
- Tries to asynchronously read the message's
HttpResponseMessage.TryReadContentAsJsonAsync<T>(JsonSerializerOptions? jsonSerializerOptions = null, CancellationToken cancellationToken = default) where T : notnull
- Tries to asynchronously read the message's
.Content
as a JSONstring
and deserialize it asT
, returning all components,TryAsync<(HttpResponseMessage Message, Result<string> Content, Result<T> Body)>
. - This method is extremely useful in ASP.NET integration tests. It succinctly parses API responses and maintains full access to the
HttpResponseMessage
and anyException
s, supporting thorough testing of success and failure paths.
- Tries to asynchronously read the message's