dll - F# 有自己的字符串操作库吗?

标签 dll f#

F# 有自己的字符串操作库吗?

当我尝试学习 F# 时,我发现自己在使用现有的 System.string 方法?

我应该这样做吗?

代码:

open System

type PhoneNumber = 
    { CountryCode:int
      Number:string }

// b. Create a function formatPhone that accepts a PhoneNumber record and formats it to look like something like this: "+44 1234 456789"  
let formatPhone phoneNumber =

    let getLeadingCharacters (length:int) (text:string) =
        text.Substring(0, length)

    let getLastCharacters (length:int) (text:string) =
        text.Substring(text.Length - length, length)

    printf "+%i %s %s" phoneNumber.CountryCode 
                       (phoneNumber.Number |> getLeadingCharacters 4)
                       (phoneNumber.Number |> getLastCharacters 6)

formatPhone { CountryCode=44; Number="123456789" };;

更新

更新函数来自:
let formatPhone phoneNumber =

    let getLeadingCharacters (length:int) (text:string) =
        text.Substring(0, length)

    let getLastCharacters (length:int) (text:string) =
        text.Substring(text.Length - length, length)

    printf "+%i %s %s" phoneNumber.CountryCode 
                       (phoneNumber.Number |> getLeadingCharacters 4)
                       (phoneNumber.Number |> getLastCharacters 6)

formatPhone { CountryCode=44; Number="123456789" };;

至:
let formatPhone phoneNumber =

    printf "+%i %s %s" phoneNumber.CountryCode 
                       phoneNumber.Number.[0..3]
                       phoneNumber.Number.[4..8]

formatPhone { CountryCode=44; Number="123456789" };;

最佳答案

不,F# 没有特定的 String library复制 .NET 库。它确实有 string module带有额外的字符串函数。
是的,使用 .NET 函数。
F# 可以利用所有 .NET 库这一事实是它最强大的功能之一。起初,将使用 curried 参数的函数与使用元组参数的 .NET 函数混合在一起似乎很奇怪。
这就是为什么在 NuGet 中您会看到也具有 FSharp 扩展的包的原因。例如MathNet NumericsMathNet Numerics FSharp .这些是允许 F# 习惯使用 .NET 库的包装函数。
在寻找与 F# 一起使用的函数和方法时,我经常使用这个技巧。
要搜索 .NET,请使用 class作为关键字并搜索 F# 特定代码,请使用 module作为关键字。
例如:
谷歌:MSDN string class第一项:String Class
谷歌:MSDN string module第一项:Core.String Module (F#) (现在是 404)但它也是 String 的 F# 语言引用的有效链接.有什么不对劲。

关于dll - F# 有自己的字符串操作库吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35366996/

相关文章:

.net - 不使用类表示受限类型

f# - 将字节序列转换为浮点序列 F# (seq<seq<byte> -> seq<seq<float>)

c++ - 从 dll 返回多个字符串

c++ - 将 char 数组添加到 LoadLibrary

.net - F# 中 int 类型的 NaN 等价物是什么?

f# - 学习F#: What books using other programming languages can be translated to F# to learn functional concepts?

c# - 什么数据结构类似于数据库?

c++ - 绕过 dll 中的 DrawString 不起作用

c# - MissingMethodException:没有为此对象定义无参数构造函数

c++ - 在 dll 中嵌入序言引擎