F#:有什么方法可以将成员函数用作未绑定函数吗?

发布于 2024-09-05 12:24:57 字数 425 浏览 1 评论 0原文

有没有办法提取成员函数并将它们用作 F# 函数?我希望能够编写以下内容:

mystring |> string.Split '\n' |> Array.filter (string.Length >> (=) 0 >> not)

如果您[让],上面的代码就可以工作

let mystring = "a c\nb\n"
let stringSplit (y:char) (x:string) = x.Split(y)
let stringLength (x:string) = x.Length
mystring |> stringSplit '\n' |> Array.filter (stringLength >> (=) 0 >> not)

Is there a way to extract member functions, and use them as F# functions? I'd like to be able to write the following:

mystring |> string.Split '\n' |> Array.filter (string.Length >> (=) 0 >> not)

The code above works if you [let]

let mystring = "a c\nb\n"
let stringSplit (y:char) (x:string) = x.Split(y)
let stringLength (x:string) = x.Length
mystring |> stringSplit '\n' |> Array.filter (stringLength >> (=) 0 >> not)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

层林尽染 2024-09-12 12:24:57

这与我几天前问的一个问题非常相似(但你的措辞是更好的)。共识似乎是:

  1. 不。
  2. 也许语法 string#Split"foo"#Split,或者只是 #Split (类型推断) 将来会添加。但是 Tomas 链接到的 Don Syme 的提案是 2007 年的,所以我不知道我不知道它发生的可能性有多大——我猜,可能与懒惰的特定语法一样可能。

编辑:

我猜 "foo"#Split 也可以写成 string#Split "foo"。我想这取决于您定义 # 语法的灵活性。

This is quite similar to a question I asked a few days ago (but your wording is better). The consensus seems to be:

  1. No.
  2. Maybe the syntax string#Split, "foo"#Split, or just #Split (type-inferred) will be added in the future. But Don Syme's proposal that Tomas linked to was from 2007, so I don't know how likely it is to happen--probably about as likely as a specific syntax for laziness, I'd guess.

Edit:

I guess "foo"#Split could also be written as string#Split "foo". I guess it depends how flexibly you define the # syntax.

悟红尘 2024-09-12 12:24:57

用一下

(fun x -> x.Member ...)

暂时 。例如

someString |> (fun s -> s.Split "\n") |> ...

Use

(fun x -> x.Member ...)

for now. For example

someString |> (fun s -> s.Split "\n") |> ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文