adopt F# conventions

This commit is contained in:
Francesco Mecca 2024-12-12 09:45:57 +01:00
parent 350cd98661
commit b116e29fea
8 changed files with 33 additions and 204 deletions

View file

@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="Seq.fs" />
<Compile Include="logginghelpers.fs" />
<Compile Include="binaryprefix.fs" />
<Compile Include="pervasives.fs" />

12
Pentole/Seq.fs Normal file
View file

@ -0,0 +1,12 @@
namespace Pentole
module Seq =
/// <summary>
/// Pass each item to a function and return the item unchanged.
/// Useful in the middle of pipelines to see what values are being passed or log.
/// </summary>
let tee lambda (iterable: 'a seq) = seq {
for o in iterable do
lambda o
yield o
}

View file

@ -12,7 +12,7 @@ type IECBit =
[<CustomComparison>]
type Bits (n: double, scale: IECBit) =
struct
member x.bits =
member _.bits =
match scale with
| Bit -> n
| Kb -> n * 1000.0
@ -21,7 +21,7 @@ type Bits (n: double, scale: IECBit) =
| Tb -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0
| Pb -> n * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0
member x.bytes =
member _.bytes =
match scale with
| Bit -> n / 8.0
| Kb -> n * 1000.0 / 8.0
@ -62,7 +62,7 @@ type IECByte =
[<CustomComparison>]
type Bytes (n: double, scale: IECByte) =
struct
member x.bits =
member _.bits =
match scale with
| Byte -> n * 8.0
| KB -> n * 1000.0 * 8.0
@ -77,7 +77,7 @@ type Bytes (n: double, scale: IECByte) =
| TiB -> n * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 8.0
| PiB -> n * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 8.0
member x.bytes =
member _.bytes =
match scale with
| Byte -> n
| KB -> n * 1000.0
@ -103,7 +103,7 @@ type Bytes (n: double, scale: IECByte) =
| :? Bytes as b -> a.bytes = b.bytes
| _ -> false
override this.ToString () = $"Bytes({n} {scale})"
override _.ToString () = $"Bytes({n} {scale})"
override this.GetHashCode () = this.bits.GetHashCode()
end
@ -130,4 +130,4 @@ module Bytes =
member this.MiB = Bytes (this, MiB)
member this.GiB = Bytes (this, GiB)
member this.TiB = Bytes (this, TiB)
member this.PiB = Bytes (this, PiB)
member this.PiB = Bytes (this, PiB)

View file

