The Computer Language
Benchmarks Game

regex-redux F# .NET Core #4 program

source code

// The Computer Language Benchmarks Game
// http://benchmarksgame.alioth.debian.org/
//
// regex-dna program contributed by David Grenier
// converted from regex-dna program

open System.Text.RegularExpressions
open System.Threading

let inline flip f a b = f b a
let inline curry f a b = f(a, b)

let regex s = Regex (s, RegexOptions.Compiled)
let replace rx = ((regex rx).Replace : string * string -> string) |> curry |> flip
let matchCount rx = (regex rx).Matches >> fun x -> x.Count

let input = stdin.ReadToEnd ()
let text = input |> replace ">.*\n" "" |> replace "\n" ""

module Array =
    module Parallel =
        let loop (count: int) f =
            let count = ref count
            let rec loop f =
                async {
                    match Interlocked.Decrement count with
                    | i when i < 0 -> ()
                    | i -> f i; Async.StartImmediate (loop f)
                }
            Array.init System.Environment.ProcessorCount (fun _ -> loop f)
            |> Async.Parallel
            |> Async.RunSynchronously
            |> ignore


        let init len f =
            let result = Array.zeroCreate len
            loop len (fun i -> result.[i] <- f i)
            result

        let map f arr = init (Array.length arr) (fun i -> f arr.[i])

[|
    "agggtaaa|tttaccct"
    "[cgt]gggtaaa|tttaccc[acg]"
    "a[act]ggtaaa|tttacc[agt]t"
    "ag[act]gtaaa|tttac[agt]ct"
    "agg[act]taaa|ttta[agt]cct"
    "aggg[acg]aaa|ttt[cgt]ccct"
    "agggt[cgt]aa|tt[acg]accct"
    "agggta[cgt]a|t[acg]taccct"
    "agggtaa[cgt]|[acg]ttaccct"
|]
|> Array.Parallel.map (fun s -> sprintf "%s %d" s (matchCount s text))
|> Array.iter (printfn "%s")

let chunkedMap windowCount f arr =
    let len = Array.length arr
    let size = len / (windowCount - 1)
    Array.Parallel.init windowCount (fun i ->
        if i + 1 = windowCount then len % (windowCount - 1) else size
        |> Array.sub arr (i * size)
        |> f
    )

let applyPatterns =
    [
        replace "tHa[Nt]" "<4>"
        replace "aND|caN|Ha[DS]|WaS" "<3>"
        replace "a[NSt]|BY" "<2>"
        replace "<[^>]*>" "|"
        replace "\\|[^|][^|]*\\|" "-"
    ]
    |> List.reduce (>>)

text.ToCharArray()
|> chunkedMap 16 (fun cs ->
    System.String cs
    |> applyPatterns
    |> String.length
)
|> Array.sum
|> printfn "\n%i\n%i\n%i" input.Length text.Length
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
2.0.2 a04b4bf512
"System.GC.Server": true


Thu, 09 Nov 2017 00:45:47 GMT

MAKE:
cp regexredux.fsharpcore-4.fsharpcore Program.fs
cp Include/fsharpcore/tmp.fsproj .
cp Include/fsharpcore/runtimeconfig.template.json .
mkdir obj
cp Include/fsharpcore/project.assets.json ./obj
cp Include/fsharpcore/tmp.fsproj.nuget.g.props ./obj
cp Include/fsharpcore/tmp.fsproj.nuget.g.targets ./obj
/usr/bin/dotnet build -c Release --no-restore
Microsoft (R) Build Engine version 15.4.8.50001 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  tmp -> /home/dunham/benchmarksgame_quadcore/regexredux/tmp/bin/Release/netcoreapp2.0/tmp.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:09.29

9.86s to complete and log all make actions

COMMAND LINE:
/usr/bin/dotnet ./bin/Release/netcoreapp2.0/tmp.dll 0 < regexredux-input50000.txt

UNEXPECTED OUTPUT 

13c13
< 273969
---
> 273927

PROGRAM OUTPUT:
agggtaaa|tttaccct 3
[cgt]gggtaaa|tttaccc[acg] 12
a[act]ggtaaa|tttacc[agt]t 43
ag[act]gtaaa|tttac[agt]ct 27
agg[act]taaa|ttta[agt]cct 58
aggg[acg]aaa|ttt[cgt]ccct 16
agggt[cgt]aa|tt[acg]accct 15
agggta[cgt]a|t[acg]taccct 18
agggtaa[cgt]|[acg]ttaccct 20

508411
500000
273969