CSharpFunctionalExtensions icon indicating copy to clipboard operation
CSharpFunctionalExtensions copied to clipboard

Is there a need for some syntactic suger? Or am i too blind to find it?

Open LaszloLueck opened this issue 3 years ago • 7 comments

Hi, thank you for this nice extensions. Currently i using a similar extension, because of inactivity i assume to change to this one. I used your Maybe<T> functionality extensively to keep the nullables away from me. I came from the scala-world, so in this project am missing some syntactic sugar around the Maybe<T>. In scala it is a little part of pattern matching: e.g.

public static TOut ResolveOr<TIn, TOut>(this Maybe<TIn> source, Func<TIn, TOut> resolver,
            Func<TOut> alternative) => !source.HasValue ? alternative.Invoke() : resolver.Invoke(source.Value);

to realize something like this (resolve the Maybe<T> to a TOut):

Maybe<T> value = 
IEnumerable
.Select(some things)
.Where(some filter)
.TryFirst();

And then:

var result = value.ResolveOr(
resolver => {do a lot of stuff with resolver and give back a value},
() => {when TryFirst will give back nothing, here you can go an alternative way and give back a value}
)

result is then of the result-type of your resolver/alternative method.

Is there a need to put this generic sugar to your extension or exists those things in your extension but i´m too blind to find it (missing reference or else)?

LaszloLueck avatar Jun 08 '21 10:06 LaszloLueck

Looks like Match is similar to what you are describing? https://github.com/vkhorikov/CSharpFunctionalExtensions/blob/master/CSharpFunctionalExtensions/Maybe/MaybeExtensions.cs#L103

vkhorikov avatar Jun 08 '21 21:06 vkhorikov

Thank you @vkhorikov ! Yes it is exactly the same, i´m too blind! ;)

Funny part, the signature is the same as my ResolveOr so i can simply replace the words in my project.

Thanks again, Regards, Laszlo

LaszloLueck avatar Jun 09 '21 05:06 LaszloLueck

Hi again, one signature i´m missing with match:

public static TOut ResolveOr<TOut>(this Maybe<TOut> source, TOut alternative) =>
        !source.HasValue ? alternative : source.Value;

With this you can build something like this (e.g. for string type):

var value = maybeValue.ResolveOr("alternativeValue");

Or i´m too blind to find it! ;)

Regards, Laszlo

LaszloLueck avatar Jun 09 '21 05:06 LaszloLueck

OK, i answer my own question. Looks like Unwrap does the same as my function do.

I´m too blind.

Tanks again, Laszlo

LaszloLueck avatar Jun 09 '21 06:06 LaszloLueck

One question: Is there a Wrapper for nullable (or the counterpart of unwrap)? Currently i have written something like this:

        public static Maybe<TOut> MaybeValue<TOut>(this TOut value)
        {
            return value == null ? Maybe<TOut>.None : Maybe<TOut>.From(value);
        }

And another one: You have a IEnumerable<Maybe<T>> and as a result your goal is a filtered IEnumerable<T>. Is there something? Currently i use:

public static IEnumerable<TSource> Values<TSource>(this IEnumerable<Maybe<TSource>> source) =>
            source
                .Where(filtered => filtered.HasValue)
                .Select(selected => selected.Value);

Thanks, Laszlo

LaszloLueck avatar Jun 09 '21 07:06 LaszloLueck

OK, for the Values method there is a Choose method in your project. That seems nearly that what i was looking for. But its usage is a little bit more trickier as i need only a filter for the none-types withoud a Func<T,U>.

As an example:

var l = new List<Maybe<int>>() {Maybe<int>.None, Maybe<int>.From(2), Maybe<int>.None, Maybe<int>.From(4)};

Your method needs a Func<T, U> selector as parameter. If you only need the filter you must write too much.

var result1 = l.Choose(k => k);

My signature filters only the None-Values and return the rest, so it´s easier to read IMHO.

var result2 = l.Values();

Regards, Laszlo

LaszloLueck avatar Jun 09 '21 21:06 LaszloLueck

Is there a Wrapper for nullable (or the counterpart of unwrap)?

There's an implicit conversion that does that. You can just do this and it will automatically wrap the null into a Maybe:

Maybe<string> MyMethod()
{
    return null;
}

OK, for the Values method there is a Choose method in your project. That seems nearly that what i was looking for. But its usage is a little bit more trickier as i need only a filter for the none-types withoud a Func<T,U>.

We can add an overload for this method that would omit the selector.

vkhorikov avatar Jun 14 '21 16:06 vkhorikov