-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSystem.fs
More file actions
68 lines (53 loc) · 2.53 KB
/
System.fs
File metadata and controls
68 lines (53 loc) · 2.53 KB
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
/// This code is licensed as Apache 2.0
/// Access this below functionality by issuing an "open System"
namespace System
[<AutoOpen>]
module Core =
/// Call ToString() on the argument
let toString x =
x.ToString ()
/// records may be null when created from C# or other .Net languages
/// Use when F# complains: "The Type 'foo' does not have 'null' as a proper value"
let nullRecordAsNone x =
if(box x = null) then None else Some x
/// Unwrap a Nullable<_> by providing a defaultValue if null
let unNullable defaultValue (x :Nullable<_>) =
if x.HasValue then x.Value else defaultValue
/// Nicer implementation than the 'core.defualtArg' since the default value is the first parameter - making it easier to use with piping
let unOption defaultValue x =
match x with
| Some y -> y
| None -> defaultValue
let tryresultAsOption x =
match x with
| true, y -> Some y
| false, _ -> None
[<AutoOpen>]
module DateAndTime =
type System.TimeSpan with
static member fromDays x = TimeSpan.FromDays (float x)
static member fromHours x = TimeSpan.FromHours(float x)
static member fromMilliseconds x = TimeSpan.FromMilliseconds(float x)
static member fromMinutes x = TimeSpan.FromMinutes (float x)
static member fromSeconds x = TimeSpan.FromSeconds(float x)
[<AutoOpen>]
module Parsing =
type System.Boolean with
static member tryParse str = System.Boolean.TryParse str |> tryresultAsOption
type System.Double with
static member tryParse str = System.Double.TryParse str |> tryresultAsOption
type System.Int32 with
static member tryParse str = System.Int32.TryParse str |> tryresultAsOption
type System.Int64 with
static member tryParse str = System.Int64.TryParse str |> tryresultAsOption
type System.Guid with
static member tryParse str = System.Guid.TryParse str |> tryresultAsOption
type System.DateTime with
static member tryParse str = System.DateTime.TryParse str |> tryresultAsOption
// for each of the above extension methods, define an active pattern below
let (|Bool|_|) str = System.Boolean.tryParse str
let (|Float|_|) str = System.Double.tryParse str
let (|Int|_|) str = System.Int32.tryParse str
let (|Int64|_|) str = System.Int64.tryParse str
let (|Guid|_|) str = System.Guid.tryParse str
let (|DateTime|_|) str = System.DateTime.tryParse str