4 people like it.
Like the snippet!
Generic number literal
A generic numeral G. It allows writing functions for arbitrary numeric type. The transformation is an efficient one, because it is implemented explicitly for every type.
It combines the type classes technique of FsControl (https://github.com/gmpl/FsControl and http://nut-cracker.azurewebsites.net/typeclasses-for-fsharp) with numeric literals. But FsControl is removed to completely avoid unnecessary function calls.
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:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
|
open System.Numerics
// optional
open MathNet.Numerics
module NumericLiteralG =
type GenericNumber = GenericNumber with
static member instance (GenericNumber, x:int32, _:int8) = fun () -> int8 x
static member instance (GenericNumber, x:int32, _:uint8) = fun () -> uint8 x
static member instance (GenericNumber, x:int32, _:int16) = fun () -> int16 x
static member instance (GenericNumber, x:int32, _:uint16) = fun () -> uint16 x
static member instance (GenericNumber, x:int32, _:int32) = fun () -> x
static member instance (GenericNumber, x:int32, _:uint32) = fun () -> uint32 x
static member instance (GenericNumber, x:int32, _:int64) = fun () -> int64 x
static member instance (GenericNumber, x:int32, _:uint64) = fun () -> uint64 x
static member instance (GenericNumber, x:int32, _:float32) = fun () -> float32 x
static member instance (GenericNumber, x:int32, _:float) = fun () -> float x
static member instance (GenericNumber, x:int32, _:bigint) = fun () -> bigint x
static member instance (GenericNumber, x:int32, _:decimal) = fun () -> decimal x
static member instance (GenericNumber, x:int32, _:Complex) = fun () -> Complex.op_Implicit x
static member instance (GenericNumber, x:int64, _:int64) = fun () -> int64 x
static member instance (GenericNumber, x:int64, _:uint64) = fun () -> uint64 x
static member instance (GenericNumber, x:int64, _:float32) = fun () -> float32 x
static member instance (GenericNumber, x:int64, _:float) = fun () -> float x
static member instance (GenericNumber, x:int64, _:bigint) = fun () -> bigint x
static member instance (GenericNumber, x:int64, _:decimal) = fun () -> decimal x
static member instance (GenericNumber, x:int64, _:Complex) = fun () -> Complex.op_Implicit x
static member instance (GenericNumber, x:string, _:float32) = fun () -> float32 x
static member instance (GenericNumber, x:string, _:float) = fun () -> float x
static member instance (GenericNumber, x:string, _:bigint) = fun () -> bigint.Parse x
static member instance (GenericNumber, x:string, _:decimal) = fun () -> decimal x
static member instance (GenericNumber, x:string, _:Complex) = fun () -> Complex(float x, 0.0)
// MathNet.Numerics
static member instance (GenericNumber, x:int32, _:Complex32) = fun () -> Complex32.op_Implicit x
static member instance (GenericNumber, x:int32, _:bignum) = fun () -> bignum.FromInt x
static member instance (GenericNumber, x:int64, _:Complex32) = fun () -> Complex32.op_Implicit x
static member instance (GenericNumber, x:int64, _:bignum) = fun () -> bignum.FromBigInt (bigint x)
static member instance (GenericNumber, x:string, _:Complex32) = fun () -> Complex32(float32 x, 0.0f)
static member instance (GenericNumber, x:string, _:bignum) = fun () -> bignum.FromBigInt (bigint.Parse x)
let inline genericNumber num = Inline.instance (GenericNumber, num) ()
let inline FromZero () = LanguagePrimitives.GenericZero
let inline FromOne () = LanguagePrimitives.GenericOne
let inline FromInt32 n = genericNumber n
let inline FromInt64 n = genericNumber n
let inline FromString n = genericNumber n
|
namespace System
namespace System.Numerics
namespace MathNet
namespace MathNet.Numerics
module NumericLiteralG
from Script
Multiple items
union case GenericNumber.GenericNumber: GenericNumber
--------------------
type GenericNumber =
| GenericNumber
static member instance : GenericNumber:GenericNumber * x:int32 * int8 -> (unit -> sbyte)
static member instance : GenericNumber:GenericNumber * x:int32 * uint8 -> (unit -> byte)
static member instance : GenericNumber:GenericNumber * x:int32 * int16 -> (unit -> int16)
static member instance : GenericNumber:GenericNumber * x:int32 * uint16 -> (unit -> uint16)
static member instance : GenericNumber:GenericNumber * x:int32 * int32 -> (unit -> int32)
static member instance : GenericNumber:GenericNumber * x:int32 * uint32 -> (unit -> uint32)
static member instance : GenericNumber:GenericNumber * x:int32 * int64 -> (unit -> int64)
static member instance : GenericNumber:GenericNumber * x:int32 * uint64 -> (unit -> uint64)
static member instance : GenericNumber:GenericNumber * x:int32 * float32 -> (unit -> float32)
static member instance : GenericNumber:GenericNumber * x:int32 * float -> (unit -> float)
static member instance : GenericNumber:GenericNumber * x:int32 * bigint -> (unit -> BigInteger)
static member instance : GenericNumber:GenericNumber * x:int32 * decimal -> (unit -> decimal)
static member instance : GenericNumber:GenericNumber * x:int32 * Complex -> (unit -> Complex)
static member instance : GenericNumber:GenericNumber * x:int64 * int64 -> (unit -> int64)
static member instance : GenericNumber:GenericNumber * x:int64 * uint64 -> (unit -> uint64)
static member instance : GenericNumber:GenericNumber * x:int64 * float32 -> (unit -> float32)
static member instance : GenericNumber:GenericNumber * x:int64 * float -> (unit -> float)
static member instance : GenericNumber:GenericNumber * x:int64 * bigint -> (unit -> BigInteger)
static member instance : GenericNumber:GenericNumber * x:int64 * decimal -> (unit -> decimal)
static member instance : GenericNumber:GenericNumber * x:int64 * Complex -> (unit -> Complex)
static member instance : GenericNumber:GenericNumber * x:string * float32 -> (unit -> float32)
static member instance : GenericNumber:GenericNumber * x:string * float -> (unit -> float)
static member instance : GenericNumber:GenericNumber * x:string * bigint -> (unit -> BigInteger)
static member instance : GenericNumber:GenericNumber * x:string * decimal -> (unit -> decimal)
static member instance : GenericNumber:GenericNumber * x:string * Complex -> (unit -> Complex)
static member instance : GenericNumber:GenericNumber * x:int32 * Complex32 -> (unit -> Complex32)
static member instance : GenericNumber:GenericNumber * x:int32 * bignum -> (unit -> BigRational)
static member instance : GenericNumber:GenericNumber * x:int64 * Complex32 -> (unit -> Complex32)
static member instance : GenericNumber:GenericNumber * x:int64 * bignum -> (unit -> BigRational)
static member instance : GenericNumber:GenericNumber * x:string * Complex32 -> (unit -> Complex32)
static member instance : GenericNumber:GenericNumber * x:string * bignum -> (unit -> BigRational)
Full name: Script.NumericLiteralG.GenericNumber
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int32 * int8 -> (unit -> sbyte)
Full name: Script.NumericLiteralG.GenericNumber.instance
val x : int32
Multiple items
val int32 : value:'T -> int32 (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int32
--------------------
type int32 = System.Int32
Full name: Microsoft.FSharp.Core.int32
Multiple items
val int8 : value:'T -> sbyte (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.int8
--------------------
type int8 = System.SByte
Full name: Microsoft.FSharp.Core.int8
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int32 * uint8 -> (unit -> byte)
Full name: Script.NumericLiteralG.GenericNumber.instance
Multiple items
val uint8 : value:'T -> byte (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.uint8
--------------------
type uint8 = System.Byte
Full name: Microsoft.FSharp.Core.uint8
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int32 * int16 -> (unit -> int16)
Full name: Script.NumericLiteralG.GenericNumber.instance
Multiple items
val int16 : value:'T -> int16 (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int16
--------------------
type int16 = System.Int16
Full name: Microsoft.FSharp.Core.int16
--------------------
type int16<'Measure> = int16
Full name: Microsoft.FSharp.Core.int16<_>
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int32 * uint16 -> (unit -> uint16)
Full name: Script.NumericLiteralG.GenericNumber.instance
Multiple items
val uint16 : value:'T -> uint16 (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.uint16
--------------------
type uint16 = System.UInt16
Full name: Microsoft.FSharp.Core.uint16
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int32 * int32 -> (unit -> int32)
Full name: Script.NumericLiteralG.GenericNumber.instance
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int32 * uint32 -> (unit -> uint32)
Full name: Script.NumericLiteralG.GenericNumber.instance
Multiple items
val uint32 : value:'T -> uint32 (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.uint32
--------------------
type uint32 = System.UInt32
Full name: Microsoft.FSharp.Core.uint32
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int32 * int64 -> (unit -> int64)
Full name: Script.NumericLiteralG.GenericNumber.instance
Multiple items
val int64 : value:'T -> int64 (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int64
--------------------
type int64 = System.Int64
Full name: Microsoft.FSharp.Core.int64
--------------------
type int64<'Measure> = int64
Full name: Microsoft.FSharp.Core.int64<_>
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int32 * uint64 -> (unit -> uint64)
Full name: Script.NumericLiteralG.GenericNumber.instance
Multiple items
val uint64 : value:'T -> uint64 (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.uint64
--------------------
type uint64 = System.UInt64
Full name: Microsoft.FSharp.Core.uint64
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int32 * float32 -> (unit -> float32)
Full name: Script.NumericLiteralG.GenericNumber.instance
Multiple items
val float32 : value:'T -> float32 (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.float32
--------------------
type float32 = System.Single
Full name: Microsoft.FSharp.Core.float32
--------------------
type float32<'Measure> = float32
Full name: Microsoft.FSharp.Core.float32<_>
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int32 * float -> (unit -> float)
Full name: Script.NumericLiteralG.GenericNumber.instance
Multiple items
val float : value:'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.float
--------------------
type float = System.Double
Full name: Microsoft.FSharp.Core.float
--------------------
type float<'Measure> = float
Full name: Microsoft.FSharp.Core.float<_>
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int32 * bigint -> (unit -> BigInteger)
Full name: Script.NumericLiteralG.GenericNumber.instance
type bigint = BigInteger
Full name: Microsoft.FSharp.Core.bigint
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int32 * decimal -> (unit -> decimal)
Full name: Script.NumericLiteralG.GenericNumber.instance
Multiple items
val decimal : value:'T -> decimal (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.decimal
--------------------
type decimal = System.Decimal
Full name: Microsoft.FSharp.Core.decimal
--------------------
type decimal<'Measure> = decimal
Full name: Microsoft.FSharp.Core.decimal<_>
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int32 * Complex -> (unit -> Complex)
Full name: Script.NumericLiteralG.GenericNumber.instance
Multiple items
type Complex =
struct
new : real:float * imaginary:float -> Complex
member Equals : obj:obj -> bool + 1 overload
member GetHashCode : unit -> int
member Imaginary : float
member Magnitude : float
member Phase : float
member Real : float
member ToString : unit -> string + 3 overloads
static val Zero : Complex
static val One : Complex
...
end
Full name: System.Numerics.Complex
--------------------
Complex()
Complex(real: float, imaginary: float) : unit
Complex.op_Implicit(value: float) : Complex
Complex.op_Implicit(value: float32) : Complex
Complex.op_Implicit(value: byte) : Complex
Complex.op_Implicit(value: sbyte) : Complex
Complex.op_Implicit(value: uint64) : Complex
Complex.op_Implicit(value: uint32) : Complex
Complex.op_Implicit(value: uint16) : Complex
Complex.op_Implicit(value: int64) : Complex
Complex.op_Implicit(value: int) : Complex
Complex.op_Implicit(value: int16) : Complex
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int64 * int64 -> (unit -> int64)
Full name: Script.NumericLiteralG.GenericNumber.instance
val x : int64
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int64 * uint64 -> (unit -> uint64)
Full name: Script.NumericLiteralG.GenericNumber.instance
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int64 * float32 -> (unit -> float32)
Full name: Script.NumericLiteralG.GenericNumber.instance
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int64 * float -> (unit -> float)
Full name: Script.NumericLiteralG.GenericNumber.instance
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int64 * bigint -> (unit -> BigInteger)
Full name: Script.NumericLiteralG.GenericNumber.instance
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int64 * decimal -> (unit -> decimal)
Full name: Script.NumericLiteralG.GenericNumber.instance
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int64 * Complex -> (unit -> Complex)
Full name: Script.NumericLiteralG.GenericNumber.instance
static member GenericNumber.instance : GenericNumber:GenericNumber * x:string * float32 -> (unit -> float32)
Full name: Script.NumericLiteralG.GenericNumber.instance
val x : string
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
static member GenericNumber.instance : GenericNumber:GenericNumber * x:string * float -> (unit -> float)
Full name: Script.NumericLiteralG.GenericNumber.instance
static member GenericNumber.instance : GenericNumber:GenericNumber * x:string * bigint -> (unit -> BigInteger)
Full name: Script.NumericLiteralG.GenericNumber.instance
BigInteger.Parse(value: string) : BigInteger
BigInteger.Parse(value: string, provider: System.IFormatProvider) : BigInteger
BigInteger.Parse(value: string, style: System.Globalization.NumberStyles) : BigInteger
BigInteger.Parse(value: string, style: System.Globalization.NumberStyles, provider: System.IFormatProvider) : BigInteger
static member GenericNumber.instance : GenericNumber:GenericNumber * x:string * decimal -> (unit -> decimal)
Full name: Script.NumericLiteralG.GenericNumber.instance
static member GenericNumber.instance : GenericNumber:GenericNumber * x:string * Complex -> (unit -> Complex)
Full name: Script.NumericLiteralG.GenericNumber.instance
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int32 * Complex32 -> (unit -> Complex32)
Full name: Script.NumericLiteralG.GenericNumber.instance
Multiple items
type Complex32 =
struct
new : real:float32 * imaginary:float32 -> Complex32
member CommonLogarithm : unit -> Complex32
member Conjugate : unit -> Complex32
member CubicRoots : unit -> Tuple<Complex32, Complex32, Complex32>
member Equals : other:Complex32 -> bool + 1 overload
member Exponential : unit -> Complex32
member GetHashCode : unit -> int
member Imaginary : float32
member IsImaginaryOne : unit -> bool
member IsInfinity : unit -> bool
...
end
Full name: MathNet.Numerics.Complex32
--------------------
Complex32()
Complex32(real: float32, imaginary: float32) : unit
Complex32.op_Implicit(value: float32) : Complex32
Complex32.op_Implicit(value: uint64) : Complex32
Complex32.op_Implicit(value: uint32) : Complex32
Complex32.op_Implicit(value: int64) : Complex32
Complex32.op_Implicit(value: BigInteger) : Complex32
Complex32.op_Implicit(value: int) : Complex32
Complex32.op_Implicit(value: uint16) : Complex32
Complex32.op_Implicit(value: sbyte) : Complex32
Complex32.op_Implicit(value: int16) : Complex32
Complex32.op_Implicit(value: byte) : Complex32
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int32 * bignum -> (unit -> BigRational)
Full name: Script.NumericLiteralG.GenericNumber.instance
type bignum = BigRational
Full name: MathNet.Numerics.bignum
static member BigRational.FromInt : int -> BigRational
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int64 * Complex32 -> (unit -> Complex32)
Full name: Script.NumericLiteralG.GenericNumber.instance
static member GenericNumber.instance : GenericNumber:GenericNumber * x:int64 * bignum -> (unit -> BigRational)
Full name: Script.NumericLiteralG.GenericNumber.instance
static member BigRational.FromBigInt : BigInteger -> BigRational
static member GenericNumber.instance : GenericNumber:GenericNumber * x:string * Complex32 -> (unit -> Complex32)
Full name: Script.NumericLiteralG.GenericNumber.instance
static member GenericNumber.instance : GenericNumber:GenericNumber * x:string * bignum -> (unit -> BigRational)
Full name: Script.NumericLiteralG.GenericNumber.instance
val genericNumber : num:'a -> 'b
Full name: Script.NumericLiteralG.genericNumber
val num : 'a
val FromZero : unit -> 'a (requires member get_Zero)
Full name: Script.NumericLiteralG.FromZero
module LanguagePrimitives
from Microsoft.FSharp.Core
val GenericZero<'T (requires member get_Zero)> : 'T (requires member get_Zero)
Full name: Microsoft.FSharp.Core.LanguagePrimitives.GenericZero
val FromOne : unit -> 'a (requires member get_One)
Full name: Script.NumericLiteralG.FromOne
val GenericOne<'T (requires member get_One)> : 'T (requires member get_One)
Full name: Microsoft.FSharp.Core.LanguagePrimitives.GenericOne
val FromInt32 : n:'a -> 'b
Full name: Script.NumericLiteralG.FromInt32
val n : 'a
val FromInt64 : n:'a -> 'b
Full name: Script.NumericLiteralG.FromInt64
val FromString : n:'a -> 'b
Full name: Script.NumericLiteralG.FromString
More information