Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Func<> equivalent to Execute Action<> Maybe extension #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ public void Execute_executes_action()
property.Should().Be("Some value");
}

[Fact]
public void Execute_returns_func_result()
{
Maybe<MyClass> maybe = new MyClass { Property = "Some value" };

string property = maybe.Execute(x => x.Property);

property.Should().Be("Some value");
}

private static Maybe<string> GetPropertyIfExists(MyClass myClass)
{
Expand Down
9 changes: 9 additions & 0 deletions CSharpFunctionalExtensions/MaybeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,14 @@ public static void Execute<T>(this Maybe<T> maybe, Action<T> action)

action(maybe.Value);
}

public static TOut Execute<T, TOut>(this Maybe<T> maybe, Func<T, TOut> func)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like Map method, not like Execute or Select

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Select == Map in C#. Found some conflicts merging this PR. Could you re-submit?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@a-z-hub Ping!
Is this still active?

where T : class
{
if (maybe.HasNoValue)
return default(TOut);

return func(maybe.Value);
}
}
}