@ -1,182 +0,0 @@
module Pentole.BinaryPrefix
type Bit =
| Bit of double
| Kb of double
| Mb of double
| Gb of double
| Tb of double
| Pb of double
member this.bytes =
match this with
| Bit.Bit i -> i
| Bit.Kb i -> i * 1000.0 / 8.0
| Bit.Mb i -> i * 1000.0 * 1000.0 / 8.0
| Bit.Gb i -> i * 1000.0 * 1000.0 * 1000.0 / 8.0
| Bit.Tb i -> i * 1000.0 * 1000.0 * 1000.0 * 1000.0 / 8.0
| Bit.Pb i -> i * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 / 8.0
type Byte =
| Byte of double
| KB of double
| MB of double
| GB of double
| TB of double
| PB of double
| KiB of double
| MiB of double
| GiB of double
| TiB of double
| PiB of double
member this.bytes =
match this with
| Byte.Byte i -> i
| Byte.KB i -> i * 1000.0
| Byte.MB i -> i * 1000.0 * 1000.0
| Byte.GB i -> i * 1000.0 * 1000.0 * 1000.0
| Byte.TB i -> i * 1000.0 * 1000.0 * 1000.0 * 1000.0
| Byte.PB i -> i * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0
| Byte.KiB i -> i * 1024.0
| Byte.MiB i -> i * 1024.0 * 1024.0
| Byte.GiB i -> i * 1024.0 * 1024.0 * 1024.0
| Byte.TiB i -> i * 1024.0 * 1024.0 * 1024.0 * 1024.0
| Byte.PiB i -> i * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0
type System.Double with
member this.bit = Bit this
member this.Kb = Bit this
member this.Mb = Mb this
member this.Gb = Gb this
member this.Tb = Tb this
member this.Pb = Pb this
member this.byte = Bit this
member this.KB = Bit this
member this.MB = Mb this
member this.GB = Gb this
member this.TB = Tb this
member this.PB = Pb this
member this.KiB = Bit this
member this.MiB = Mb this
member this.GiB = Gb this
member this.TiB = Tb this
member this.PiB = Pb this
module =
type IECBitUnit =
| Bit
| Kilobit
| Megabit
| Gigabit
| Terabit
| Petabit
type IECByteUnit =
| Byte
| Kilobyte
| Kibibyte
| Megabyte
| Mebibyte
| Gigabyte
| Gibibyte
| Terabyte
| Tebibyte
| Petabyte
| Pebibyte
let private get_i_byte = function
| KB i | MB i | GB i | TB i | PB i
| KiB i | MiB i | GiB i | TiB i | PiB i
| Byte.Byte i -> i
let convert_bytes (scale: IECBitUnit) (old_value: Byte) =
let x = get_i_byte old_value
let up, base_ =
match old_value with
| Byte.Byte _ -> 0.0, 1000.0
| KB _ -> 1.0, 1000.0
| MB _ -> 2.0, 1000.0
| GB _ -> 3.0, 1000.0
| TB _ -> 4.0, 1000.0
| PB _ -> 5.0, 1000.0
| KiB _ -> 1.0, 1024.0
| MiB _ -> 2.0, 1024.0
| GiB _ -> 3.0, 1024.0
| TiB _ -> 4.0, 1024.0
| PiB _ -> 5.0, 1024.0
let down =
match scale with
| Bit -> 0.0
| Kilobit -> 1.0
| Megabit -> 2.0
| Gigabit -> 3.0
| Terabit -> 4.0
| Petabit -> 5.0
let new_value = x * 8.0 * (base_ ** (up - down))
match scale with
| Bit -> Bit.Bit new_value
| Kilobit -> Bit.Kb new_value
| Megabit -> Bit.Mb new_value
| Gigabit -> Bit.Gb new_value
| Terabit -> Bit.Tb new_value
| Petabit -> Bit.Pb new_value
let inline convert_bytes (scale: IECByteUnit) (old_value: Byte) =
let x = get_i_byte old_value
let up, base_ =
match old_value with
| KB _ -> 1.0, 1000.0
| MB _ -> 2.0, 1000.0
| GB _ -> 3.0, 1000.0
| TB _ -> 4.0, 1000.0
| PB _ -> 5.0, 1000.0
| KiB _ -> 1.0, 1024.0
| MiB _ -> 2.0, 1024.0
| GiB _ -> 3.0, 1024.0
| TiB _ -> 4.0, 1024.0
| PiB _ -> 5.0, 1024.0
| Byte.Byte _ -> 0.0, 1000.0
let down =
match scale with
| Kilobyte -> 1.0
| Megabyte -> 2.0
| Gigabyte -> 3.0
| Terabyte -> 4.0
| Petabyte -> 5.0
| Kibibyte -> 1.0
| Mebibyte -> 2.0
| Gibibyte -> 3.0
| Tebibyte -> 4.0
| Pebibyte -> 5.0
| Byte -> 0.0
let new_value = x * 8.0 * (base_ ** (up - down))
match scale with
| Byte -> Byte.Byte new_value
| Kilobyte -> KB new_value
| Megabyte -> MB new_value
| Gigabyte -> GB new_value
| Terabyte -> TB new_value
| Petabyte -> PB new_value
| Kibibyte -> KiB new_value
| Mebibyte -> MiB new_value
| Gibibyte -> GiB new_value
| Tebibyte -> TiB new_value
| Pebibyte -> PiB new_value

View file

@ -26,6 +26,9 @@ module PathInternal =
|> trim
|> fun p -> Ok (p, false)
/// <summary>
/// See RelativePath and AbsolutePath
/// </summary>
type IPath =
abstract member ToString: unit -> string
/// The string value of the path. Mainly useful for C# interop.
@ -45,7 +48,7 @@ type IPath =
type AbsolutePath internal (path: string) =
interface IPath with
member _.ToString () = $"AbsolutePath {path}"
member _.string_value = path
override _.string_value = path
member x.CompareTo (o: obj) =
match o with

View file

@ -1,14 +1,9 @@
module Pentole.Pervasives
namespace Pentole
/// <summary>
/// The identity function.
/// </summary>
let identity = id
[<AutoOpen>]
module Pervasives =
/// <summary>
/// Pass the object to a function and return the object unchanged.
/// Useful in the middle of pipelines to see what values are being passed or log
/// </summary>
let tee fun_ obj =
fun_ obj
obj
/// <summary>
/// The identity function.
/// </summary>
let identity = id

View file

@ -7,13 +7,13 @@ module Result =
Ok (f x)
with e -> Error e
let inline pairwise_map fun_ (x: 'a, y: 'a) =
let inline pairwiseMap fun_ (x: 'a, y: 'a) =
match fun_ x with
| Error e -> Error e
| Ok o ->
match fun_ y with | Ok o' -> Ok (o, o') | Error e -> Error e
let of_option = function | Some s -> Ok s | None -> Error ()
let fromOption = function | Some s -> Ok s | None -> Error ()
let zip a b =
match (a, b) with

View file

@ -1,6 +1,6 @@
module Pentole.String
let split(separator: string) (target: string) : string list =
let split (separator: string) (target: string) : string list =
if System.String.IsNullOrEmpty separator then
[target]
else