【问题标题】:Function templates in F#F# 中的函数模板
【发布时间】:2012-07-18 16:59:57
【问题描述】:

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

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

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

【问题讨论】:

  • 我认为一般来说你需要一个Numeric 类/接口,这在.net 中不存在(还没有?)。但也许一些特殊的 F# 东西可以帮助你。看看this question
  • 你为什么要问你是否应该使用这个?使用 GenericXXX 的内联函数是在 F# 中执行此操作的明确方法。
  • 你好丹尼尔。我是 F# 的新手,我真的在尝试,这意味着我努力以实用的方式来做这件事,即使我脑海中尖叫的解决方案都是循环和副作用以及 goto 和诸如此类的东西。现在,来自数字电子/微控制器背景的我非常不知道上面的工作原理。我曾经知道我的代码中每条指令的时间。我曾经手动优化我的编译器输出。所以这是一个诚实的问题,针对比我更了解这个主题的人。 FWIW 我可能正在做最愚蠢的事情而没有意识到这一点。
  • 从那个背景来看,我会说这与 C++ 模板相当。注意inline 无处不在。
  • 这就是我问的一个原因:stackoverflow.com/questions/2840714/… 这家伙显然抄袭了我的辛勤工作并在 2 年前在这里发布。我的 Google Fu 很弱。

标签: f# inline


【解决方案1】:

我认为通用算术是 .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.

【讨论】:

  • 这看起来棒极了,我什至看不懂它的一半。非常感谢。
【解决方案2】:

这里是an article on generic, numeric calculations in F#。一般来说,您有两种选择:

  • 静态成员约束
  • 全局数字关联(在 F# PowerPack 中可用)

...或者您可以结合这些技术。

在你的情况下,听起来静态约束会起作用。

该文章中的一个简单示例:

let inline halfSquare num =
   let res = LanguagePrimitives.DivideByInt num 2
   res * res

【讨论】:

  • 那是一篇非常有趣的文章。没有真正专注于整数->整数的东西,这让我想知道这是否是我所要求的在 F# 中真的那么不寻常?或者它是微不足道的,每个人都知道,我是唯一一个在互联网上要求它的人?
【解决方案3】:

一种方法是结合inline 关键字和来自LanguagePrimitives 模块的通用位:

let inline greatFunction param1 param2 = 
    let one = LanguagePrimitives.GenericOne
    let two = one + one
    (param1+one)/(param2*two)

// Usage
let f1 = greatFunction 4 2
let f2 = greatFunction 4L 2L
let f3 = greatFunction 4I 2I

【讨论】:

  • 同时,在阅读了上面的文章后,我想到了这个: let inline genericN n = let rec loop nIn nOut = if nInGenericZero then loop (nIn - GenericOne) (nOut + GenericOne) else nOut 循环 n GenericZero let inline halfSquare num = let res = num / (genericN 2) res * res 现在的问题是这是多么无效和丑陋?或者就是这样?顺便说一句,我放弃编辑这个答案,代码将保持内联,因为它就是这样。
  • 是的,它既丑陋又低效。也许这些就是你不经常看到它的原因。
【解决方案4】:

虽然不理想,并且有点绕过您的主要问题,但您可以添加类型注释以强制编译器处理:

let greatFunction (param1:int64) (param2:int64) : int64 = (param1+1)/(param2*2)

当然,现在 F# 中没有隐式转换,因此您需要将 L 添加到所有数字文字中,但它们至少会显示为编译器错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 2010-12-20
    相关资源
    最近更新 更多