Home
Insert
Update snippet 'Parsing formulas'
Title
Description
Parsing simple formulas using active patterns
Source code
open System let (|ParseNumber|_|) text = match Int32.TryParse(text) with | true, num -> Some(sprintf "Number: %d" num) | _ -> None let (|ParseExpression|_|) (text:string) = if text.StartsWith("=") then Some(sprintf "Formula: %s" (text.Substring(1))) else None let (|Length|) (text:string) = text.Length let parseFormula text = match text with | Length 0 -> "Empty" | ParseNumber s -> s | ParseExpression s -> s | _ -> "Error" parseFormula "" parseFormula "= 4 + 3" parseFormula "42" parseFormula "error"
Tags
active pattern
formula
active pattern
formula
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update