© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
LukeJ

✅ LanguageExt bind if

I'm having trouble understanding how to do something particular in LanguageExt that I think is just a part of functional programming in general. I have an effectful method, which I then want to evaluate the result of, and rerun if not meeting a condition. Originally, I did it like this
var key = PollInput<RT>();
        
return key.Bind(pressedKey => pressedKey.Match(
    Left: keys.Contains,
    Right: _ => true)
    ? key
    : IO<RT>.ClearCharacter()
        .Bind(_ => PollValidInput<RT>(keys)));
var key = PollInput<RT>();
        
return key.Bind(pressedKey => pressedKey.Match(
    Left: keys.Contains,
    Right: _ => true)
    ? key
    : IO<RT>.ClearCharacter()
        .Bind(_ => PollValidInput<RT>(keys)));

Turns out, obviously enough, it runs PollInput twice when you do this, which isn't good. Instead, I'm now trying to do something like this
from keyPressed in PollInput<RT>()
from _ in keyPressed.Match(
    Left: keys.Contains,
    Right: _ => true)
    ? keyPressed
    : IO<RT>.ClearCharacter()
        .Bind(_ => PollValidInput<RT>(keys));
from keyPressed in PollInput<RT>()
from _ in keyPressed.Match(
    Left: keys.Contains,
    Right: _ => true)
    ? keyPressed
    : IO<RT>.ClearCharacter()
        .Bind(_ => PollValidInput<RT>(keys));

But this doesn't work, since keyPressed is not an Eff, and PollValidInput (the method this is running in), is an Eff. I have no idea how to do this. I just want to bind that else condition when that match fails, but I need to bind/map to run that condition, so I also need to return something for when that condition is met.

Any ideas?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Typed delegates in maps (LanguageExt)
C#CC# / help
3y ago
❔ ❔ Bind selected items
C#CC# / help
4y ago
Cant bind styles to elements
C#CC# / help
11mo ago