Skip to content

Commit 30db896

Browse files
author
Ruben Schmidmeister
authored
Merge pull request #44 from messerli-informatik-ag/flatten-and-then
Add overload which flattens AndThen
2 parents f671e57 + 58bb2db commit 30db896

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

Funcky.Test/OptionTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@ public void GivenAnOptionToAndThenWithAStatementThenItShouldCompile()
268268
some.AndThen(Statement);
269269
}
270270

271+
[Fact]
272+
public void GivenAnOptionAndAndAFuncToOptionItShouldBeFlattened()
273+
{
274+
var none = Option<int>.None();
275+
var some = Option.Some(42);
276+
277+
Assert.False(none.AndThen(value => Option.Some(1337)).Match(false, v => v == 1337));
278+
Assert.True(some.AndThen(value => Option.Some(1337)).Match(false, v => v == 1337));
279+
}
280+
271281
[Fact]
272282
public void GivenANoneWithOrElseFuncWeGetTheCorrectValue()
273283
{

Funcky/Monads/Option.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ public Option<TResult> AndThen<TResult>(Func<TItem, TResult> andThenFunction)
9898
? Option.Some(andThenFunction(_item))
9999
: Option<TResult>.None();
100100

101+
public Option<TResult> AndThen<TResult>(Func<TItem, Option<TResult>> andThenFunction)
102+
where TResult : notnull
103+
=> _hasItem
104+
? andThenFunction(_item)
105+
: Option<TResult>.None();
106+
101107
public void AndThen(Action<TItem> andThenFunction)
102108
{
103109
if (_hasItem)

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212
* Add `True` and `False` functions to public API
1313
* Match of `Result` Monad accepts actions
1414
* Add `FirstOrNone`, `LastOrNone` and `SingleOrNone` extension functions
15+
16+
## Unreleased
17+
* Added overload for AndThen which flattens the Option

0 commit comments

Comments
 (0)