C# 中作为方法参数的运算符

发布于 2024-08-07 09:04:16 字数 745 浏览 4 评论 0原文

我认为不可能在 C# 3.0 中使用运算符作为方法的参数,但是有没有办法模拟它或使用一些语法糖使其看起来像是正在发生的事情?

我问这个问题是因为我最近在 C# 中实现了画眉组合器 但在翻译Raganwald 的 Ruby 示例

(1..100).select(&:odd?).inject(&:+).into { |x| x * x }

其内容为“取出 1 到 100 之间的数字,保留奇数,将这些数字相加,然后计算该数字的平方。”

我对 Symbol#to_proc 的内容感到不满意。这就是上面 select(&:odd?)inject(&:+) 中的 &: 。

I don't think it's possible to use operators as a parameters to methods in C# 3.0 but is there a way to emulate that or some syntactic sugar that makes it seem like that's what's going on?

I ask because I recently implemented the thrush combinator in C# but while translating Raganwald's Ruby example

(1..100).select(&:odd?).inject(&:+).into { |x| x * x }

Which reads "Take the numbers from 1 to 100, keep the odd ones, take the sum of those, and then answer the square of that number."

I fell short on the Symbol#to_proc stuff. That's the &: in the select(&:odd?) and the inject(&:+) above.

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

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

发布评论

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

评论(2

醉态萌生 2024-08-14 09:04:16

嗯,简单来说,您可以只使用 lambda:

public void DoSomething(Func<int, int, int> op)
{
    Console.WriteLine(op(5, 2));
}

DoSomething((x, y) => x + y);
DoSomething((x, y) => x * y);
// etc

但这并不是很令人兴奋。如果能为我们预先构建所有这些代表,那就太好了。当然,您可以使用静态类来做到这一点:

public static class Operator<T>
{
     public static readonly Func<T, T, T> Plus;
     public static readonly Func<T, T, T> Minus;
     // etc

     static Operator()
     {
         // Build the delegates using expression trees, probably
     }
}

事实上,Marc Gravell 已经完成了 如果您想查看,MiscUtil 中的内容非常相似。然后你可以调用:

DoSomething(Operator<int>.Plus);

它并不完全漂亮,但我相信它是目前支持的最接近的。

恐怕我真的不懂 Ruby 的东西,所以我无法对此发表评论......

Well, in simple terms you can just use a lambda:

public void DoSomething(Func<int, int, int> op)
{
    Console.WriteLine(op(5, 2));
}

DoSomething((x, y) => x + y);
DoSomething((x, y) => x * y);
// etc

That's not very exciting though. It would be nice to have all those delegates prebuilt for us. Of course you could do this with a static class:

public static class Operator<T>
{
     public static readonly Func<T, T, T> Plus;
     public static readonly Func<T, T, T> Minus;
     // etc

     static Operator()
     {
         // Build the delegates using expression trees, probably
     }
}

Indeed, Marc Gravell has done something very similar in MiscUtil, if you want to look. You could then call:

DoSomething(Operator<int>.Plus);

It's not exactly pretty, but it's the closest that's supported at the moment, I believe.

I'm afraid I really don't understand the Ruby stuff, so I can't comment on that...

顾北清歌寒 2024-08-14 09:04:16

以下是直接的、字面的(尽可能多的)C# 翻译:

(Func<int>)(x => x * x)(
    Enumerable.Range(1, 100)
        .Where(x => x % 2 == 1)
        .Aggregate((x, y) => x + y))

具体来说:

  • 块:{||} - 成为 lambda:=>
  • select 变为 Where
  • inject 变为 Aggregate
  • into 变为对 lambda 实例的直接调用

The following is direct, literal (as much as possible) C# translation:

(Func<int>)(x => x * x)(
    Enumerable.Range(1, 100)
        .Where(x => x % 2 == 1)
        .Aggregate((x, y) => x + y))

Specifically:

  • blocks: {||} - become lambdas: =>
  • select becomes Where
  • inject becomes Aggregate
  • into becomes a direct call on a lambda instance
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文