Skip to content

Commit

Permalink
Merge pull request #342 from sideeffffect/master
Browse files Browse the repository at this point in the history
Cleanup & added parsers
  • Loading branch information
forki committed Aug 23, 2016
2 parents 02d8032 + e958948 commit d5f2f0e
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 66 deletions.
18 changes: 17 additions & 1 deletion FSharpx.Extras.sln
Expand Up @@ -28,7 +28,23 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "content", "content", "{8E6D5255-776D-4B61-85F9-73C37AA1FB9A}"
ProjectSection(SolutionItems) = preProject
docs\content\index.fsx = docs\content\index.fsx
docs\content\tutorial.fsx = docs\content\tutorial.fsx
docs\content\AsyncFileExtensions.fsx = docs\content\AsyncFileExtensions.fsx
docs\content\AsyncSeqObservable.fsx = docs\content\AsyncSeqObservable.fsx
docs\content\AutoCancel.fsx = docs\content\AutoCancel.fsx
docs\content\BatchProcessing.fsx = docs\content\BatchProcessing.fsx
docs\content\BlockingQueue.fsx = docs\content\BlockingQueue.fsx
docs\content\Caching.fsx = docs\content\Caching.fsx
docs\content\ChatServer.fsx = docs\content\ChatServer.fsx
docs\content\CircularBuffer.fsx = docs\content\CircularBuffer.fsx
docs\content\Crawler.fsx = docs\content\Crawler.fsx
docs\content\DiningPhilosophers.fsx = docs\content\DiningPhilosophers.fsx
docs\content\MouseFollow.fsx = docs\content\MouseFollow.fsx
docs\content\Santa.fsx = docs\content\Santa.fsx
docs\content\StmSample.fsx = docs\content\StmSample.fsx
docs\content\StockStream.fsx = docs\content\StockStream.fsx
docs\content\StructuredFormatSample.fsx = docs\content\StructuredFormatSample.fsx
docs\content\UndoSample.fsx = docs\content\UndoSample.fsx
docs\content\WebProxy.fsx = docs\content\WebProxy.fsx
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{ED8079DD-2B06-4030-9F0F-DC548F98E1C4}"
Expand Down
6 changes: 6 additions & 0 deletions src/FSharpx.Extras/Monoid.fs
Expand Up @@ -130,6 +130,12 @@ namespace FSharpx.Collections

module Seq =

let monoid<'T> =
{ new Monoid<seq<'T>>() with
override this.Zero() = Seq.empty
override this.Combine(a,b) = Seq.append a b
}

let foldMap (monoid: _ Monoid) f =
Seq.fold (fun s e -> monoid.Combine(s, f e)) (monoid.Zero())

Expand Down
147 changes: 84 additions & 63 deletions src/FSharpx.Extras/Prelude.fs
Expand Up @@ -65,110 +65,131 @@ module Prelude =
// Bottom value
let undefined<'T> : 'T = raise (NotImplementedException("result was implemented as undefined"))

let inline toOption x = match x with
| true, v -> Some v
| _ -> None

let inline tryWith f x = f x |> toOption

type Boolean with
static member parse =
tryWith bool.TryParse

type SByte with
static member parseWithOptions style provider x =
SByte.TryParse(x, style, provider) |> toOption

static member parse x =
SByte.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x

type Byte with
static member parseWithOptions style provider x =
Byte.TryParse(x, style, provider) |> toOption

static member parse x =
match bool.TryParse(x) with
| true,v -> Some v
| _ -> None

Byte.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x

type UInt16 with
static member parseWithOptions style provider x =
UInt16.TryParse(x, style, provider) |> toOption

static member parse x =
UInt16.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x

type Int16 with
static member parseWithOptions style provider x =
match Int16.TryParse(x, style, provider) with
| true,v -> Some v
| _ -> None

static member parse x =
Int16.TryParse(x, style, provider) |> toOption

static member parse x =
Int16.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x

