-
-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,386 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace LanguageExt.DSL; | ||
|
||
static class IODsl | ||
{ | ||
public static IODsl<A> Lift<A>(Func<EnvIO, A> f) => new IOLiftSync<A>(f); | ||
public static IODsl<A> Lift<A>(Func<EnvIO, Task<A>> f) => new IOLiftAsync<A>(f); | ||
} | ||
|
||
abstract record IODsl<A> | ||
{ | ||
public abstract IODsl<B> Map<B>(Func<A, B> f); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace LanguageExt.DSL; | ||
|
||
record IOLiftAsync<A>(Func<EnvIO, Task<A>> F) : IODsl<A> | ||
{ | ||
public override IODsl<B> Map<B>(Func<A, B> f) => | ||
new IOLiftAsync<B>(async x => f(await F(x).ConfigureAwait(false))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace LanguageExt.DSL; | ||
|
||
record IOLiftSync<A>(Func<EnvIO, A> F) : IODsl<A> | ||
{ | ||
public override IODsl<B> Map<B>(Func<A, B> f) => | ||
new IOLiftSync<B>(x => f(F(x))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using LanguageExt.DSL; | ||
using LanguageExt.Traits; | ||
|
||
namespace LanguageExt; | ||
|
||
record IOBind<A>(IODsl<IO<A>> Value) : IO<A> | ||
{ | ||
public override IO<B> Map<B>(Func<A, B> f) => | ||
new IOBind<B>(Value.Map(fa => fa.Map(f))); | ||
|
||
public override IO<B> Bind<B>(Func<A, K<IO, B>> f) => | ||
new IOBind<B>(Value.Map(mx => mx.Bind(f))); | ||
|
||
public override IO<B> ApplyBack<B>(K<IO, Func<A, B>> mf) => | ||
mf switch | ||
{ | ||
IOPure<Func<A, B>> (var f) => new IOBind<B>(Value.Map(fa => fa.Map(f))), | ||
IOPureAsync<Func<A, B>> f => new IOBind<B>(Value.Map(fa => fa.ApplyBack(f))), | ||
IOFail<Func<A, B>> (var v) => new IOFail<B>(v), | ||
IOBind<Func<A, B>> (var f) => new IOBind<B>(f.Map(x => x.Apply(this).As())), | ||
IOCatch<Func<A, B>> mc => new IOBind<B>(Value.Map(fa => fa.ApplyBack(mc))), | ||
_ => throw new InvalidOperationException() | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using LanguageExt.Traits; | ||
using LanguageExt.Common; | ||
using System.Threading.Tasks; | ||
|
||
namespace LanguageExt; | ||
|
||
abstract record IOCatch<A> : IO<A> | ||
{ | ||
public abstract ValueTask<IO<A>> Invoke(EnvIO envIO); | ||
} | ||
|
||
record IOCatch<X, A>( | ||
K<IO, X> Operation, | ||
Func<Error, bool> Predicate, | ||
Func<Error, K<IO, X>> Failure, | ||
Func<X, IO<A>> Next) : IOCatch<A> | ||
{ | ||
public override IO<B> Map<B>(Func<A, B> f) => | ||
new IOCatch<X, B>(Operation, Predicate, Failure, x => Next(x).Map(f)); | ||
|
||
public override IO<B> Bind<B>(Func<A, K<IO, B>> f) => | ||
new IOCatch<X, B>(Operation, Predicate, Failure, x => Next(x).Bind(f)); | ||
|
||
public override IO<B> ApplyBack<B>(K<IO, Func<A, B>> f) => | ||
new IOCatch<X, B>(Operation, Predicate, Failure, x => Next(x).ApplyBack(f)); | ||
|
||
public override async ValueTask<IO<A>> Invoke(EnvIO envIO) | ||
{ | ||
try | ||
{ | ||
var x = await Operation.As().RunAsync(envIO); | ||
return Next(x); | ||
} | ||
catch(Exception e) | ||
{ | ||
var err = Error.New(e); | ||
if (Predicate(err)) | ||
{ | ||
var x = await Failure(e).As().RunAsync(envIO); | ||
return Next(x); | ||
} | ||
throw; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using LanguageExt.Common; | ||
using LanguageExt.DSL; | ||
using LanguageExt.Traits; | ||
|
||
namespace LanguageExt; | ||
|
||
record IOFail<A>(Error Value) : IO<A> | ||
{ | ||
public override IO<B> Map<B>(Func<A, B> f) => | ||
new IOFail<B>(Value); | ||
|
||
public override IO<B> Bind<B>(Func<A, K<IO, B>> f) => | ||
IO.fail<B>(Value); | ||
|
||
public override IO<B> ApplyBack<B>(K<IO, Func<A, B>> mf) => | ||
mf switch | ||
{ | ||
IOPure<Func<A, B>> => new IOFail<B>(Value), | ||
IOPureAsync<Func<A, B>> => new IOFail<B>(Value), | ||
IOFail<Func<A, B>> (var v) => new IOFail<B>(v + Value), | ||
IOBind<Func<A, B>> (var f) => new IOBind<B>(f.Map(x => x.Apply(this).As())), | ||
IOCatch<Func<A, B>> => new IOFail<B>(Value), | ||
_ => throw new InvalidOperationException() | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using LanguageExt.DSL; | ||
using LanguageExt.Traits; | ||
|
||
namespace LanguageExt; | ||
|
||
record IOPure<A>(A Value) : IO<A> | ||
{ | ||
public override IO<B> Map<B>(Func<A, B> f) => | ||
new IOPure<B>(f(Value)); | ||
|
||
public override IO<B> Bind<B>(Func<A, K<IO, B>> f) => | ||
f(Value).As(); | ||
|
||
public override IO<B> ApplyBack<B>(K<IO, Func<A, B>> mf) => | ||
mf switch | ||
{ | ||
IOPure<Func<A, B>> (var f) => new IOPure<B>(f(Value)), | ||
IOPureAsync<Func<A, B>> (var tf) => new IOPureAsync<B>(tf.Map(f => f(Value))), | ||
IOFail<Func<A, B>> (var v) => new IOFail<B>(v), | ||
IOBind<Func<A, B>> (var f) => new IOBind<B>(f.Map(x => x.Apply(this).As())), | ||
IOCatch<Func<A, B>> mc => mc.Map(f => f(Value)), | ||
_ => throw new InvalidOperationException() | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using LanguageExt.DSL; | ||
using LanguageExt.Traits; | ||
|
||
namespace LanguageExt; | ||
|
||
record IOPureAsync<A>(Task<A> Value) : IO<A> | ||
{ | ||
public override IO<B> Map<B>(Func<A, B> f) => | ||
new IOPureAsync<B>(Value.Map(f)); | ||
|
||
public override IO<B> Bind<B>(Func<A, K<IO, B>> f) => | ||
new IOPureAsync<K<IO, B>>(Value.Map(f)).Bind(Prelude.identity); | ||
|
||
public override IO<B> ApplyBack<B>(K<IO, Func<A, B>> mf) => | ||
mf switch | ||
{ | ||
IOPure<Func<A, B>> (var f) => new IOPureAsync<B>(Apply(f, Value)), | ||
IOPureAsync<Func<A, B>> (var tf) => new IOPureAsync<B>(Apply(tf, Value)), | ||
IOFail<Func<A, B>> (var v) => new IOFail<B>(v), | ||
IOBind<Func<A, B>> (var f) => new IOBind<B>(f.Map(f1 => f1.Apply(this).As())), | ||
IOCatch<Func<A, B>> mc => mc.Bind(f => new IOPureAsync<B>(Value.Map(f))), | ||
_ => throw new InvalidOperationException() | ||
}; | ||
|
||
static async Task<B> Apply<B>(Func<A, B> f, Task<A> ta) | ||
{ | ||
var a = await ta; | ||
return f(a); | ||
} | ||
|
||
static async Task<B> Apply<B>(Task<Func<A, B>> f, Task<A> a) | ||
{ | ||
await Task.WhenAll(f, a); | ||
return f.Result(a.Result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.