-
Notifications
You must be signed in to change notification settings - Fork 1
Type safe Scanf
Module: FSharp.Scanf
Source: src/FSharp.Scanf/scanf.fs
FSharp.Scanf provides several functions to parse string into typed objects type-safely.
FSharp.Scanf is available as a separate package:
PM> Install-Package FSharp.Scanf
open FSharp.Scanf
let (idNum, name, isDead) = scanfn "#%d: %s (dead:%b)"
// input: "#1991: James Brown (dead:true)"
// val name : string = "James Brown"
// val idNum : int = 1991
// val isDead : bool = trueThe FSharp.Scanf.Optimized contains the optimized version of the functions above.
They can only work with up to 7 captures (= the return type must be 7-tuples or less), but they don't use reflections under the hood and will run about 6x-7x faster than the normal ones.
To use them, simply open FSharp.Scanf.Optimized instead of FSharp.Scanf. They have the same signature, except the optimized ones have SRTPs in their type parameters.
-
tryFoovariants returnOk resultwhen it successfully parses the input, orError exceptionotherwise. -
kFoovariants take a continuation to modify the result after parsing. -
tryKfoovariants do the both.
-
scanfn: PrintfFormat<..> -> 'a -
tryScanfn: PrintfFormat<..> -> Result<'a, exn> -
kscanfn: PrintfFormat<..> -> ('a -> 'b) -> 'b -
tryKscanfn: PrintfFormat<..> -> ('a -> 'b) -> Result<'b, exn>
sscanf: PrintfFormat<..> -> string -> 'atrySscanf: PrintfFormat<..> -> string -> Result<'a, exn>ksscanf: PrintfFormat<..> -> ('a -> 'b) -> string -> 'btryKsscanf: PrintfFormat<..> -> ('a -> 'b) -> string -> Result<'b, exn>
fscanfn: PrintfFormat<..> -> TextReader -> 'atryFscanfn: PrintfFormat<..> -> TextReader -> Result<'a, exn>kfscanfn: PrintfFormat<..> -> ('a -> 'b) -> TextReader -> 'btryKfscanfn: PrintfFormat<..> -> ('a -> 'b) -> TextReader -> Result<'b, exn>
function
| Sscanf "%i-%b" (num, flag) -> ...