Skip to content

Commit e958948

Browse files
committed
add parsers and Active patterns to Prelude
1 parent 13dbaac commit e958948

1 file changed

Lines changed: 84 additions & 63 deletions

File tree

src/FSharpx.Extras/Prelude.fs

Lines changed: 84 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -65,110 +65,131 @@ module Prelude =
6565
// Bottom value
6666
let undefined<'T> : 'T = raise (NotImplementedException("result was implemented as undefined"))
6767

68+
let inline toOption x = match x with
69+
| true, v -> Some v
70+
| _ -> None
71+
72+
let inline tryWith f x = f x |> toOption
73+
6874
type Boolean with
75+
static member parse =
76+
tryWith bool.TryParse
77+
78+
type SByte with
79+
static member parseWithOptions style provider x =
80+
SByte.TryParse(x, style, provider) |> toOption
81+
82+
static member parse x =
83+
SByte.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x
84+
85+
type Byte with
86+
static member parseWithOptions style provider x =
87+
Byte.TryParse(x, style, provider) |> toOption
88+
6989
static member parse x =
70-
match bool.TryParse(x) with
71-
| true,v -> Some v
72-
| _ -> None
73-
90+
Byte.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x
91+
92+
type UInt16 with
93+
static member parseWithOptions style provider x =
94+
UInt16.TryParse(x, style, provider) |> toOption
95+
96+
static member parse x =
97+
UInt16.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x
98+
7499
type Int16 with
75100
static member parseWithOptions style provider x =
76-
match Int16.TryParse(x, style, provider) with
77-
| true,v -> Some v
78-
| _ -> None
79-
80-
static member parse x =
101+
Int16.TryParse(x, style, provider) |> toOption
102+
103+
static member parse x =
81104
Int16.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x
82105

106+
type UInt32 with
107+
static member parseWithOptions style provider x =
108+
UInt32.TryParse(x, style, provider) |> toOption
109+
110+
static member parse x =
111+
UInt32.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x
112+
83113
type Int32 with
84114
static member parseWithOptions style provider x =
85-
match Int32.TryParse(x, style, provider) with
86-
| true,v -> Some v
87-
| _ -> None
88-
89-
static member parse x =
115+
Int32.TryParse(x, style, provider) |> toOption
116+
117+
static member parse x =
90118
Int32.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x
91119

92-
type Decimal with
120+
type UInt64 with
93121
static member parseWithOptions style provider x =
94-
match Decimal.TryParse(x, style, provider) with
95-
| true,v -> Some v
96-
| _ -> None
97-
98-
static member parse x =
99-
Decimal.parseWithOptions NumberStyles.Currency CultureInfo.InvariantCulture x
122+
UInt64.TryParse(x, style, provider) |> toOption
100123

101-
type Byte with
102-
static member parseWithOptions style provider x =
103-
match Byte.TryParse(x, style, provider) with
104-
| true,v -> Some v
105-
| _ -> None
106-
107-
static member parse x =
108-
Byte.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x
124+
static member parse x =
125+
UInt64.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x
109126

110127
type Int64 with
111128
static member parseWithOptions style provider x =
112-
match Int64.TryParse(x, style, provider) with
113-
| true,v -> Some v
114-
| _ -> None
115-
116-
static member parse x =
129+
Int64.TryParse(x, style, provider) |> toOption
130+
131+
static member parse x =
117132
Int64.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x
118133

134+
type Decimal with
135+
static member parseWithOptions style provider x =
136+
Decimal.TryParse(x, style, provider) |> toOption
137+
138+
static member parse x =
139+
Decimal.parseWithOptions NumberStyles.Currency CultureInfo.InvariantCulture x
140+
119141
type Single with
120142
static member parseWithOptions style provider x =
121-
match Single.TryParse(x, style, provider) with
122-
| true,v -> Some v
123-
| _ -> None
124-
125-
static member parse x =
143+
Single.TryParse(x, style, provider) |> toOption
144+
145+
static member parse x =
126146
Single.parseWithOptions NumberStyles.Float CultureInfo.InvariantCulture x
127147

128148
type Double with
129149
static member parseWithOptions style provider x =
130-
match Double.TryParse(x, style, provider) with
131-
| true,v -> Some v
132-
| _ -> None
133-
134-
static member parse x =
150+
Double.TryParse(x, style, provider) |> toOption
151+
152+
static member parse x =
135153
Double.parseWithOptions NumberStyles.Float CultureInfo.InvariantCulture x
136154

137155
type DateTime with
138156
static member parseWithOptions style provider x =
139-
match DateTime.TryParse(x, provider, style) with
140-
| true,v -> Some v
141-
| _ -> None
142-
143-
static member parse x =
157+
DateTime.TryParse(x, provider, style) |> toOption
158+
159+
static member parse x =
144160
DateTime.parseWithOptions DateTimeStyles.None CultureInfo.InvariantCulture x
145161

146162
static member parseExactWithOptions style provider (formats: string[]) x =
147-
match DateTime.TryParseExact(x, formats, provider, style) with
148-
| true,v -> Some v
149-
| _ -> None
163+
DateTime.TryParseExact(x, formats, provider, style) |> toOption
150164

151165
static member parseExact formats x =
152166
DateTime.parseExactWithOptions DateTimeStyles.None CultureInfo.InvariantCulture formats x
153167

154168
type DateTimeOffset with
155169
static member parseWithOptions style provider x =
156-
match DateTimeOffset.TryParse(x, provider, style) with
157-
| true,v -> Some v
158-
| _ -> None
159-
160-
static member parse x =
170+
DateTimeOffset.TryParse(x, provider, style) |> toOption
171+
172+
static member parse x =
161173
DateTimeOffset.parseWithOptions DateTimeStyles.None CultureInfo.InvariantCulture x
162174

163175
static member parseExactWithOptions style provider (formats: string[]) x =
164-
match DateTimeOffset.TryParseExact(x, formats, provider, style) with
165-
| true,v -> Some v
166-
| _ -> None
176+
DateTimeOffset.TryParseExact(x, formats, provider, style) |> toOption
167177

168178
static member parseExact formats x =
169179
DateTimeOffset.parseExactWithOptions DateTimeStyles.None CultureInfo.InvariantCulture formats x
170180

171181
// Active patterns
172-
let (|Boolean|_|) = Boolean.parse
173-
let (|Int32|_|) = Int32.parse
174-
let (|Double|_|) = Double.parse
182+
let (|Boolean |_|) = Boolean.parse
183+
let (|SByte |_|) = SByte.parse
184+
let (|Byte |_|) = Byte.parse
185+
let (|UInt16 |_|) = UInt16.parse
186+
let (|Int16 |_|) = Int16.parse
187+
let (|UInt32 |_|) = UInt32.parse
188+
let (|Int32 |_|) = Int32.parse
189+
let (|UInt64 |_|) = UInt64.parse
190+
let (|Int64 |_|) = Int64.parse
191+
let (|Decimal |_|) = Decimal.parse
192+
let (|Single |_|) = Single.parse
193+
let (|Double |_|) = Double.parse
194+
let (|DateTime |_|) = DateTime.parse
195+
let (|DateTimeOffset|_|) = DateTime.parse

0 commit comments

Comments
 (0)