Skip to content

Commit

Permalink
Add Option.contains
Browse files Browse the repository at this point in the history
Per RFC-FS-1007 and the discussion in fsharp/fslang-design#60
  • Loading branch information
Marcus Griep committed Nov 18, 2016
1 parent 551a7a0 commit d3fbb94
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,17 @@ type OptionModule() =
let expected = None
Assert.AreEqual(expected, actual)
[""; " "; "Baz Quux"; "Corge grault"] |> List.iter test


[<Test>]
member this.Contains() =
Assert.IsFalse( Option.contains 1 None)
Assert.IsTrue( Option.contains 1 (Some 1))

Assert.IsFalse( Option.contains "" None)
Assert.IsTrue( Option.contains "" (Some ""))

Assert.IsFalse( Option.contains None None)
Assert.IsTrue( Option.contains None (Some None))
[<Test>]
member this.OfToNullable() =
Assert.IsTrue( Option.ofNullable (System.Nullable<int>()) = None)
Expand Down
1 change: 1 addition & 0 deletions src/fsharp/FSharp.Core.Unittests/SurfaceArea.coreclr.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2713,6 +2713,7 @@ Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures
Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures+FSharpFunc`6[T1,T2,T3,T4,T5,TResult]
Microsoft.FSharp.Core.OptimizedClosures: System.String ToString()
Microsoft.FSharp.Core.OptimizedClosures: System.Type GetType()
Microsoft.FSharp.Core.OptionModule: Boolean Contains[T](T, Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean Equals(System.Object)
Microsoft.FSharp.Core.OptionModule: Boolean Exists[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean ForAll[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Expand Down
1 change: 1 addition & 0 deletions src/fsharp/FSharp.Core.Unittests/SurfaceArea.net40.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2747,6 +2747,7 @@ Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures
Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures+FSharpFunc`6[T1,T2,T3,T4,T5,TResult]
Microsoft.FSharp.Core.OptimizedClosures: System.String ToString()
Microsoft.FSharp.Core.OptimizedClosures: System.Type GetType()
Microsoft.FSharp.Core.OptionModule: Boolean Contains[T](T, Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean Equals(System.Object)
Microsoft.FSharp.Core.OptionModule: Boolean Exists[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean ForAll[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2718,6 +2718,7 @@ Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures
Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures+FSharpFunc`6[T1,T2,T3,T4,T5,TResult]
Microsoft.FSharp.Core.OptimizedClosures: System.String ToString()
Microsoft.FSharp.Core.OptimizedClosures: System.Type GetType()
Microsoft.FSharp.Core.OptionModule: Boolean Contains[T](T, Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean Equals(System.Object)
Microsoft.FSharp.Core.OptionModule: Boolean Exists[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean ForAll[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Expand Down
1 change: 1 addition & 0 deletions src/fsharp/FSharp.Core.Unittests/SurfaceArea.portable47.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2719,6 +2719,7 @@ Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures
Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures+FSharpFunc`6[T1,T2,T3,T4,T5,TResult]
Microsoft.FSharp.Core.OptimizedClosures: System.String ToString()
Microsoft.FSharp.Core.OptimizedClosures: System.Type GetType()
Microsoft.FSharp.Core.OptionModule: Boolean Contains[T](T, Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean Equals(System.Object)
Microsoft.FSharp.Core.OptionModule: Boolean Exists[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean ForAll[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Expand Down
1 change: 1 addition & 0 deletions src/fsharp/FSharp.Core.Unittests/SurfaceArea.portable7.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2731,6 +2731,7 @@ Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures
Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures+FSharpFunc`6[T1,T2,T3,T4,T5,TResult]
Microsoft.FSharp.Core.OptimizedClosures: System.String ToString()
Microsoft.FSharp.Core.OptimizedClosures: System.Type GetType()
Microsoft.FSharp.Core.OptionModule: Boolean Contains[T](T, Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean Equals(System.Object)
Microsoft.FSharp.Core.OptionModule: Boolean Exists[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean ForAll[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Expand Down
1 change: 1 addition & 0 deletions src/fsharp/FSharp.Core.Unittests/SurfaceArea.portable78.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2718,6 +2718,7 @@ Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures
Microsoft.FSharp.Core.OptimizedClosures: Microsoft.FSharp.Core.OptimizedClosures+FSharpFunc`6[T1,T2,T3,T4,T5,TResult]
Microsoft.FSharp.Core.OptimizedClosures: System.String ToString()
Microsoft.FSharp.Core.OptimizedClosures: System.Type GetType()
Microsoft.FSharp.Core.OptionModule: Boolean Contains[T](T, Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean Equals(System.Object)
Microsoft.FSharp.Core.OptionModule: Boolean Exists[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Microsoft.FSharp.Core.OptionModule: Boolean ForAll[T](Microsoft.FSharp.Core.FSharpFunc`2[T,System.Boolean], Microsoft.FSharp.Core.FSharpOption`1[T])
Expand Down
3 changes: 3 additions & 0 deletions src/fsharp/FSharp.Core/option.fs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ namespace Microsoft.FSharp.Core
[<CompiledName("ForAll")>]
let forall p inp = match inp with None -> true | Some x -> p x

[<CompiledName("Contains")>]
let contains x inp = match inp with None -> false | Some v -> x = v

[<CompiledName("Iterate")>]
let iter f inp = match inp with None -> () | Some x -> f x

Expand Down
7 changes: 7 additions & 0 deletions src/fsharp/FSharp.Core/option.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ namespace Microsoft.FSharp.Core
[<CompiledName("ForAll")>]
val forall: predicate:('T -> bool) -> option:'T option -> bool

/// <summary>Evaluates to true if <paramref name="option"/> is <c>Some</c> and its value is equal to <paramref name="value"/>.</summary>
/// <param name="value">The value to test for equality.</param>
/// <param name="option">The input option.</param>
/// <returns>True if the option is <c>Some</c> and contains a value equal to <paramref name="value"/>, otherwise false.</returns>
[<CompiledName("Contains")>]
val contains: value:'T -> option:'T option -> bool when 'T : equality

/// <summary><c>iter f inp</c> executes <c>match inp with None -> () | Some x -> f x</c>.</summary>
/// <param name="action">A function to apply to the option value.</param>
/// <param name="option">The input option.</param>
Expand Down

0 comments on commit d3fbb94

Please sign in to comment.