gpt4 book ai didi

f# - F# 中的函数模板

转载 作者:行者123 更新时间:2023-12-05 00:58:50 25 4
gpt4 key购买 nike

假设我正在解决一个特定的问题并提出一个函数

let function parameter1 ... = 
a lot of adding, multiplying & so on with a lot of
literals all over the place

现在,如果我的参数是 int 类型,这个函数就可以正常工作。但是在某个地方我需要将它增加到 11,我需要额外插入 int64 甚至 BigInteger。那我该怎么办?
我复制并粘贴函数,更改名称,并寻找所有使编译器认为函数应该在 int 上运行的文字外观。这很糟糕。

有没有办法做到这一点:
let greatFunction param1 param2 = (param1+1)/(param2*2)

其中 param1 和 param2 可以是整数类型的任何组合?

编辑:

对下面 kvb 的一个重要提示进行了扩展,我想出了以下内容
module NumericLiteralG 

let inline FromZero() = LanguagePrimitives.GenericZero
let inline FromOne() = LanguagePrimitives.GenericOne
let inline FromInt32 n =
let rec loop nIn nOut =
if nIn>0 then loop (nIn - 1) (nOut + LanguagePrimitives.GenericOne)
else nOut
loop n LanguagePrimitives.GenericZero

所以使用时会变得不那么难看
let inline halfSquare num =
let res = num / 2G
res * res

let solve1 = halfSquare 5I
let solve2 = halfSquare 5.0
let solve3 = halfSquare 5uy

现在的问题是我应该使用它吗?如果没有,为什么不呢?

最佳答案

我认为泛型算术是 .NET 语言中的常见问题。
有很多文章解释了不同的方法,很快我会发布另一篇解释我的文章,这类似于您发布的解决方案。

现在,如果你问我是否应该使用它,我会说:只要你明白你在做什么,为什么不呢?我在生产中部分使用它并且完全没有问题,但是因为我关心运行时性能,所以我在编译时使用重载来解决所有问题。
然后为了加快编译时间,我重新定义了基本数学运算符以在相同类型中运行,否则类型签名会变得非常复杂并且可能需要很长时间来编译。

还有更多事情需要考虑,但对于您的具体问题,这里有一个示例代码:

open System.Numerics

type FromInt = FromInt with
static member ($) (FromInt, _:sbyte ) = fun (x:int) -> sbyte x
static member ($) (FromInt, _:int16 ) = fun (x:int) -> int16 x
static member ($) (FromInt, _:int32 ) = id
static member ($) (FromInt, _:float ) = fun (x:int) -> float x
static member ($) (FromInt, _:float32 ) = fun (x:int) -> float32 x
static member ($) (FromInt, _:int64 ) = fun (x:int) -> int64 x
static member ($) (FromInt, _:nativeint ) = fun (x:int) -> nativeint x
static member ($) (FromInt, _:byte ) = fun (x:int) -> byte x
static member ($) (FromInt, _:uint16 ) = fun (x:int) -> uint16 x
static member ($) (FromInt, _:char ) = fun (x:int) -> char x
static member ($) (FromInt, _:uint32 ) = fun (x:int) -> uint32 x
static member ($) (FromInt, _:uint64 ) = fun (x:int) -> uint64 x
static member ($) (FromInt, _:unativeint) = fun (x:int) -> unativeint x
static member ($) (FromInt, _:bigint ) = fun (x:int) -> bigint x
static member ($) (FromInt, _:decimal ) = fun (x:int) -> decimal x
static member ($) (FromInt, _:Complex ) = fun (x:int) -> Complex(float x,0.0)

let inline fromInt (a:int) : ^t = (FromInt $ Unchecked.defaultof< ^t>) a

module NumericLiteralG =
let inline FromZero() =LanguagePrimitives.GenericZero
let inline FromOne() = LanguagePrimitives.GenericOne
let inline FromInt32 (i:int) = fromInt i


// This will reduce the number of types inferred, will reduce compile time too.
let inline (+) (a:^t) (b:^t) : ^t = a + b
let inline (-) (a:^t) (b:^t) : ^t = a - b
let inline (*) (a:^t) (b:^t) : ^t = a * b
let inline (/) (a:^t) (b:^t) : ^t = a / b
let inline (~-) (a:^t) : ^t = -a


let inline halfSquare num =
let res = num / 2G
res * res

let solve1 = halfSquare 5I
let solve2 = halfSquare 5.0
let solve3 = halfSquare 5uy

// Define more generic math functions.

关于f# - F# 中的函数模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11562037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com