28 people like it.

Functional wrappers for TryParse APIs

Exemplary convenience wrappers for some of the System..TryParse APIs, using the combined power of F#' return value deconstruction mechanism via pattern matching, active patterns and option types instead of "out/ref" parameters

 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: 
// see also 
// http://stackoverflow.com/questions/4949941/convert-string-to-system-datetime-in-f

module TryParser =
    // convenient, functional TryParse wrappers returning option<'a>
    let tryParseWith tryParseFunc = tryParseFunc >> function
        | true, v    -> Some v
        | false, _   -> None

    let parseDate   = tryParseWith System.DateTime.TryParse
    let parseInt    = tryParseWith System.Int32.TryParse
    let parseSingle = tryParseWith System.Single.TryParse
    let parseDouble = tryParseWith System.Double.TryParse
    // etc.

    // active patterns for try-parsing strings
    let (|Date|_|)   = parseDate
    let (|Int|_|)    = parseInt
    let (|Single|_|) = parseSingle
    let (|Double|_|) = parseDouble

open TryParser

// tests
let parseMe = function
    | Date d   -> printfn "DateTime %A" d
    | Int 42   -> printfn "Bingo!"
    | Int i    -> printfn "Int32 %i" i
    | Single f -> printfn "Single %g" f
    | Double d -> printfn "Double %g" d // never hit, always parsed as float32
    | s        -> printfn "Don't know how to parse %A" s

parseMe "213"
parseMe "2010-02-22"
parseMe "3,0"
parseMe "jslkdfjlkj"
parseMe "42"
val tryParseWith : tryParseFunc:('a -> bool * 'b) -> ('a -> 'b option)

Full name: Script.TryParser.tryParseWith
val tryParseFunc : ('a -> bool * 'b)
val v : 'b
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
val parseDate : (string -> System.DateTime option)

Full name: Script.TryParser.parseDate
namespace System
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
System.DateTime()
   (+0 other overloads)
System.DateTime(ticks: int64) : unit
   (+0 other overloads)
System.DateTime(ticks: int64, kind: System.DateTimeKind) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, calendar: System.Globalization.Calendar) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: System.DateTimeKind) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: System.Globalization.Calendar) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: System.DateTimeKind) : unit
   (+0 other overloads)
System.DateTime.TryParse(s: string, result: byref<System.DateTime>) : bool
System.DateTime.TryParse(s: string, provider: System.IFormatProvider, styles: System.Globalization.DateTimeStyles, result: byref<System.DateTime>) : bool
val parseInt : (string -> int option)

Full name: Script.TryParser.parseInt
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
System.Int32.TryParse(s: string, result: byref<int>) : bool
System.Int32.TryParse(s: string, style: System.Globalization.NumberStyles, provider: System.IFormatProvider, result: byref<int>) : bool
val parseSingle : (string -> float32 option)

Full name: Script.TryParser.parseSingle
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
System.Single.TryParse(s: string, result: byref<float32>) : bool
System.Single.TryParse(s: string, style: System.Globalization.NumberStyles, provider: System.IFormatProvider, result: byref<float32>) : bool
val parseDouble : (string -> float option)

Full name: Script.TryParser.parseDouble
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
System.Double.TryParse(s: string, result: byref<float>) : bool
System.Double.TryParse(s: string, style: System.Globalization.NumberStyles, provider: System.IFormatProvider, result: byref<float>) : bool
module TryParser

from Script
val parseMe : _arg1:string -> unit

Full name: Script.parseMe
active recognizer Date: string -> System.DateTime option

Full name: Script.TryParser.( |Date|_| )
val d : System.DateTime
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
active recognizer Int: string -> int option

Full name: Script.TryParser.( |Int|_| )
val i : int
active recognizer Single: string -> float32 option

Full name: Script.TryParser.( |Single|_| )
val f : float32
active recognizer Double: string -> float option

Full name: Script.TryParser.( |Double|_| )
val d : float
val s : string

More information

Link:http://fssnip.net/2y
Posted:6 years ago
Author:Novox
Tags: parsing , pattern matching , active patterns , wrapper