The Computer Language
24.04 Benchmarks Game

regex-redux F# .NET #4 program

source code

// The Computer Language Benchmarks Game
// https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
//
// 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
.NET SDK 8.0.200
Host Version: 8.0.2
Commit: 1381d5ebd2
<ServerGarbageCollection>true
F# 8.0



 Mon, 01 Apr 2024 23:25:48 GMT

MAKE:
cp regexredux.fsharpcore-4.fsharpcore Program.fs
cp Include/fsharpcore/tmp.fsproj .
mkdir obj
cp Include/fsharpcore/project.assets.json ./obj
/usr/bin/dotnet build -c Release --no-self-contained -r linux-x64 	
MSBuild version 17.9.6+a4ecab324 for .NET
  Determining projects to restore...
  Restored /home/dunham/all-benchmarksgame/benchmarksgame_i53330/regexredux/tmp/tmp.fsproj (in 716 ms).
  tmp -> /home/dunham/all-benchmarksgame/benchmarksgame_i53330/regexredux/tmp/bin/Release/net8.0/linux-x64/tmp.dll

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

Time Elapsed 00:00:08.12

10.22s to complete and log all make actions

COMMAND LINE:
 ./bin/Release/net8.0/linux-x64/tmp 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