Skip to content

Commit 348e4f2

Browse files
gdziadkiewiczTheAngryByrd
authored andcommitted
Add Option.maybe
1 parent d8f60d3 commit 348e4f2

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/FsToolkit.ErrorHandling/Option.fs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,12 @@ module Option =
119119
binder x
120120
|> ofNull
121121
| None -> None
122+
123+
let inline maybe
124+
([<InlineIfLambda>] onNone: unit -> 'output)
125+
([<InlineIfLambda>] onSome: 'a -> 'output)
126+
(input: 'a option)
127+
: 'output =
128+
match input with
129+
| Some x -> onSome x
130+
| None -> onNone ()

tests/FsToolkit.ErrorHandling.Tests/Option.fs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ let bindNullTests =
111111
Expect.equal (Option.bindNull someBinder value1) (None) ""
112112
]
113113

114+
let maybeTests =
115+
testList "Option.maybe Tests" [
116+
testCase "Some"
117+
<| fun _ ->
118+
let value1 = Some 5
119+
let f () = 42
120+
let add2 = (+) 2
121+
Expect.equal (Option.maybe f add2 value1) 7 ""
122+
testCase "None"
123+
<| fun _ ->
124+
let value1 = None
125+
let f () = 42
126+
let add2 = (+) 2
127+
Expect.equal (Option.maybe f add2 value1) 42 ""
128+
]
129+
114130
let allTests =
115131
testList "Option Tests" [
116132
traverseResultTests
@@ -119,4 +135,5 @@ let allTests =
119135
ofResultTests
120136
ofNullTests
121137
bindNullTests
138+
maybeTests
122139
]

0 commit comments

Comments
 (0)