F#中的功能模板 [英] Function templates in F#

查看:63
本文介绍了F#中的功能模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Let's say I am solving a particular problem and come up with a function

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

现在,如果我的参数为int类型,则此功能正常运行.但是在某个地方,我需要将其提高到11,我需要额外地将其推入int64甚至BigInteger.那我该怎么办? 我复制并粘贴该函数,更改名称,然后搜寻所有使编译器认为该函数应在int上运行的文字外观.这很烂.

Now this function works fine if my parameters are of type int. But somewhere I will need it to go up to 11, I will need that extra push into int64 or even BigInteger. So what do I do? I copy&paste the function, change the name, and go hunting for all literal appearances that make the compiler think the function should operate on int. And this sucks.

有没有办法做到这一点:

Is there a way to do this:

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

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

where param1 and param2 can be any combo of integer types?

下面通过kvb扩展了一个很好的技巧,我想到了以下内容

Expanding a bit on a great tip by kvb below, I came up with the following

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

因此使用起来会变得不那么难看

so it becomes a bit less ugly when used

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

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

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

Now the question is should I use this? If not, why not?

推荐答案

我认为通用算术是.NET语言中的常见问题. 有很多文章介绍了不同的方法,不久我将发布另一篇文章来解释我的方法,与您发布的解决方案相似.

I think Generic Arithmetic is common problem in .NET languages. There are many articles explaining different approaches and very soon I will post another one explaining mine which is similar to the solution you posted.

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

Now, if you ask me if should you use it, I would say: as long as you understand what you are doing why not? I'm using it partially in production and have no issues at all, but because I care about run-time performance I use overloading to resolve everything at compile time. Then to speed up compile time I redefine basic math operators to operate in the same type, otherwise type signatures get really complicated and may take ages to compile.

还有更多要考虑的事情,但是对于您的特定问题,这里是示例代码:

There are more things to consider but for your specific problem here is a sample code:

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#中的功能模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