Skip to content

Commit

Permalink
Applicative and Functor apply and map standardisation
Browse files Browse the repository at this point in the history
  • Loading branch information
louthy committed Oct 12, 2024
1 parent bb47eed commit 85c5f0c
Show file tree
Hide file tree
Showing 49 changed files with 1,207 additions and 959 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,11 @@ public static partial class Prelude
public static Eff<B> map<A, B>(Func<A, B> f, K<Eff, A> ma) =>
ma.As().Map(f);

/// <summary>
/// Functor map operation
/// </summary>
/// <remarks>
/// Unwraps the value within the functor, passes it to the map function `f` provided, and
/// then takes the mapped value and wraps it back up into a new functor.
/// </remarks>
/// <param name="ma">Functor to map</param>
/// <param name="f">Mapping function</param>
/// <returns>Mapped functor</returns>
public static Eff<B> map<A, B>(Func<A, B> f, Eff<A> ma) =>
ma.Map(f);

/// <summary>
/// Applicative action: runs the first applicative, ignores the result, and returns the second applicative
/// </summary>
public static Eff<B> action<A, B>(Eff<A> ma, Eff<B> mb) =>
ma.Kind().Action(mb).As();

/// <summary>
/// Applicative functor apply operation
/// </summary>
/// <remarks>
/// Unwraps the value within the `ma` applicative-functor, passes it to the unwrapped function(s) within `mf`, and
/// then takes the resulting value and wraps it back up into a new applicative-functor.
/// </remarks>
/// <param name="ma">Value(s) applicative functor</param>
/// <param name="mf">Mapping function(s)</param>
/// <returns>Mapped applicative functor</returns>
public static Eff<B> apply<A, B>(Eff<Func<A, B>> mf, K<Eff, A> ma) =>
mf.Kind().Apply(ma).As();
public static Eff<B> action<A, B>(K<Eff, A> ma, K<Eff, B> mb) =>
ma.Action(mb).As();

/// <summary>
/// Applicative functor apply operation
Expand All @@ -60,6 +34,6 @@ public static Eff<B> apply<A, B>(Eff<Func<A, B>> mf, K<Eff, A> ma) =>
/// <param name="ma">Value(s) applicative functor</param>
/// <param name="mf">Mapping function(s)</param>
/// <returns>Mapped applicative functor</returns>
public static Eff<B> apply<A, B>(Eff<Func<A, B>> mf, Eff<A> ma) =>
mf.Kind().Apply(ma).As();
public static Eff<B> apply<A, B>(K<Eff, Func<A, B>> mf, K<Eff, A> ma) =>
mf.Apply(ma).As();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,12 @@ public static partial class Prelude
public static Eff<RT, B> map<RT, A, B>(Func<A, B> f, K<Eff<RT>, A> ma) =>
ma.As().Map(f);

/// <summary>
/// Functor map operation
/// </summary>
/// <remarks>
/// Unwraps the value within the functor, passes it to the map function `f` provided, and
/// then takes the mapped value and wraps it back up into a new functor.
/// </remarks>
/// <param name="ma">Functor to map</param>
/// <param name="f">Mapping function</param>
/// <returns>Mapped functor</returns>
public static Eff<RT, B> map<RT, A, B>(Func<A, B> f, Eff<RT, A> ma) =>
ma.Map(f);

/// <summary>
/// Applicative action: runs the first applicative, ignores the result, and returns the second applicative
/// </summary>
public static Eff<RT, B> action<RT, A, B>(
Eff<RT, A> ma,
Eff<RT, B> mb) =>
public static Eff<RT, B> action<RT, A, B>(K<Eff<RT>, A> ma, K<Eff<RT>, B> mb) =>
ma.Kind().Action(mb).As();

/// <summary>
/// Applicative functor apply operation
/// </summary>
/// <remarks>
/// Unwraps the value within the `ma` applicative-functor, passes it to the unwrapped function(s) within `mf`, and
/// then takes the resulting value and wraps it back up into a new applicative-functor.
/// </remarks>
/// <param name="ma">Value(s) applicative functor</param>
/// <param name="mf">Mapping function(s)</param>
/// <returns>Mapped applicative functor</returns>
public static Eff<RT, B> apply<RT, A, B>(Eff<RT, Func<A, B>> mf, K<Eff<RT>, A> ma) =>
mf.Kind().Apply(ma).As();

