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.

Cycle around Either<> result (a SelfFoldWhile function?)

See original GitHub issue

I have derived this very simple example of a problem that I’m facing:

public class CalculationTest
{
    private static Either<string, decimal> Evaluate(decimal x)
    {
        if (x < 0 || x > 1)
            return "value outside boundaries";
        return x / 2;
    }

    [Fact]
    public void Test()
    {
        decimal x = 0.8M;
        decimal epsilon = 0.00001M;

        while (x >= epsilon)
        {
            Either<string, decimal> v = Evaluate(x).Map(d => x = d);
            
            if (v.IsLeft)
                break;
        }

        Assert.True(x < epsilon);
    }
}

My real code is more complex, but we can recognize the idea: I have a function that can fail (so the return type is an Either<>, with a simple string as the error, in this example), and I need to call it repeatedly progressively approximating the result (that means reusing the returned value).
This is the best formulation that I could find, but I feel there should be a more elegant solution that I don’t see…
What could be a better approach? Thank you.

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
louthycommented, Dec 5, 2022

I assume x changes within the while loop?

There’s a couple of patterns that are relevant here:

  • Recursive functions
  • Folds

This is a recursive approach

    Recursive(decimal x) =>
        from v in Evaluate(x).Map(d => x = d)
        from r in v < epsilon
                ? Right<string, decimal>(r)
                : Recursive(x + 1)       // this is where the x changes per loop
        select r;

The next way is to turn the x value into a stream of xs and Fold or FoldWhile. You can create a stream of xs by having a for loop that yields an IEnumerable, or use Prelude.unfold:

0reactions
ABIOLI-COMcommented, Dec 7, 2022

Oh, I really read it very badly… 😦 Sorry, and thanks again for your patience! Now it’s all clear! Thank you again… AB

Read more comments on GitHub >

github_iconTop Results From Across the Web

No results found

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