Replies: 2 comments
-
There's nothing in the library, because we can't ad-hoc define the fish operator for public static class Kleisli
{
public static Func<A, Option<C>> Compose<A, B, C>(
this Func<A, Option<B>> fa,
Func<B, Option<C>> fb) =>
x => from b in fa(x)
from c in fb(b)
select c;
} For methods, you'd need to call the public static Option<int> ParseInt(string x) =>
Int32.TryParse(x, out var r)
? r
: None;
public static Option<int> OddOnly(int x) =>
x % 2 == 1
? x
: None;
var mc = Kleisli.Compose<string, int, int>(ParseInt, OddOnly); It even expects the generic arguments to be provided, which is frustrating. If you're working with Func<string, Option<int>> f = ParseInt;
Func<int, Option<int>> g = OddOnly;
var gf = f.Compose(g); Obviously if you step up to multiple stages of composition, rather than just one, it starts getting more and more inelegant. Creating local functions or static methods to represent the composition often looks and reads better (IMHO): Option<int> local(string x) =>
from b in ParseInt(x)
from c in OddOnly(b)
select c; I'm not against adding support for Kleisli if you still feel strongly that it's a better approach. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the explanation! I was looking for a c# definition to aid a discussion with a colleague. This was the first place I thought to look. I was a little surprised when I didn't find it, but assumed there'd be a good reason it was left out. I didn't find any discussion in old issues, so figured I'd ask out of curiosity. Your examples in this thread actually provide a nice supplement to our discussion already, so thank you! I'm not currently doing much c# development, and don't have a good reason to ask for this feature. If I find myself in need in the future, perhaps I'll revisit this thread. Thanks again! |
Beta Was this translation helpful? Give feedback.
-
Is Kleisli composition for monad returning functions (aka <=<) implemented in this library?
Beta Was this translation helpful? Give feedback.
All reactions