/// <summary>
/// Applicative functor apply operation
/// </summary>
/// <remarks>
/// Unwraps the value within the `ma` applicative-functor, passes it to the unwrapped function(s) within `mf`, and
/// then takes the resulting value and wraps it back up into a new applicative-functor.
/// </remarks>
/// <param name="ma">Value(s) applicative functor</param>
/// <param name="mf">Mapping function(s)</param>
/// <returns>Mapped applicative functor</returns>
public static Eff<RT, B> apply<RT, A, B>(Eff<RT, Func<A, B>> mf, Eff<RT, A> ma) =>
mf.Kind().Apply(ma).As();

/// <summary>
/// Applicative functor apply operation
/// </summary>
Expand Down
41 changes: 1 addition & 40 deletions LanguageExt.Core/Effects/IO/IO.Prelude.mapapply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,12 @@ public static partial class Prelude
public static IO<B> map<A, B>(Func<A, B> f, K<IO, A> ma) =>
ma.As().Map(f);

/// <summary>
/// Functor map operation
/// </summary>
/// <remarks>
/// Unwraps the value within the functor, passes it to the map function `f` provided, and
/// then takes the mapped value and wraps it back up into a new functor.
/// </remarks>
/// <param name="ma">Functor to map</param>
/// <param name="f">Mapping function</param>
/// <returns>Mapped functor</returns>
public static IO<B> map<A, B>(Func<A, B> f, IO<A> ma) =>
ma.Map(f);

/// <summary>
/// Applicative action: runs the first applicative, ignores the result, and returns the second applicative
/// </summary>
public static IO<B> action<A, B>(IO<A> ma, IO<B> mb) =>
public static IO<B> action<A, B>(K<IO, A> ma, K<IO, B> mb) =>
ma.Kind().Action(mb).As();

/// <summary>
/// Applicative functor apply operation
/// </summary>
/// <remarks>
/// Unwraps the value within the `ma` applicative-functor, passes it to the unwrapped function(s) within `mf`, and
/// then takes the resulting value and wraps it back up into a new applicative-functor.
/// </remarks>
/// <param name="ma">Value(s) applicative functor</param>
/// <param name="mf">Mapping function(s)</param>
/// <returns>Mapped applicative functor</returns>
public static IO<B> apply<A, B>(IO<Func<A, B>> mf, K<IO, A> ma) =>
mf.Kind().Apply(ma).As();

/// <summary>
/// Applicative functor apply operation
/// </summary>
/// <remarks>
/// Unwraps the value within the `ma` applicative-functor, passes it to the unwrapped function(s) within `mf`, and
/// then takes the resulting value and wraps it back up into a new applicative-functor.
/// </remarks>
/// <param name="ma">Value(s) applicative functor</param>
/// <param name="mf">Mapping function(s)</param>
/// <returns>Mapped applicative functor</returns>
public static IO<B> apply<A, B>(IO<Func<A, B>> mf, IO<A> ma) =>
mf.Kind().Apply(ma).As();

/// <summary>
/// Applicative functor apply operation
/// </summary>
Expand Down
90 changes: 90 additions & 0 deletions LanguageExt.Core/Effects/Pipes/Extensions/Extensions.MapApply.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using LanguageExt.Traits;
using LanguageExt.Pipes;

namespace LanguageExt;

public static partial class ProxyExtensions
{
/// <summary>
/// Functor map operation
/// </summary>
/// <remarks>
/// Unwraps the value within the functor, passes it to the map function `f` provided, and
/// then takes the mapped value and wraps it back up into a new functor.
/// </remarks>
/// <param name="ma">Functor to map</param>
/// <param name="f">Mapping function</param>
/// <returns>Mapped functor</returns>
public static Proxy<UOut, UIn, DIn, DOut, M, B> Map<UOut, UIn, DIn, DOut, M, A, B>(
this Func<A, B> f,
K<Proxy<UOut, UIn, DIn, DOut, M>, A> ma)
where M : Monad<M> =>
ma.As().Map(f);

/// <summary>
/// Functor map operation
/// </summary>
/// <remarks>
/// Unwraps the value within the functor, passes it to the map function `f` provided, and
/// then takes the mapped value and wraps it back up into a new functor.
/// </remarks>
/// <param name="ma">Functor to map</param>
/// <param name="f">Mapping function</param>
/// <returns>Mapped functor</returns>
public static Proxy<UOut, UIn, DIn, DOut, M, B> Map<UOut, UIn, DIn, DOut, M, A, B>(
this Func<A, B> f,
Proxy<UOut, UIn, DIn, DOut, M, A> ma)
where M : Monad<M> =>
ma.Map(f);

/// <summary>
/// Applicative action: runs the first applicative, ignores the result, and returns the second applicative
/// </summary>
public static Proxy<UOut, UIn, DIn, DOut, M, B> Action<UOut, UIn, DIn, DOut, M, A, B>(
this Proxy<UOut, UIn, DIn, DOut, M, A> ma,
K<Proxy<UOut, UIn, DIn, DOut, M>, B> mb)
where M : Monad<M> =>
ma.Kind().Action(mb).As();

/// <summary>
/// Applicative action: runs the first applicative, ignores the result, and returns the second applicative
/// </summary>
public static Proxy<UOut, UIn, DIn, DOut, M, B> Action<UOut, UIn, DIn, DOut, M, A, B>(
this K<Proxy<UOut, UIn, DIn, DOut, M>, A> ma,
K<Proxy<UOut, UIn, DIn, DOut, M>, B> mb)
where M : Monad<M> =>
ma.As().Action(mb);

/// <summary>
/// Applicative functor apply operation
/// </summary>
/// <remarks>
/// Unwraps the value within the `ma` applicative-functor, passes it to the unwrapped function(s) within `mf`, and
/// then takes the resulting value and wraps it back up into a new applicative-functor.
/// </remarks>
/// <param name="ma">Value(s) applicative functor</param>
/// <param name="mf">Mapping function(s)</param>
/// <returns>Mapped applicative functor</returns>
public static Proxy<UOut, UIn, DIn, DOut, M, B> Apply<UOut, UIn, DIn, DOut, M, A, B>(
this Proxy<UOut, UIn, DIn, DOut, M, Func<A, B>> mf,
K<Proxy<UOut, UIn, DIn, DOut, M>, A> ma)
where M : Monad<M> =>
mf.Kind().Apply(ma).As();

/// <summary>
/// Applicative functor apply operation
/// </summary>
/// <remarks>
/// Unwraps the value within the `ma` applicative-functor, passes it to the unwrapped function(s) within `mf`, and
/// then takes the resulting value and wraps it back up into a new applicative-functor.
/// </remarks>
/// <param name="ma">Value(s) applicative functor</param>
/// <param name="mf">Mapping function(s)</param>
/// <returns>Mapped applicative functor</returns>
public static Proxy<UOut, UIn, DIn, DOut, M, B> Apply<UOut, UIn, DIn, DOut, M, A, B>(
this K<Proxy<UOut, UIn, DIn, DOut, M>, Func<A, B>> mf,
K<Proxy<UOut, UIn, DIn, DOut, M>, A> ma)
where M : Monad<M> =>
mf.As().Apply(ma);
}
2 changes: 1 addition & 1 deletion LanguageExt.Core/Effects/Pipes/Extensions/LINQ.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace LanguageExt;

