28 people like it.
Like the snippet!
Functions around nullable types
Various functions around nullable types
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
|
module FSharp.Nullable
open System
open Microsoft.FSharp.Math
module Option =
let fromNullable (n: _ Nullable) =
if n.HasValue
then Some n.Value
else None
let toNullable =
function
| None -> Nullable()
| Some x -> Nullable(x)
let (|Null|Value|) (x: _ Nullable) =
if x.HasValue then Value x.Value else Null
module Nullable =
let create x = Nullable x
let getOrDefault n v = match n with Value x -> x | _ -> v
let getOrElse (n: 'a Nullable) (v: 'a Lazy) = match n with Value x -> x | _ -> v.Force()
let get (x: _ Nullable) = x.Value
let fromOption = Option.toNullable
let toOption = Option.fromNullable
let bind f x =
match x with
| Null -> Nullable()
| Value v -> f v
let hasValue (x: _ Nullable) = x.HasValue
let isNull (x: _ Nullable) = not x.HasValue
let count (x: _ Nullable) = if x.HasValue then 1 else 0
let fold f state x =
match x with
| Null -> state
| Value v -> f state v
let foldBack f x state =
match x with
| Null -> state
| Value v -> f x state
let exists p x =
match x with
| Null -> false
| Value v -> p x
let forall p x =
match x with
| Null -> true
| Value v -> p x
let iter f x =
match x with
| Null -> ()
| Value v -> f v
let map f x =
match x with
| Null -> Nullable()
| Value v -> f v
let toArray x =
match x with
| Null -> [||]
| Value v -> [| v |]
let toList x =
match x with
| Null -> []
| Value v -> [v]
let liftNullable op (a: _ Nullable) (b: _ Nullable) =
if a.HasValue && b.HasValue
then Nullable(op a.Value b.Value)
else Nullable()
let mapBoolOp op a b =
match a,b with
| Value x, Value y -> op x y
| _ -> false
let inline (+?) a b = (liftNullable (+)) a b
let inline (-?) a b = (liftNullable (-)) a b
let inline ( *?) a b = (liftNullable ( *)) a b
let inline (/?) a b = (liftNullable (/)) a b
let inline (>?) a b = (mapBoolOp (>)) a b
let inline (>=?) a b = a >? b || a = b
let inline (<?) a b = (mapBoolOp (<)) a b
let inline (<=?) a b = a <? b || a = b
let inline notn (a: bool Nullable) =
if a.HasValue
then Nullable(not a.Value)
else Nullable()
let inline (&?) a b =
let rec and' a b =
match a,b with
| Null, Value y when not y -> Nullable(false)
| Null, Value y when y -> Nullable()
| Null, Null -> Nullable()
| Value x, Value y -> Nullable(x && y)
| _ -> and' b a
and' a b
let inline (|?) a b = notn ((notn a) &? (notn b))
type Int32 with
member x.n = Nullable x
type Double with
member x.n = Nullable x
type Single with
member x.n = Nullable x
type Byte with
member x.n = Nullable x
type Int64 with
member x.n = Nullable x
|
Multiple items
namespace FSharp
--------------------
namespace Microsoft.FSharp
module Nullable
from FSharp
namespace System
namespace Microsoft
namespace Microsoft.FSharp
type Math =
static val PI : float
static val E : float
static member Abs : value:sbyte -> sbyte + 6 overloads
static member Acos : d:float -> float
static member Asin : d:float -> float
static member Atan : d:float -> float
static member Atan2 : y:float * x:float -> float
static member BigMul : a:int * b:int -> int64
static member Ceiling : d:decimal -> decimal + 1 overload
static member Cos : d:float -> float
...
Full name: System.Math
module Option
from Microsoft.FSharp.Core
val fromNullable : n:Nullable<'a> -> 'a option (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Option.fromNullable
val n : Nullable<'a> (requires default constructor and value type and 'a :> ValueType)
Multiple items
type Nullable =
static member Compare<'T> : n1:Nullable<'T> * n2:Nullable<'T> -> int
static member Equals<'T> : n1:Nullable<'T> * n2:Nullable<'T> -> bool
static member GetUnderlyingType : nullableType:Type -> Type
Full name: System.Nullable
--------------------
type Nullable<'T (requires default constructor and value type and 'T :> ValueType)> =
struct
new : value:'T -> Nullable<'T>
member Equals : other:obj -> bool
member GetHashCode : unit -> int
member GetValueOrDefault : unit -> 'T + 1 overload
member HasValue : bool
member ToString : unit -> string
member Value : 'T
end
Full name: System.Nullable<_>
--------------------
Nullable()
Nullable(value: 'T) : unit
property Nullable.HasValue: bool
union case Option.Some: Value: 'T -> Option<'T>
property Nullable.Value: 'a
union case Option.None: Option<'T>
val toNullable : _arg1:'a option -> Nullable<'a> (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Option.toNullable
val x : 'a (requires default constructor and value type and 'a :> ValueType)
val x : Nullable<'a> (requires default constructor and value type and 'a :> ValueType)
val create : x:'a -> Nullable<'a> (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Nullable.create
val getOrDefault : n:Nullable<'a> -> v:'a -> 'a (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Nullable.getOrDefault
val v : 'a (requires default constructor and value type and 'a :> ValueType)
active recognizer Value: Nullable<'a> -> Choice<unit,'a>
Full name: FSharp.Nullable.( |Null|Value| )
val getOrElse : n:Nullable<'a> -> v:Lazy<'a> -> 'a (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Nullable.getOrElse
val v : Lazy<'a> (requires default constructor and value type and 'a :> ValueType)
Multiple items
active recognizer Lazy: Lazy<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.( |Lazy| )
--------------------
type Lazy<'T> =
new : unit -> Lazy<'T> + 5 overloads
member IsValueCreated : bool
member ToString : unit -> string
member Value : 'T
Full name: System.Lazy<_>
--------------------
Lazy() : unit
Lazy(valueFactory: Func<'T>) : unit
Lazy(isThreadSafe: bool) : unit
Lazy(mode: Threading.LazyThreadSafetyMode) : unit
Lazy(valueFactory: Func<'T>, isThreadSafe: bool) : unit
Lazy(valueFactory: Func<'T>, mode: Threading.LazyThreadSafetyMode) : unit
member Lazy.Force : unit -> 'T
val get : x:Nullable<'a> -> 'a (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Nullable.get
val fromOption : ('a option -> Nullable<'a>) (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Nullable.fromOption
Multiple items
module Option
from FSharp.Nullable
--------------------
module Option
from Microsoft.FSharp.Core
val toOption : (Nullable<'a> -> 'a option) (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Nullable.toOption
val bind : f:('a -> Nullable<'b>) -> x:Nullable<'a> -> Nullable<'b> (requires default constructor and value type and 'a :> ValueType and default constructor and value type and 'b :> ValueType)
Full name: FSharp.Nullable.Nullable.bind
val f : ('a -> Nullable<'b>) (requires default constructor and value type and 'a :> ValueType and default constructor and value type and 'b :> ValueType)
active recognizer Null: Nullable<'a> -> Choice<unit,'a>
Full name: FSharp.Nullable.( |Null|Value| )
val hasValue : x:Nullable<'a> -> bool (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Nullable.hasValue
val isNull : x:Nullable<'a> -> bool (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Nullable.isNull
val not : value:bool -> bool
Full name: Microsoft.FSharp.Core.Operators.not
val count : x:Nullable<'a> -> int (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Nullable.count
val fold : f:('a -> 'b -> 'a) -> state:'a -> x:Nullable<'b> -> 'a (requires default constructor and value type and 'b :> ValueType)
Full name: FSharp.Nullable.Nullable.fold
val f : ('a -> 'b -> 'a) (requires default constructor and value type and 'b :> ValueType)
val state : 'a
val x : Nullable<'b> (requires default constructor and value type and 'b :> ValueType)
val v : 'b (requires default constructor and value type and 'b :> ValueType)
val foldBack : f:(Nullable<'a> -> 'b -> 'b) -> x:Nullable<'a> -> state:'b -> 'b (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Nullable.foldBack
val f : (Nullable<'a> -> 'b -> 'b) (requires default constructor and value type and 'a :> ValueType)
val state : 'b
val exists : p:(Nullable<'a> -> bool) -> x:Nullable<'a> -> bool (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Nullable.exists
val p : (Nullable<'a> -> bool) (requires default constructor and value type and 'a :> ValueType)
val forall : p:(Nullable<'a> -> bool) -> x:Nullable<'a> -> bool (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Nullable.forall
val iter : f:('a -> unit) -> x:Nullable<'a> -> unit (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Nullable.iter
val f : ('a -> unit) (requires default constructor and value type and 'a :> ValueType)
val map : f:('a -> Nullable<'b>) -> x:Nullable<'a> -> Nullable<'b> (requires default constructor and value type and 'a :> ValueType and default constructor and value type and 'b :> ValueType)
Full name: FSharp.Nullable.Nullable.map
val toArray : x:Nullable<'a> -> 'a [] (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Nullable.toArray
val toList : x:Nullable<'a> -> 'a list (requires default constructor and value type and 'a :> ValueType)
Full name: FSharp.Nullable.Nullable.toList
val liftNullable : op:('a -> 'b -> 'c) -> a:Nullable<'a> -> b:Nullable<'b> -> Nullable<'c> (requires default constructor and value type and 'a :> ValueType and default constructor and value type and 'b :> ValueType and default constructor and value type and 'c :> ValueType)
Full name: FSharp.Nullable.liftNullable
val op : ('a -> 'b -> 'c) (requires default constructor and value type and 'a :> ValueType and default constructor and value type and 'b :> ValueType and default constructor and value type and 'c :> ValueType)
val a : Nullable<'a> (requires default constructor and value type and 'a :> ValueType)
val b : Nullable<'b> (requires default constructor and value type and 'b :> ValueType)
property Nullable.Value: 'b
val mapBoolOp : op:('a -> 'b -> bool) -> a:Nullable<'a> -> b:Nullable<'b> -> bool (requires default constructor and value type and 'a :> ValueType and default constructor and value type and 'b :> ValueType)
Full name: FSharp.Nullable.mapBoolOp
val op : ('a -> 'b -> bool) (requires default constructor and value type and 'a :> ValueType and default constructor and value type and 'b :> ValueType)
val y : 'b (requires default constructor and value type and 'b :> ValueType)
val a : Nullable<'a> (requires member ( + ) and default constructor and value type and 'a :> ValueType and default constructor and value type and 'b :> ValueType and default constructor and value type and 'c :> ValueType)
val b : Nullable<'b> (requires member ( + ) and default constructor and value type and 'b :> ValueType and default constructor and value type and 'a :> ValueType and default constructor and value type and 'c :> ValueType)
val a : Nullable<'a> (requires member ( - ) and default constructor and value type and 'a :> ValueType and default constructor and value type and 'b :> ValueType and default constructor and value type and 'c :> ValueType)
val b : Nullable<'b> (requires member ( - ) and default constructor and value type and 'b :> ValueType and default constructor and value type and 'a :> ValueType and default constructor and value type and 'c :> ValueType)
val a : Nullable<'a> (requires member ( * ) and default constructor and value type and 'a :> ValueType and default constructor and value type and 'b :> ValueType and default constructor and value type and 'c :> ValueType)
val b : Nullable<'b> (requires member ( * ) and default constructor and value type and 'b :> ValueType and default constructor and value type and 'a :> ValueType and default constructor and value type and 'c :> ValueType)
val a : Nullable<'a> (requires member ( / ) and default constructor and value type and 'a :> ValueType and default constructor and value type and 'b :> ValueType and default constructor and value type and 'c :> ValueType)
val b : Nullable<'b> (requires member ( / ) and default constructor and value type and 'b :> ValueType and default constructor and value type and 'a :> ValueType and default constructor and value type and 'c :> ValueType)
val a : Nullable<'a> (requires comparison and default constructor and value type and 'a :> ValueType)
val b : Nullable<'a> (requires comparison and default constructor and value type and 'a :> ValueType)
val notn : a:Nullable<bool> -> Nullable<bool>
Full name: FSharp.Nullable.notn
val a : Nullable<bool>
type bool = Boolean
Full name: Microsoft.FSharp.Core.bool
property Nullable.Value: bool
val b : Nullable<bool>
val and' : (Nullable<bool> -> Nullable<bool> -> Nullable<bool>)
val y : bool
val x : bool
type Int32 =
struct
member CompareTo : value:obj -> int + 1 overload
member Equals : obj:obj -> bool + 1 overload
member GetHashCode : unit -> int
member GetTypeCode : unit -> TypeCode
member ToString : unit -> string + 3 overloads
static val MaxValue : int
static val MinValue : int
static member Parse : s:string -> int + 3 overloads
static member TryParse : s:string * result:int -> bool + 1 overload
end
Full name: System.Int32
val x : Int32
member Int32.n : Nullable<Int32>
Full name: FSharp.Nullable.n
type Double =
struct
member CompareTo : value:obj -> int + 1 overload
member Equals : obj:obj -> bool + 1 overload
member GetHashCode : unit -> int
member GetTypeCode : unit -> TypeCode
member ToString : unit -> string + 3 overloads
static val MinValue : float
static val MaxValue : float
static val Epsilon : float
static val NegativeInfinity : float
static val PositiveInfinity : float
...
end
Full name: System.Double
val x : Double
member Double.n : Nullable<Double>
Full name: FSharp.Nullable.n
type Single =
struct
member CompareTo : value:obj -> int + 1 overload
member Equals : obj:obj -> bool + 1 overload
member GetHashCode : unit -> int
member GetTypeCode : unit -> TypeCode
member ToString : unit -> string + 3 overloads
static val MinValue : float32
static val Epsilon : float32
static val MaxValue : float32
static val PositiveInfinity : float32
static val NegativeInfinity : float32
...
end
Full name: System.Single
val x : Single
member Single.n : Nullable<Single>
Full name: FSharp.Nullable.n
type Byte =
struct
member CompareTo : value:obj -> int + 1 overload
member Equals : obj:obj -> bool + 1 overload
member GetHashCode : unit -> int
member GetTypeCode : unit -> TypeCode
member ToString : unit -> string + 3 overloads
static val MaxValue : byte
static val MinValue : byte
static member Parse : s:string -> byte + 3 overloads
static member TryParse : s:string * result:byte -> bool + 1 overload
end
Full name: System.Byte
val x : Byte
member Byte.n : Nullable<Byte>
Full name: FSharp.Nullable.n
type Int64 =
struct
member CompareTo : value:obj -> int + 1 overload
member Equals : obj:obj -> bool + 1 overload
member GetHashCode : unit -> int
member GetTypeCode : unit -> TypeCode
member ToString : unit -> string + 3 overloads
static val MaxValue : int64
static val MinValue : int64
static member Parse : s:string -> int64 + 3 overloads
static member TryParse : s:string * result:int64 -> bool + 1 overload
end
Full name: System.Int64
val x : Int64
member Int64.n : Nullable<Int64>
Full name: FSharp.Nullable.n
More information