From b116e29fea6c96b2cc2b70009ea1e539073bdf53 Mon Sep 17 00:00:00 2001 From: Francesco Mecca Date: Thu, 12 Dec 2024 09:45:57 +0100 Subject: [PATCH] adopt F# conventions --- Pentole/Pentole.fsproj | 1 + Pentole/Seq.fs | 12 +++ Pentole/binaryprefix.fs | 12 +-- Pentole/bytesize.fs | 182 ---------------------------------------- Pentole/path.fs | 5 +- Pentole/pervasives.fs | 19 ++--- Pentole/result.fs | 4 +- Pentole/string.fs | 2 +- 8 files changed, 33 insertions(+), 204 deletions(-) create mode 100644 Pentole/Seq.fs delete mode 100644 Pentole/bytesize.fs diff --git a/Pentole/Pentole.fsproj b/Pentole/Pentole.fsproj index 57bb8b0..3f17b6f 100644 --- a/Pentole/Pentole.fsproj +++ b/Pentole/Pentole.fsproj @@ -10,6 +10,7 @@ + diff --git a/Pentole/Seq.fs b/Pentole/Seq.fs new file mode 100644 index 0000000..48648fc --- /dev/null +++ b/Pentole/Seq.fs @@ -0,0 +1,12 @@ +namespace Pentole + +module Seq = + /// + /// 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. + /// + let tee lambda (iterable: 'a seq) = seq { + for o in iterable do + lambda o + yield o + } diff --git a/Pentole/binaryprefix.fs b/Pentole/binaryprefix.fs index 5e6e6a1..01c52be 100644 --- a/Pentole/binaryprefix.fs +++ b/Pentole/binaryprefix.fs @@ -12,7 +12,7 @@ type IECBit = [] 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 = [] 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) \ No newline at end of file + member this.PiB = Bytes (this, PiB) diff --git a/Pentole/bytesize.fs b/Pentole/bytesize.fs deleted file mode 100644 index 34c38fb..0000000 --- a/Pentole/bytesize.fs +++ /dev/null @@ -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 diff --git a/Pentole/path.fs b/Pentole/path.fs index 8df2dee..f6eada4 100644 --- a/Pentole/path.fs +++ b/Pentole/path.fs @@ -26,6 +26,9 @@ module PathInternal = |> trim |> fun p -> Ok (p, false) +/// +/// See RelativePath and AbsolutePath +/// 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 diff --git a/Pentole/pervasives.fs b/Pentole/pervasives.fs index 4c74acd..380fdea 100644 --- a/Pentole/pervasives.fs +++ b/Pentole/pervasives.fs @@ -1,14 +1,9 @@ -module Pentole.Pervasives +namespace Pentole -/// -/// The identity function. -/// -let identity = id +[] +module Pervasives = -/// -/// 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 -/// -let tee fun_ obj = - fun_ obj - obj + /// + /// The identity function. + /// + let identity = id diff --git a/Pentole/result.fs b/Pentole/result.fs index 5c0e698..eb68969 100644 --- a/Pentole/result.fs +++ b/Pentole/result.fs @@ -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 diff --git a/Pentole/string.fs b/Pentole/string.fs index e732a4c..6cb06fa 100644 --- a/Pentole/string.fs +++ b/Pentole/string.fs @@ -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