public static class ProxyExtensions
public static partial class ProxyExtensions
{
/// <summary>
/// Monad bind (specialised)
Expand Down
49 changes: 49 additions & 0 deletions LanguageExt.Core/Effects/Pipes/Proxy.Prelude.mapapply.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using LanguageExt.Traits;
using LanguageExt.Pipes;

namespace LanguageExt;

public static partial class Prelude
{
/// <summary>
/// Functor map operation
/// </summary>
/// <remarks>
/// Unwraps the value within the functor, passes it to the map function `f` provided, and
/// then takes the mapped value and wraps it back up into a new functor.
/// </remarks>
/// <param name="ma">Functor to map</param>
/// <param name="f">Mapping function</param>
/// <returns>Mapped functor</returns>
public static Proxy<UOut, UIn, DIn, DOut, M, B> map<UOut, UIn, DIn, DOut, M, A, B>(
Func<A, B> f,
K<Proxy<UOut, UIn, DIn, DOut, M>, A> ma)
where M : Monad<M> =>
ma.As().Map(f);

/// <summary>
/// Applicative action: runs the first applicative, ignores the result, and returns the second applicative
/// </summary>
public static Proxy<UOut, UIn, DIn, DOut, M, B> action<UOut, UIn, DIn, DOut, M, A, B>(
K<Proxy<UOut, UIn, DIn, DOut, M>, A> ma,
K<Proxy<UOut, UIn, DIn, DOut, M>, B> mb)
where M : Monad<M> =>
ma.Action(mb).As();

/// <summary>
/// Applicative functor apply operation
/// </summary>
/// <remarks>
/// Unwraps the value within the `ma` applicative-functor, passes it to the unwrapped function(s) within `mf`, and
/// then takes the resulting value and wraps it back up into a new applicative-functor.
/// </remarks>
/// <param name="ma">Value(s) applicative functor</param>
/// <param name="mf">Mapping function(s)</param>
/// <returns>Mapped applicative functor</returns>
public static Proxy<UOut, UIn, DIn, DOut, M, B> apply<UOut, UIn, DIn, DOut, M, A, B>(
K<Proxy<UOut, UIn, DIn, DOut, M>, Func<A, B>> mf,
K<Proxy<UOut, UIn, DIn, DOut, M>, A> ma)
where M : Monad<M> =>
mf.Apply(ma).As();
}
44 changes: 1 addition & 43 deletions LanguageExt.Core/Effects/StreamT/StreamT.Prelude.mapapply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,13 @@ public static StreamT<M, B> map<M, A, B>(Func<A, B> f, K<StreamT<M>, A> ma)
where M : Monad<M> =>
ma.As().Map(f);

/// <summary>
/// Functor map operation
/// </summary>
/// <remarks>
/// Unwraps the value within the functor, passes it to the map function `f` provided, and
/// then takes the mapped value and wraps it back up into a new functor.
/// </remarks>
/// <param name="ma">Functor to map</param>
/// <param name="f">Mapping function</param>
/// <returns>Mapped functor</returns>
public static StreamT<M, B> map<M, A, B>(Func<A, B> f, StreamT<M, A> ma)
where M : Monad<M> =>
ma.Map(f);

/// <summary>
/// Applicative action: runs the first applicative, ignores the result, and returns the second applicative
/// </summary>
public static StreamT<M, B> action<M, A, B>(StreamT<M, A> ma, StreamT<M, B> mb)
public static StreamT<M, B> action<M, A, B>(K<StreamT<M>, A> ma, K<StreamT<M>, B> mb)
where M : Monad<M> =>
ma.Kind().Action(mb).As();

/// <summary>
/// Applicative functor apply operation
/// </summary>
/// <remarks>
/// Unwraps the value within the `ma` applicative-functor, passes it to the unwrapped function(s) within `mf`, and
/// then takes the resulting value and wraps it back up into a new applicative-functor.
/// </remarks>
/// <param name="ma">Value(s) applicative functor</param>
/// <param name="mf">Mapping function(s)</param>
/// <returns>Mapped applicative functor</returns>
public static StreamT<M, B> apply<M, A, B>(StreamT<M, Func<A, B>> mf, K<StreamT<M>, A> ma)
where M : Monad<M> =>
mf.Kind().Apply(ma).As();

/// <summary>
/// Applicative functor apply operation
/// </summary>
/// <remarks>
/// Unwraps the value within the `ma` applicative-functor, passes it to the unwrapped function(s) within `mf`, and
/// then takes the resulting value and wraps it back up into a new applicative-functor.
/// </remarks>
/// <param name="ma">Value(s) applicative functor</param>
/// <param name="mf">Mapping function(s)</param>
/// <returns>Mapped applicative functor</returns>
public static StreamT<M, B> apply<M, A, B>(StreamT<M, Func<A, B>> mf, StreamT<M, A> ma)
where M : Monad<M> =>
mf.Kind().Apply(ma).As();

/// <summary>
/// Applicative functor apply operation
/// </summary>
Expand Down
Loading

0 comments on commit 85c5f0c

Please sign in to comment.