New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to "Tee" (similar to "Do") for side effects #602
Comments
In FP, when you want logging (or telemetry, or any other kind of indirect output), you should think of the language-ext/Samples/TestBed/Program.cs Line 169 in 509baee
The only difference from what you were asking for is that Writer doesn't perform a side effect; instead it accumulates what you tell in a monoid(commonly a Seq ), which keeps your operation pure. And you use that pure operation as part of a impure-pure-impure sandwich, where the final impure operation takes the .Output of the writer result and sends it to the logger.
|
@gwintering thanks for the reply. This doesn't immediately "click" and may take me some time to comprehend. If you have any articles that address this in more depth please add them here. I want to get a solid understanding of how this works in general for FP; viz. how an idiomatic solution looks in F# and Haskel. |
@jltrem There isn't anything built into lang-ext for that. You could build one yourself relatively easily: public static class ObjExt
{
public static A Tee<A>(this A self, Func<A, Unit> f)
{
f(self);
return self;
}
public static A Tee<A>(this A self, Action<A> f)
{
f(self);
return self;
}
} |
Similar issue: #228 |
Super @gwintering -- thanks for finding that discussion. @louthy that I'm curious how you two typically handle logging in your sw dev... using a Do/Tee approach? Monadic writer? Plain old imperative? If this isn't the appropriate place to discuss, feel free to ping me on Twitter with the same handle: |
@jltrem If I have an suitably complex sub-system then I usually build a bespoke monad that captures the rules of that sub-system. Whether it's state, logging, environment (config, etc.), or control flow. My code then becomes a set of sub-systems, some that wrap others. A good example might be a compiler, where I will have sub-system monads for parsing, type-inference, code-gen, etc. that would be wrapped by a compiler monad that works with them all. A sub-system monad shape would be defined like so: public delegate Out<A> Subsystem<A>(); I've picked the name This monad takes no input and returns an public struct Error
{
public readonly string Message;
public readonly Option<Exception> Exception;
Error(string message, Exception exception)
{
Message = message;
Exception = exception;
}
public static Error FromString(string message) =>
new Error(message, null);
public static Error FromException(Exception ex) =>
new Error(ex.Message, ex);
} And so the public struct Out<A>
{
public readonly A Value;
public readonly Seq<string> Output;
public readonly Option<Error> Error;
Out(A value, Seq<string> output, Option<Error> error)
{
Value = value;
Output = output;
Error = error;
}
public static Out<A> FromValue(A value) =>
new Out<A>(value, Empty, None);
public static Out<A> FromValue(A value, Seq<string> output) =>
new Out<A>(value, output, None);
public static Out<A> FromError(Error error) =>
new Out<A>(default, Empty, error);
public static Out<A> FromError(Error error, Seq<string> output) =>
new Out<A>(default, output, error);
public static Out<A> FromError(string message) =>
new Out<A>(default, Empty, SubsystemTest.Error.FromString(message));
public static Out<A> FromException(Exception ex) =>
new Out<A>(default, Empty, SubsystemTest.Error.FromException(ex));
public bool HasFailed => Error.IsSome; The I will then define a public static class Subsystem
{
public static Subsystem<A> Return<A>(A value) => () =>
Out<A>.FromValue(value);
public static Subsystem<A> Fail<A>(Exception exception) => () =>
Out<A>.FromException(exception);
public static Subsystem<A> Fail<A>(string message) => () =>
Out<A>.FromError(message);
} Notice how they're all lambdas. The pattern matches the Then I'll add the public static Subsystem<B> Bind<A, B>(this Subsystem<A> ma, Func<A, Subsystem<B>> f) => () =>
{
try
{
// Run ma
var outA = ma();
if(outA.HasFailed)
{
// If running ma failed then early out
return Out<B>.FromError((Error)outA.Error, outA.Output);
}
else
{
// Run the bind function to get the mb monad
var mb = f(outA.Value);
// Run the mb monad
var outB = mb();
// Concatenate the output from running ma and mb
var output = outA.Output + outB.Output;
// Return our result
return outB.HasFailed
? Out<B>.FromError((Error)outB.Error, output)
: Out<B>.FromValue(outB.Value, output);
}
}
catch (Exception e)
{
// Capture exceptions
return Out<B>.FromException(e);
}
}; The bind function is where you insert all the magic for your bespoke monad. This essentially runs between the lines of all operations and can do special stuff. So in this case it does error handling early-outs (like Once you have the public static Subsystem<B> Map<A, B>(
this Subsystem<A> ma,
Func<A, B> f) =>
ma.Bind(a => Return(f(a)));
public static Subsystem<B> Select<A, B>(
this Subsystem<A> ma,
Func<A, B> f) =>
ma.Bind(a => Return(f(a)));
public static Subsystem<B> SelectMany<A, B>(
this Subsystem<A> ma,
Func<A, Subsystem<B>> f) =>
ma.Bind(f);
public static Subsystem<C> SelectMany<A, B, C>(
this Subsystem<A> ma,
Func<A, Subsystem<B>> bind,
Func<A, B, C> project) =>
ma.Bind(a => bind(a).Map(b => project(a, b))); You'll notice everything is written in terms of Finally we want to add a public static Subsystem<Unit> Log(string message) => () =>
Out<Unit>.FromValue(unit, Seq1(message)); Note how it just creates a single item We could also add a little helper function to the public Unit Show()
{
var self = this;
Error.Match(
Some: err => Console.WriteLine($"Error is: {err.Message}"),
None: () => Console.WriteLine($"Result is: {self.Value}"));
Console.WriteLine();
Console.WriteLine("Output");
Console.WriteLine();
foreach(var log in Output)
{
Console.WriteLine(log);
}
return unit;
} So, now the sub-system monad is defined, we can use it. Below are two functions defined that both do some logging. The public static Subsystem<A> MakeValue<A>(A value) =>
from _ in Subsystem.Log($"Making value {value}")
select value;
public static Subsystem<int> Add(int x, int y) =>
from a in MakeValue(x)
from b in MakeValue(y)
from r in Subsystem.Return(a + b)
from _ in Subsystem.Log($"{a} + {b} = {r}")
select r; Not a particularly spectacular demo I know, but it should give you an idea: static void Main(string[] args)
{
// Build expression
var expr = Add(10, 20);
// Run expression
var result = expr();
// Show results and output log
result.Show();
} The output is:
If you start working this way you'll realise you can wrap up a lot of the scaffolding of common code patterns inside the bind function of any bespoke monad you decide to build. It also means if you decide later to add features to your monad that all existing code gets it by default (without having to thread through context objects, or use dependency injection or any of that nonsense). Finally, by including public static Subsystem<A> MakeValue<A>(A value) =>
from _ in Log($"Making value {value}")
select value;
public static Subsystem<int> Add(int x, int y) =>
from a in MakeValue(x)
from b in MakeValue(y)
from r in Return(a + b)
from _ in Log($"{a} + {b} = {r}")
select r; |
@louthy this example is enlightening. Thank you! |
What is the recommended approach for doing a "tee" in LanguageExt? The concept is similar to
Option.Do
but would work whether the option isSome
orNone
. (A tee pipe fitting lets fluid flow through but also has a different direction... the side effect.)My common use case is to log something before a function returns. I've done this by making a
Tee
object extension that runs an action and returns the same object. Example usage:Is this concept addressed already in LanguageExt? What would be a better approach?
Thank you.
The text was updated successfully, but these errors were encountered: