question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to "Tee" (similar to "Do") for side effects

See original GitHub issue

What is the recommended approach for doing a “tee” in LanguageExt? The concept is similar to Option.Do but would work whether the option is Some or None. (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:

return maybe
   .Tee(_ => _logger.LogDebug("some message"));

Is this concept addressed already in LanguageExt? What would be a better approach?

Thank you.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
louthycommented, Jul 1, 2019

I’m curious how you two typically handle logging in your sw dev…

@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 Subsystem as an example, it could be Tokeniser<A>, or TypeInfer<A>, for example.

This monad takes no input and returns an Out<A>. The Out<A> will wrap an A (the bound value of the monad) and various other stuff I want to capture. So, in your case you could wrap up a Seq<string> for a list of output log entries. I usually wrap up an Error type as well:

    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 Out<A> can now be defined:

    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 static methods on both types are just there for friendly construction.

I will then define a Subsystem static class that wraps up the behaviour of the monad. So, first I’ll define the success and fail methods:

    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 Subystem<A> delegate and so they implicitly convert to the return type.

Then I’ll add the Bind function for the monad to the Subsystem static class:

    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 Option, and Either) and exception capture (like Try) as well as log collection (like Writer).

Once you have the Bind function then the rest of the stuff that makes the type into a functor and makes it work with LINQ is almost free:

        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 Bind - it’s usually trivial to do this once you have Bind defined.

Finally we want to add a Log function to Subsystem:

        public static Subsystem<Unit> Log(string message) => () =>
            Out<Unit>.FromValue(unit, Seq1(message));

Note how it just creates a single item Seq<string>. It doesn’t need to care about how the log is built, it just needs to return a single value sequence and then the bind function does the work of joining it with other logs.

We could also add a little helper function to the Out<A> type to make debugging a touch easier:

        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 MakeValue function logs the value provided as well as returning it. The Add function adds two values together.

        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:

Result is: 30

Output

Making value 10
Making value 20
10 + 20 = 30

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 using static SubsystemTest.Subsystem it’s possible to make the LINQ a bit more elegant:

        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;

2reactions
jltremcommented, Jul 1, 2019

Super @gwintering – thanks for finding that discussion. @louthy that Tee extension is exactly what I’ve done. My concern was the same as you mentioned in #228: “I’ve kind of resisted it because it goes against the core concepts of functional programming (where we should avoid side-effects).”

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Deal With Side Effects of Medicine
In other cases, you may be able to lower your dose, try a different drug, or add another one, like an anti-nausea medicine,...
Read more >
Transoesophageal Echocardiography Related Complications
TEE in sitting position can cause dysphasia which is due to local effect of probe, ... Side effects of these drugs like respiratory...
Read more >
Transesophageal Echocardiogram - Health Encyclopedia
A TEE can help assess and locate the abnormality, as well as determine its effect on heart blood flow.
Read more >
Transesophageal Echocardiogram (TEE): Procedure Details
Talk with your provider about your medical history. Some medical conditions make a TEE too risky to perform. Tell your provider if you...
Read more >
TEPEZZA Side Effects and Safety Information
Read about common TEPEZZA side effects and important safety information. ... Would fluids like coffee or alcohol make diarrhea worse?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found