File tree Expand file tree Collapse file tree 3 files changed +34
-0
lines changed
src/FsToolkit.ErrorHandling
tests/FsToolkit.ErrorHandling.Tests Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 2828 <Compile Include =" ValidationCE.fs" />
2929 <Compile Include =" Option.fs" />
3030 <Compile Include =" OptionCE.fs" />
31+ <Compile Include =" OptionOp.fs" />
3132 <Compile Include =" ValueOption.fs" />
3233 <Compile Include =" ValueOptionCE.fs" />
3334 <Compile Include =" AsyncOption.fs" />
Original file line number Diff line number Diff line change 1+ namespace FsToolkit.ErrorHandling.Operator.Option
2+
3+ open FsToolkit.ErrorHandling
4+
5+ [<AutoOpen>]
6+ module Option =
7+ let inline (>>= )
8+ ( input: Option< 'input>)
9+ ([< InlineIfLambda>] binder: 'input -> Option< 'output>)
10+ : Option< 'output> =
11+ Option.bind binder input
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ open SampleDomain
1010open TestData
1111open TestHelpers
1212open FsToolkit.ErrorHandling
13+ open FsToolkit.ErrorHandling .Operator .Option
1314
1415
1516let traverseResultTests =
@@ -172,6 +173,26 @@ let ofPairTests =
172173 Expect.equal ( Option.ofPair pair) ( None) " "
173174 ]
174175
176+ let optionOperatorsTests =
177+ testList " Option Operators Tests" [
178+ testCase " bind operator"
179+ <| fun _ ->
180+ let evenInt x = if x % 2 = 0 then Some x else None
181+
182+ let tryParseInt ( x : string ) =
183+ match Int32.TryParse x with
184+ | true , value -> Some value
185+ | _ -> None
186+
187+ let tryParseEvenInt str =
188+ tryParseInt str
189+ >>= evenInt
190+
191+
192+ tryParseEvenInt " 2"
193+ |> Expect.hasSomeValue 2
194+ ]
195+
175196let allTests =
176197 testList " Option Tests" [
177198 traverseResultTests
@@ -182,4 +203,5 @@ let allTests =
182203 bindNullTests
183204 eitherTests
184205 ofPairTests
206+ optionOperatorsTests
185207 ]
You can’t perform that action at this time.
0 commit comments