简体   繁体   English

F#转换一个int

[英]F# casting an int

This is a quite trivial thing but I am really struggling to get this to work. 这是一件微不足道的事情,但是我真的很难使它生效。 I want to cast the results of sqrt n where n is of type int64 and finally pass that to a function that takes an int but I am really struggling to get a decent solution of it, this is the way I came up with but this is hideous and I can't believe that something that is so trivial to do in for example C# should be so hard in F#. 我想转换sqrt n的结果,其中n的类型为int64 ,最后将其传递给需要一个int的函数,但是我真的很想获得一个体面的解决方案,这是我想出的方式,但这是令人毛骨悚然,我无法相信在C#中如此琐碎的事情在F#中应该很难。

n 
|> float 
|> sqrt 
|> int 
|> function

This is F# - if you don't have what you want, write a function. 这是F#-如果您没有所需的功能,请编写一个函数。 For instance: 例如:

let inline sqrttoint n =
    (int (sqrt (float n)))

n |> sqrttoint |> function

On top of that it works on anything that can cast to float . 最重要的是,它适用于任何可以float

The main issue of C# vs F# is that you are used to C# where numeric types are auto-promoted whereas F# wants you to care about the type of most everything and changes in type need to be more explicit. C#与F#的主要问题在于,您习惯于C#会自动提升数字类型,而F#希望您关心大多数事物的类型,并且类型变化需要更加明确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM