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

Inline results module #16106

Merged
merged 8 commits into from
Oct 20, 2023
Merged
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
30 changes: 15 additions & 15 deletions src/FSharp.Core/result.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ namespace Microsoft.FSharp.Core
module Result =

[<CompiledName("Map")>]
let map mapping result =
let inline map ([<InlineIfLambda>] mapping) result =
match result with
| Error e -> Error e
| Ok x -> Ok(mapping x)

[<CompiledName("MapError")>]
let mapError mapping result =
let inline mapError ([<InlineIfLambda>] mapping) result =
match result with
| Error e -> Error(mapping e)
| Ok x -> Ok x

[<CompiledName("Bind")>]
let bind binder result =
let inline bind ([<InlineIfLambda>] binder) result =
match result with
| Error e -> Error e
| Ok x -> binder x
Expand All @@ -36,43 +36,43 @@ module Result =
| Error _ -> true

[<CompiledName("DefaultValue")>]
let defaultValue value result =
let inline defaultValue value result =
match result with
| Error _ -> value
| Ok v -> v

[<CompiledName("DefaultWith")>]
let defaultWith defThunk result =
let inline defaultWith ([<InlineIfLambda>] defThunk) result =
match result with
| Error error -> defThunk error
| Ok v -> v

[<CompiledName("Count")>]
let count result =
let inline count result =
match result with
| Error _ -> 0
| Ok _ -> 1

[<CompiledName("Fold")>]
let fold<'T, 'Error, 'State> folder (state: 'State) (result: Result<'T, 'Error>) =
let inline fold<'T, 'Error, 'State> ([<InlineIfLambda>] folder) (state: 'State) (result: Result<'T, 'Error>) =
match result with
| Error _ -> state
| Ok x -> folder state x

[<CompiledName("FoldBack")>]
let foldBack<'T, 'Error, 'State> folder (result: Result<'T, 'Error>) (state: 'State) =
let inline foldBack<'T, 'Error, 'State> ([<InlineIfLambda>] folder) (result: Result<'T, 'Error>) (state: 'State) =
match result with
| Error _ -> state
| Ok x -> folder x state

[<CompiledName("Exists")>]
let exists predicate result =
let inline exists ([<InlineIfLambda>] predicate) result =
match result with
| Error _ -> false
| Ok x -> predicate x

[<CompiledName("ForAll")>]
let forall predicate result =
let inline forall ([<InlineIfLambda>] predicate) result =
match result with
| Error _ -> true
| Ok x -> predicate x
Expand All @@ -84,31 +84,31 @@ module Result =
| Ok v -> v = value

[<CompiledName("Iterate")>]
let iter action result =
let inline iter ([<InlineIfLambda>] action) result =
match result with
| Error _ -> ()
| Ok x -> action x

[<CompiledName("ToArray")>]
let toArray result =
let inline toArray result =
match result with
| Error _ -> [||]
| Ok x -> [| x |]

[<CompiledName("ToList")>]
let toList result =
let inline toList result =
match result with
| Error _ -> []
| Ok x -> [ x ]

[<CompiledName("ToOption")>]
let toOption result =
let inline toOption result =
match result with
| Error _ -> None
| Ok x -> Some x

[<CompiledName("ToValueOption")>]
let toValueOption result =
let inline toValueOption result =
match result with
| Error _ -> ValueNone
| Ok x -> ValueSome x
30 changes: 15 additions & 15 deletions src/FSharp.Core/result.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Result =
/// </code>
/// </example>
[<CompiledName("Map")>]
val map: mapping: ('T -> 'U) -> result: Result<'T, 'TError> -> Result<'U, 'TError>
val inline map: mapping: ('T -> 'U) -> result: Result<'T, 'TError> -> Result<'U, 'TError>

/// <summary><c>map f inp</c> evaluates to <c>match inp with Error x -> Error (f x) | Ok v -> Ok v</c>.</summary>
///
Expand All @@ -42,7 +42,7 @@ module Result =
/// </code>
/// </example>
[<CompiledName("MapError")>]
val mapError: mapping: ('TError -> 'U) -> result: Result<'T, 'TError> -> Result<'T, 'U>
val inline mapError: mapping: ('TError -> 'U) -> result: Result<'T, 'TError> -> Result<'T, 'U>

/// <summary><c>bind f inp</c> evaluates to <c>match inp with Error e -> Error e | Ok x -> f x</c></summary>
///
Expand All @@ -67,7 +67,7 @@ module Result =
/// </code>
/// </example>
[<CompiledName("Bind")>]
val bind: binder: ('T -> Result<'U, 'TError>) -> result: Result<'T, 'TError> -> Result<'U, 'TError>
val inline bind: binder: ('T -> Result<'U, 'TError>) -> result: Result<'T, 'TError> -> Result<'U, 'TError>

/// <summary>Returns true if the result is Ok.</summary>
/// <param name="result">The input result.</param>
Expand Down Expand Up @@ -112,7 +112,7 @@ module Result =
/// </code>
/// </example>
[<CompiledName("DefaultValue")>]
val defaultValue: value: 'T -> result: Result<'T, 'Error> -> 'T
val inline defaultValue: value: 'T -> result: Result<'T, 'Error> -> 'T

/// <summary>Gets the value of the result if the result is <c>Ok</c>, otherwise evaluates <paramref name="defThunk"/> and returns the result.</summary>
///
Expand All @@ -129,7 +129,7 @@ module Result =
/// </code>
/// </example>
[<CompiledName("DefaultWith")>]
val defaultWith: defThunk: ('Error -> 'T) -> result: Result<'T, 'Error> -> 'T
val inline defaultWith: defThunk: ('Error -> 'T) -> result: Result<'T, 'Error> -> 'T

/// <summary><c>count inp</c> evaluates to <c>match inp with Error _ -> 0 | Ok _ -> 1</c>.</summary>
///
Expand All @@ -144,7 +144,7 @@ module Result =
/// </code>
/// </example>
[<CompiledName("Count")>]
val count: result: Result<'T, 'Error> -> int
val inline count: result: Result<'T, 'Error> -> int

/// <summary><c>fold f s inp</c> evaluates to <c>match inp with Error _ -> s | Ok x -> f s x</c>.</summary>
///
Expand All @@ -163,7 +163,7 @@ module Result =
/// </code>
/// </example>
[<CompiledName("Fold")>]
val fold<'T, 'Error, 'State> :
val inline fold<'T, 'Error, 'State> :
folder: ('State -> 'T -> 'State) -> state: 'State -> result: Result<'T, 'Error> -> 'State

/// <summary><c>fold f inp s</c> evaluates to <c>match inp with Error _ -> s | Ok x -> f x s</c>.</summary>
Expand All @@ -183,7 +183,7 @@ module Result =
/// </code>
/// </example>
[<CompiledName("FoldBack")>]
val foldBack<'T, 'Error, 'State> :
val inline foldBack<'T, 'Error, 'State> :
folder: ('T -> 'State -> 'State) -> result: Result<'T, 'Error> -> state: 'State -> 'State

/// <summary><c>exists p inp</c> evaluates to <c>match inp with Error _ -> false | Ok x -> p x</c>.</summary>
Expand All @@ -202,7 +202,7 @@ module Result =
/// </code>
/// </example>
[<CompiledName("Exists")>]
val exists: predicate: ('T -> bool) -> result: Result<'T, 'Error> -> bool
val inline exists: predicate: ('T -> bool) -> result: Result<'T, 'Error> -> bool

/// <summary><c>forall p inp</c> evaluates to <c>match inp with Error _ -> true | Ok x -> p x</c>.</summary>
///
Expand All @@ -220,7 +220,7 @@ module Result =
/// </code>
/// </example>
[<CompiledName("ForAll")>]
val forall: predicate: ('T -> bool) -> result: Result<'T, 'Error> -> bool
val inline forall: predicate: ('T -> bool) -> result: Result<'T, 'Error> -> bool

/// <summary>Evaluates to true if <paramref name="result"/> is <c>Ok</c> and its value is equal to <paramref name="value"/>.</summary>
///
Expand Down Expand Up @@ -251,7 +251,7 @@ module Result =
/// </code>
/// </example>
[<CompiledName("Iterate")>]
val iter: action: ('T -> unit) -> result: Result<'T, 'Error> -> unit
val inline iter: action: ('T -> unit) -> result: Result<'T, 'Error> -> unit

/// <summary>Convert the result to an array of length 0 or 1.</summary>
///
Expand All @@ -266,7 +266,7 @@ module Result =
/// </code>
/// </example>
[<CompiledName("ToArray")>]
val toArray: result: Result<'T, 'Error> -> 'T[]
val inline toArray: result: Result<'T, 'Error> -> 'T[]

/// <summary>Convert the result to a list of length 0 or 1.</summary>
///
Expand All @@ -281,7 +281,7 @@ module Result =
/// </code>
/// </example>
[<CompiledName("ToList")>]
val toList: result: Result<'T, 'Error> -> 'T list
val inline toList: result: Result<'T, 'Error> -> 'T list

/// <summary>Convert the result to an Option value.</summary>
///
Expand All @@ -296,7 +296,7 @@ module Result =
/// </code>
/// </example>
[<CompiledName("ToOption")>]
val toOption: result: Result<'T, 'Error> -> 'T option
val inline toOption: result: Result<'T, 'Error> -> 'T option

/// <summary>Convert the result to an Option value.</summary>
///
Expand All @@ -311,4 +311,4 @@ module Result =
/// </code>
/// </example>
[<CompiledName("ToValueOption")>]
val toValueOption: result: Result<'T, 'Error> -> 'T voption
val inline toValueOption: result: Result<'T, 'Error> -> 'T voption