<commit_before>﻿// Prime - A PRIMitivEs code library.
// Copyright (C) <NAME>, 2012-2014.

namespace Prime
open System
open System.Diagnostics
open System.ComponentModel
open System.Reflection

[<AutoOpen>]
module Miscellanea =

    /// The tautology function.
    /// No matter what you pass it, it returns true.
    let inline tautology _ = true

    /// The absurdity function.
    /// No matter what you pass it, it returns false.
    let inline absurdity _ = false

    /// A generic identification code type.
    type Id = int64

    /// The invalid Id.
    let [<Literal>] InvalidId = 0L

    /// Perform a ToString operation on anything.
    let inline string any =
        any.ToString ()

    /// Perform a formatted ToString operation on a formattable object.
    let inline stringf (formattable : IFormattable) format =
        formattable.ToString (format, null)

    /// Perform a formatted ToString operation on a formattable object.
    let inline stringfp (formattable : IFormattable) format formatProvider =
        formattable.ToString (format, formatProvider)

    /// Apply a function recursively a number of times.
    let rec doTimes fn arg times =
        if times < 0 then failwith "Cannot call doTimes with times < 0."
        elif times = 0 then arg
        else doTimes fn (fn arg) (times - 1)

    /// Perform an operation until a predicate passes.
    let rec doUntil op pred =
        if not <| pred () then
            op ()
            doUntil op pred

    /// Make a function that gets a unique number.
    /// TODO: place a mutex lock in this
    /// TODO: see if returned function can be optimized by minimizing dereferences
    let makeIdMaker () =
        let id = ref InvalidId
        let makeId =
            (fun () ->
                id := !id + 1L
                if !id = 0L then Debug.Fail "Id counter overflowed (flipped back to zero). Big trouble likely ahead!"
                !id)
        makeId

    /// Add a custom TypeConverter to an existing type.
    let assignTypeConverter<'t, 'c> () =
        ignore <| TypeDescriptor.AddAttributes (typeof<'t>, TypeConverterAttribute typeof<'c>)

    /// Short-hand for linq enumerable cast.
    let enumerable =
        System.Linq.Enumerable.Cast

    /// Convert 
    let intsToGuid m n =
        let bytes = Array.create<byte> 8 (byte 0)
        Guid (m, int16 (n >>> 16), int16 n, bytes)

    /// Try to find a type by its name from all the loaded assemblies. Time-intensive.
    /// TODO: move into TypeExtension.fs.
    let tryFindType typeName =
        match Type.GetType typeName with
        | null ->
            let allAssemblies = AppDomain.CurrentDomain.GetAssemblies ()
            let types =
                Seq.choose
                    (fun (assembly : Assembly) -> match assembly.GetType typeName with null -> None | aType -> Some aType)
                    allAssemblies
            Seq.tryFind (fun _ -> true) types 
        | aType -> Some aType

    /// Find a type by its name from all the loaded assemblies. Time-intensive.
    /// TODO: move into TypeExtension.fs.
    let findType typeName =
        match tryFindType typeName with
        | None -> failwith <| "Could not find type with name '" + typeName + "'."
        | Some aType -> aType<commit_msg>Finished documentation comment.<commit_after>﻿// Prime - A PRIMitivEs code library.
// Copyright (C) <NAME>, 2012-2014.

namespace Prime
open System
open System.Diagnostics
open System.ComponentModel
open System.Reflection

[<AutoOpen>]
module Miscellanea =

    /// The tautology function.
    /// No matter what you pass it, it returns true.
    let inline tautology _ = true

    /// The absurdity function.
    /// No matter what you pass it, it returns false.
    let inline absurdity _ = false

    /// A generic identification code type.
    type Id = int64

    /// The invalid Id.
    let [<Literal>] InvalidId = 0L

    /// Perform a ToString operation on anything.
    let inline string any =
        any.ToString ()

    /// Perform a formatted ToString operation on a formattable object.
    let inline stringf (formattable : IFormattable) format =
        formattable.ToString (format, null)

    /// Perform a formatted ToString operation on a formattable object.
    let inline stringfp (formattable : IFormattable) format formatProvider =
        formattable.ToString (format, formatProvider)

    /// Apply a function recursively a number of times.
    let rec doTimes fn arg times =
        if times < 0 then failwith "Cannot call doTimes with times < 0."
        elif times = 0 then arg
        else doTimes fn (fn arg) (times - 1)

    /// Perform an operation until a predicate passes.
    let rec doUntil op pred =
        if not <| pred () then
            op ()
            doUntil op pred

    /// Make a function that gets a unique number.
    /// TODO: place a mutex lock in this
    /// TODO: see if returned function can be optimized by minimizing dereferences
    let makeIdMaker () =
        let id = ref InvalidId
        let makeId =
            (fun () ->
                id := !id + 1L
                if !id = 0L then Debug.Fail "Id counter overflowed (flipped back to zero). Big trouble likely ahead!"
                !id)
        makeId

    /// Add a custom TypeConverter to an existing type.
    let assignTypeConverter<'t, 'c> () =
        ignore <| TypeDescriptor.AddAttributes (typeof<'t>, TypeConverterAttribute typeof<'c>)

    /// Short-hand for linq enumerable cast.
    let enumerable =
        System.Linq.Enumerable.Cast

    /// Convert a couple of ints to a Guid value.
    /// It is the user's responsibility to ensure uniqueness when using the resulting Guids.
    let intsToGuid m n =
        let bytes = Array.create<byte> 8 (byte 0)
        Guid (m, int16 (n >>> 16), int16 n, bytes)

    /// Try to find a type by its name from all the loaded assemblies. Time-intensive.
    /// TODO: move into TypeExtension.fs.
    let tryFindType typeName =
        match Type.GetType typeName with
        | null ->
            let allAssemblies = AppDomain.CurrentDomain.GetAssemblies ()
            let types =
                Seq.choose
                    (fun (assembly : Assembly) -> match assembly.GetType typeName with null -> None | aType -> Some aType)
                    allAssemblies
            Seq.tryFind (fun _ -> true) types 
        | aType -> Some aType

    /// Find a type by its name from all the loaded assemblies. Time-intensive.
    /// TODO: move into TypeExtension.fs.
    let findType typeName =
        match tryFindType typeName with
        | None -> failwith <| "Could not find type with name '" + typeName + "'."
        | Some aType -> aType
{'fraction_non_alphanumeric': 0.0855223880597015, 'fraction_numerical': 0.007462686567164179, 'mean_word_length': 2.0766758494031223, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2864070', 'n_tokens_mistral': 1877, 'n_tokens_neox': 1764, 'n_words': 746}