Skip to content

Instantly share code, notes, and snippets.

@ericrey85
Last active October 26, 2023 10:40
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ericrey85/da9671a22234ef981e5ee3653face4af to your computer and use it in GitHub Desktop.
Save ericrey85/da9671a22234ef981e5ee3653face4af to your computer and use it in GitHub Desktop.

AsyncLazyPipeline Monad (C#)

I had the need to create an async pipeline that consisted of actions (methods) composing over each other. I also wanted the construction of this pipeline to not generate any side effects, including Exceptions, Object state mutations, IO operations, etc. I had the need for function composition and Monadic Composition (Kleisli Composition), and did not want to use Task.Run, I do not like doing that in ASP.NET applications.

Having worked in JavaScript with the IO Monad, I decided to do something similar in C# that would fulfill my needs. In here, I leave the implementation I've come up so far with, and below, a simple code example that shows how to use it, plus some other code for the sake of completeness.

public class AsyncLazyPipeline<TSource>
{
private Func<Task<TSource>> Expression { get; }
public AsyncLazyPipeline(Func<Task<TSource>> expression)
{
Expression = expression;
}
public Task<TSource> Flatten() => Expression();
public AsyncLazyPipeline<TDestination> Select<TDestination>(Func<TSource, Task<TDestination>> fn)
{
async Task<TDestination> CombineExpressions()
{
var result = await Expression();
return await fn(result);
}
return CreatePipeLine.With(CombineExpressions);
}
public AsyncLazyPipeline<TDestination> Select<TDestination>(Func<TSource, TDestination> fn)
{
async Task<TDestination> CombineExpressions()
{
var result = await Expression();
return fn(result);
}
return CreatePipeLine.With(CombineExpressions);
}
public AsyncLazyPipeline<TDestination> SelectMany<TDestination>(Func<TSource, AsyncLazyPipeline<TDestination>> fn)
{
async Task<TDestination> CombineExpressions()
{
var result = await Expression();
return await fn(result).Flatten();
}
return CreatePipeLine.With(CombineExpressions);
}
public AsyncLazyPipeline<TDestination> SelectMany<TIntermediate, TDestination>(
Func<TSource, AsyncLazyPipeline<TIntermediate>> fn, Func<TSource, TIntermediate, TDestination> select)
=> SelectMany(a => fn(a).Select(b => select(a, b)));
}
public static class CreatePipeLine
{
public static AsyncLazyPipeline<TDestination> With<TDestination>(Func<Task<TDestination>> fn)
=> new AsyncLazyPipeline<TDestination>(fn);
public static AsyncLazyPipeline<TDestination> With<TDestination>(Func<TDestination> fn)
=> new AsyncLazyPipeline<TDestination>(() => fn().AsTask());
public static AsyncLazyPipeline<TDestination> Return<TDestination>(TDestination value)
=> new AsyncLazyPipeline<TDestination>(() => value.AsTask());
}
public interface IFinalNoteAppender
{
AsyncLazyPipeline<string> AppendFinalText(string value);
}
public class FinalNoteAppender : IFinalNoteAppender
{
public AsyncLazyPipeline<string> AppendFinalText(string value)
=> CreatePipeLine.With(() => $"{value} final text.".AsTask());
}
public interface IOperations
{
Task<string> GetFirstFileNote();
Task<string> CombineWithSecondFileNote(string firstNote);
}
public class Item
{
public string Notes { get; private set; }
public Item AddNotes(string notes)
{
Notes = notes;
return this;
}
}
public class ItemService
{
private IFinalNoteAppender NoteAppender { get; }
private IOperations Operations { get; }
public ItemService(IOperations operations, IFinalNoteAppender noteAppender)
{
NoteAppender = noteAppender;
Operations = operations;
}
public Task<Item> AddNotesToItem()
{
var combineNotes = CreatePipeLine
.With(Operations.GetFirstFileNote)
.Select(Operations.CombineWithSecondFileNote);
var appendLastNote = from notes in combineNotes
from finalNote in NoteAppender.AppendFinalText(notes)
select finalNote;
var combinedOperations = appendLastNote.Select(new Item().AddNotes);
return combinedOperations.Flatten();
}
}
public static class ObjectExtensions
{
public static Task<T> AsTask<T>(this T that) => Task.FromResult(that);
}
@damiensawyer
Copy link

Thanks for this. Very cool.

@ericrey85
Copy link
Author

Thank you @damiensawyer. I am glad you find it cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment