21 people like it.

Functional style Regex engine

Functional style Regex engine

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
let char c (s : string) = seq { if s.Length > 0 && s.[0] = c then yield s.Substring(1) }

let (=>) l r s = seq { for sl in l s do for sr in r sl -> sr }

let (<|>) l r s = seq { yield! l s; yield! r s }

let rec (<*>) e s = seq { yield s; yield! (e => (<*>) e) s }

let (<+>) e = e => (<*>) e

// example c(a|d)+r
let pattern = char 'c' => (<+>) (char 'a' <|> char 'd') => char 'r'
Multiple items
val char : c:char -> s:string -> seq<string>

Full name: Script.char

--------------------
type char = System.Char

Full name: Microsoft.FSharp.Core.char
val c : char
val s : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
property System.String.Length: int
System.String.Substring(startIndex: int) : string
System.String.Substring(startIndex: int, length: int) : string
val l : ('a -> #seq<'c>)
val r : ('c -> #seq<'e>)
val s : 'a
val sl : 'c
val sr : 'e
val r : ('a -> #seq<'c>)
val e : ('a -> #seq<'a>)
val pattern : (string -> seq<string>)

Full name: Script.pattern
Raw view Test code New version

More information

Link:http://fssnip.net/10
Posted:14 years ago
Author:Nick Palladinos
Tags: regex