6 people like it.

DIY F# getSize()

Why do my functions work "properly" with int8, int16, ... float64, decimal but not with string and bigInt ?

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
#nowarn "051"
 
open System
open System.Numerics

let getAddr (data : nativeptr<'T>) : int = 
    
    sprintf "%A" data
    |> String.filter ( fun c -> Char.IsDigit(c) )
    |> int 

let GetSize (data : 'T) : int =

    let mutable value = [| data ; data |]

    (getAddr &&value.[1]) - (getAddr &&value.[0])

printfn " bool    : %02d byte(s) " (GetSize true)
printfn " int8    : %02d byte(s) " (GetSize 8y)
printfn " int16   : %02d byte(s) " (GetSize 123s)
printfn " int32   : %02d byte(s) " (GetSize 999 )
printfn " int64   : %02d byte(s) " (GetSize 999L)
printfn " float32 : %02d byte(s) " (GetSize 999.999f)
printfn " float64 : %02d byte(s) " (GetSize 999.999)
printfn " decimal : %02d byte(s) " (GetSize 99999999999999999999999M)
printfn " BigInt  : %02d byte(s) " (GetSize 999999999999999999999999999999999999999999I)
namespace System
namespace System.Numerics
val getAddr : data:nativeptr<'T> -> int (requires unmanaged)

Full name: Script.getAddr
val data : nativeptr<'T> (requires unmanaged)
type nativeptr<'T (requires unmanaged)> = (# "<Common IL Type Omitted>" #)

Full name: Microsoft.FSharp.Core.nativeptr<_>
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
val filter : predicate:(char -> bool) -> str:string -> string

Full name: Microsoft.FSharp.Core.String.filter
val c : char
type Char =
  struct
    member CompareTo : value:obj -> int + 1 overload
    member Equals : obj:obj -> bool + 1 overload
    member GetHashCode : unit -> int
    member GetTypeCode : unit -> TypeCode
    member ToString : unit -> string + 1 overload
    static val MaxValue : char
    static val MinValue : char
    static member ConvertFromUtf32 : utf32:int -> string
    static member ConvertToUtf32 : highSurrogate:char * lowSurrogate:char -> int + 1 overload
    static member GetNumericValue : c:char -> float + 1 overload
    ...
  end

Full name: System.Char
Char.IsDigit(c: char) : bool
Char.IsDigit(s: string, index: int) : bool
val GetSize : data:'T -> int (requires unmanaged)

Full name: Script.GetSize
val data : 'T (requires unmanaged)
val mutable value : 'T [] (requires unmanaged)
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn

More information

Link:http://fssnip.net/7XK
Posted:4 years ago
Author:me
Tags: data