Skip to content

Commit

Permalink
Add filter, map, and mapM pipe creation functions
Browse files Browse the repository at this point in the history
Introduce three new utility functions: `filter` for filtering stream values based on a predicate, `map` for transforming stream values using a mapping function, and `mapM` for mapping stream values within a monadic context. These additions enhance the pipe composition capabilities.
  • Loading branch information
louthy committed Feb 5, 2025
1 parent e709ccb commit 514bf36
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions Samples/PipesExamples/PipeT/PipeT.Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,48 @@ public static PipeT<IN, OUT, M, Unit> yieldAll<M, IN, OUT>(IAsyncEnumerable<OUT>
/// <summary>
/// Await a value from upstream
/// </summary>
/// <typeparam name="M">Lifted monad type</typeparam>
/// <typeparam name="IN">Stream value to consume</typeparam>
/// <typeparam name="OUT">Stream value to produce</typeparam>
/// <typeparam name="M">Lifted monad type</typeparam>
/// <returns></returns>
/// <returns>Pipe</returns>
public static PipeT<IN, OUT, M, IN> awaiting<M, IN, OUT>()
where M : Monad<M> =>
new PipeTAwait<IN, OUT, M, IN>(pure<IN, OUT, M, IN>);

/// <summary>
/// Create a pipe that filters out values that return `false` when applied to a predicate function
/// </summary>
/// <param name="f">Predicate function</param>
/// <typeparam name="M">Lifted monad type</typeparam>
/// <typeparam name="A">Stream value to consume and produce</typeparam>
/// <returns>Pipe</returns>
public static PipeT<A, A, M, Unit> filter<M, A>(Func<A, bool> f)
where M : Monad<M> =>
awaiting<M, A, A>().Bind(x => f(x) ? yield<M, A, A>(x) : pure<A, A, M, Unit>(default));

/// <summary>
/// Create a pipe from a mapping function
/// </summary>
/// <param name="f">Mapping function</param>
/// <typeparam name="M">Lifted monad type</typeparam>
/// <typeparam name="IN">Stream value to consume</typeparam>
/// <typeparam name="OUT">Stream value to produce</typeparam>
/// <returns>Pipe</returns>
public static PipeT<IN, OUT, M, Unit> map<M, IN, OUT>(Func<IN, OUT> f)
where M : Monad<M> =>
awaiting<M, IN, OUT>().Bind(x => yield<M, IN, OUT>(f(x)));

/// <summary>
/// Create a pipe from a mapping function
/// </summary>
/// <param name="f">Mapping function</param>
/// <typeparam name="M">Lifted monad type</typeparam>
/// <typeparam name="IN">Stream value to consume</typeparam>
/// <typeparam name="OUT">Stream value to produce</typeparam>
/// <returns>Pipe</returns>
public static PipeT<IN, OUT, M, Unit> mapM<M, IN, OUT>(Func<IN, K<M, OUT>> f)
where M : Monad<M> =>
awaiting<M, IN, OUT>().Bind(x => liftM<IN, OUT, M, OUT>(f(x)).Bind(yield<M, IN, OUT>));

/// <summary>
/// Create a pipe that simply returns a bound value without yielding anything
Expand Down

0 comments on commit 514bf36

Please sign in to comment.