Author Archives: Rolf

F# split text into sections

type Section (lines:string list) =
    member x.Lines = lines

/// Splits a sequence after the last line of a section defined by the predicate.
let splitAtEach (predicate: 'T -> bool) (xs : seq<'T>) : seq<'T list> =
    let section = new ResizeArray<'T>()
    seq {
        for x in xs do
            section.Add x
            if predicate x then 
                yield Seq.toList section
                section.Clear()
        if section.Count > 0 then
            yield Seq.toList section}

/// Splits a sequence before the first line of a section defined by the predicate.
let splitBeforeEach (predicate: 'T -> bool) (xs : seq<'T>) : seq<'T list> =
    let section = new ResizeArray<'T>()
    seq {
        for x in xs do
            if predicate x then 
                yield Seq.toList section
                section.Clear()
            section.Add x
        if section.Count > 0 then
            yield Seq.toList section}

usage:

open System.IO

let lines = File.ReadAllLines(myTextFile)

let getSections lines =
    splitAtEach isSectionLine lines
    |> Seq.map (fun x -> new Section(x))
    |> Seq.toList

This is a slightly modified version from: https://codereview.stackexchange.com/questions/55554/parsing-sections-from-a-log-file-using-f

VBA Write2dArrayToSheet

A subroutine to write the data of a 2D-string array to a worksheet.

Sub Write2dArrayToSheet(data() As String, ByRef sheet As Worksheet)
    Dim i, j, n, m As Long
    
    n = 1
    For i = LBound(data, 1) To UBound(data, 1)
        m = 1
        For j = LBound(data, 2) To UBound(data, 2)
            sheet.Cells(n, m) = data(i, j)
            m = m + 1
        Next j
        n = n + 1
    Next i
End Sub

F# get object data type

This function prints the data type of an object to the standard output.

let getObjectType (x:obj) =
    match x with
    | :? sbyte       as y -> printfn  "sbyte : %A" y
    | :? byte        as y -> printfn  "byte : %A" y
    | :? int16       as y -> printfn  "int16 : %A" y
    | :? uint16      as y -> printfn  "uint16 : %A" y
    | :? int32       as y -> printfn  "int32 : %A (int)" y
//    | :? int         as y -> printfn  "int : %A" y            
//    //   int is actually the same as int32
    | :? uint32      as y -> printfn  "uint32 : %A" y
    | :? int64       as y -> printfn  "int64 : %A" y
    | :? uint64      as y -> printfn  "uint64 : %A" y
    | :? bigint      as y -> printfn  "bigint : %A" y
    | :? float32     as y -> printfn  "float32 : %A" y
    | :? float       as y -> printfn  "float : %A" y
    | :? decimal     as y -> printfn  "decimal : %A" y
//    | :? BigRational as y -> printfn  "BigRational : %A" y    
//    //   BigRational requires a reference to FSharp.PowerPack.dll
    | :? char        as y -> printfn  "char : %A" y
    | :? string      as y -> printfn  "string : %A" y
    | :? bool        as y -> printfn  "bool : %A" y
    | :? System.DateTime as y -> printfn "DateTime : %A" y
    | _ -> printfn "unknown data type"

List of F# data types: https://www.tutorialspoint.com/fsharp/fsharp_data_types.htm