Skip to content

Commit d5f2f0e

Browse files
authored
Merge pull request #342 from sideeffffect/master
Cleanup & added parsers
2 parents 02d8032 + e958948 commit d5f2f0e

5 files changed

Lines changed: 107 additions & 66 deletions

File tree

FSharpx.Extras.sln

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,23 @@ EndProject
2828
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "content", "content", "{8E6D5255-776D-4B61-85F9-73C37AA1FB9A}"
2929
ProjectSection(SolutionItems) = preProject
3030
docs\content\index.fsx = docs\content\index.fsx
31-
docs\content\tutorial.fsx = docs\content\tutorial.fsx
31+
docs\content\AsyncFileExtensions.fsx = docs\content\AsyncFileExtensions.fsx
32+
docs\content\AsyncSeqObservable.fsx = docs\content\AsyncSeqObservable.fsx
33+
docs\content\AutoCancel.fsx = docs\content\AutoCancel.fsx
34+
docs\content\BatchProcessing.fsx = docs\content\BatchProcessing.fsx
35+
docs\content\BlockingQueue.fsx = docs\content\BlockingQueue.fsx
36+
docs\content\Caching.fsx = docs\content\Caching.fsx
37+
docs\content\ChatServer.fsx = docs\content\ChatServer.fsx
38+
docs\content\CircularBuffer.fsx = docs\content\CircularBuffer.fsx
39+
docs\content\Crawler.fsx = docs\content\Crawler.fsx
40+
docs\content\DiningPhilosophers.fsx = docs\content\DiningPhilosophers.fsx
41+
docs\content\MouseFollow.fsx = docs\content\MouseFollow.fsx
42+
docs\content\Santa.fsx = docs\content\Santa.fsx
43+
docs\content\StmSample.fsx = docs\content\StmSample.fsx
44+
docs\content\StockStream.fsx = docs\content\StockStream.fsx
45+
docs\content\StructuredFormatSample.fsx = docs\content\StructuredFormatSample.fsx
46+
docs\content\UndoSample.fsx = docs\content\UndoSample.fsx
47+
docs\content\WebProxy.fsx = docs\content\WebProxy.fsx
3248
EndProjectSection
3349
EndProject
3450
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{ED8079DD-2B06-4030-9F0F-DC548F98E1C4}"

src/FSharpx.Extras/Monoid.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ namespace FSharpx.Collections
130130

131131
module Seq =
132132

133+
let monoid<'T> =
134+
{ new Monoid<seq<'T>>() with
135+
override this.Zero() = Seq.empty
136+
override this.Combine(a,b) = Seq.append a b
137+
}
138+
133139
let foldMap (monoid: _ Monoid) f =
134140
Seq.fold (fun s e -> monoid.Combine(s, f e)) (monoid.Zero())
135141

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

tmp.bin

Lines changed: 0 additions & 1 deletion
This file was deleted.

tmp.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)