type UInt32 with
static member parseWithOptions style provider x =
UInt32.TryParse(x, style, provider) |> toOption

static member parse x =
UInt32.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x

type Int32 with
static member parseWithOptions style provider x =
match Int32.TryParse(x, style, provider) with
| true,v -> Some v
| _ -> None

static member parse x =
Int32.TryParse(x, style, provider) |> toOption

static member parse x =
Int32.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x

type Decimal with
type UInt64 with
static member parseWithOptions style provider x =
match Decimal.TryParse(x, style, provider) with
| true,v -> Some v
| _ -> None

static member parse x =
Decimal.parseWithOptions NumberStyles.Currency CultureInfo.InvariantCulture x
UInt64.TryParse(x, style, provider) |> toOption

type Byte with
static member parseWithOptions style provider x =
match Byte.TryParse(x, style, provider) with
| true,v -> Some v
| _ -> None

static member parse x =
Byte.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x
static member parse x =
UInt64.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x

type Int64 with
static member parseWithOptions style provider x =
match Int64.TryParse(x, style, provider) with
| true,v -> Some v
| _ -> None

static member parse x =
Int64.TryParse(x, style, provider) |> toOption

static member parse x =
Int64.parseWithOptions NumberStyles.Integer CultureInfo.InvariantCulture x

type Decimal with
static member parseWithOptions style provider x =
Decimal.TryParse(x, style, provider) |> toOption

static member parse x =
Decimal.parseWithOptions NumberStyles.Currency CultureInfo.InvariantCulture x

type Single with
static member parseWithOptions style provider x =
match Single.TryParse(x, style, provider) with
| true,v -> Some v
| _ -> None

static member parse x =
Single.TryParse(x, style, provider) |> toOption

static member parse x =
Single.parseWithOptions NumberStyles.Float CultureInfo.InvariantCulture x

type Double with
static member parseWithOptions style provider x =
match Double.TryParse(x, style, provider) with
| true,v -> Some v
| _ -> None

static member parse x =
Double.TryParse(x, style, provider) |> toOption

static member parse x =
Double.parseWithOptions NumberStyles.Float CultureInfo.InvariantCulture x

type DateTime with
static member parseWithOptions style provider x =
match DateTime.TryParse(x, provider, style) with
| true,v -> Some v
| _ -> None

static member parse x =
DateTime.TryParse(x, provider, style) |> toOption

static member parse x =
DateTime.parseWithOptions DateTimeStyles.None CultureInfo.InvariantCulture x

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

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

type DateTimeOffset with
static member parseWithOptions style provider x =
match DateTimeOffset.TryParse(x, provider, style) with
| true,v -> Some v
| _ -> None

static member parse x =
DateTimeOffset.TryParse(x, provider, style) |> toOption

static member parse x =
DateTimeOffset.parseWithOptions DateTimeStyles.None CultureInfo.InvariantCulture x

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

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

// Active patterns
let (|Boolean|_|) = Boolean.parse
let (|Int32|_|) = Int32.parse
let (|Double|_|) = Double.parse
let (|Boolean |_|) = Boolean.parse
let (|SByte |_|) = SByte.parse
let (|Byte |_|) = Byte.parse
let (|UInt16 |_|) = UInt16.parse
let (|Int16 |_|) = Int16.parse
let (|UInt32 |_|) = UInt32.parse
let (|Int32 |_|) = Int32.parse
let (|UInt64 |_|) = UInt64.parse
let (|Int64 |_|) = Int64.parse
let (|Decimal |_|) = Decimal.parse
let (|Single |_|) = Single.parse
let (|Double |_|) = Double.parse
let (|DateTime |_|) = DateTime.parse
let (|DateTimeOffset|_|) = DateTime.parse
1 change: 0 additions & 1 deletion tmp.bin

This file was deleted.

1 change: 0 additions & 1 deletion tmp.txt

This file was deleted.

0 comments on commit d5f2f0e

Please sign in to comment.