简体   繁体   English

是否有等同于C的逗号运算符的惯用C#?

[英]Is there idiomatic C# equivalent to C's comma operator?

I'm using some functional stuff in C# and keep getting stuck on the fact that List.Add doesn't return the updated list. 我在C#中使用了一些功能性的东西,并不断陷入List.Add不返回更新列表的事实。

In general, I'd like to call a function on an object and then return the updated object. 通常,我想在对象上调用一个函数,然后返回更新后的对象。

For example it would be great if C# had a comma operator: 例如,如果C#具有逗号运算符,那就太好了:

((accum, data) => accum.Add(data), accum)

I could write my own "comma operator" like this: 我可以这样写自己的“逗号运算符”:

static T comma(Action a, Func<T> result) {
    a();
    return result();
}

It looks like it would work but the call site would ugly. 看起来可以使用,但是呼叫站点很难使用。 My first example would be something like: 我的第一个例子是:

((accum, data) => comma(accum.Add(data), ()=>accum))

Enough examples! 足够的例子! What's the cleanest way to do this without another developer coming along later and wrinkling his or her nose at the code smell? 没有其他开发人员随后出现并在代码气味中皱起鼻子的最干净方法是什么?

I know this as Fluent . 我知道这是流利的

A Fluent example of a List.Add using Extension Methods 使用扩展方法添加列表的流利示例

static List<T> MyAdd<T>(this List<T> list, T element)
{
    list.Add(element);
    return list;
}

I know that this thread is very old, but I want to append the following information for future users: 我知道此线程非常旧,但是我想为以后的用户添加以下信息:

There isn't currently such an operator. 当前没有这样的运算符。 During the C# 6 development cycle a semicolon operator was added, as: 在C#6开发周期中,添加了semicolon operator ,如下所示:

int square = (int x = int.Parse(Console.ReadLine()); Console.WriteLine(x - 2); x * x);

which can be translated as follows: 可以翻译如下:

int square = compiler_generated_Function();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int compiler_generated_Function()
{
    int x = int.Parse(Console.ReadLine());

    Console.WriteLine(x - 2);

    return x * x;
}

However, this feature was dropped before the final C# release . 但是, 此功能在最终C#发行版之前已被删除

This is what Concat http://msdn.microsoft.com/en-us/library/vstudio/bb302894%28v=vs.100%29.aspx is for. 这就是Concat http://msdn.microsoft.com/zh-cn/library/vstudio/bb302894%28v=vs.100%29.aspx的用途。 Just wrap a single item in an array. 只需将单个项目包装在数组中即可。 Functional code should not mutate the original data. 功能代码不应更改原始数据。 If performance is a concern, and this isn't good enough, then you'll no longer be using the functional paradigm. 如果性能是一个问题,而这还不够好,那么您将不再使用功能范式。

((accum, data) => accum.Concat(new[]{data}))

您自然可以在C#3.0中使用代码块来完成几乎第一个示例。

((accum, data) => { accum.Add(data); return accum; })

The extension method is arguably the best solution, but for completeness' sake, don't forget the obvious alternative: a wrapper class. 扩展方法可以说是最好的解决方案,但是出于完整性考虑,请不要忘记显而易见的替代方法:包装器类。

public class FList<T> : List<T>
{
    public new FList<T> Add(T item)
    {
        base.Add(item);
        return this;
    }

    public new FList<T> RemoveAt(int index)
    {
        base.RemoveAt(index);
        return this;
    }

    // etc...
}

{
     var list = new FList<string>();
     list.Add("foo").Add("remove me").Add("bar").RemoveAt(1);
}

I thought it would be interesting to make a version of my wrapper class answer that doesn't require you write the wrapper methods. 我认为制作一个不需要您编写包装器方法的包装器类答案版本会很有趣。

public class FList<T> : List<T>
{
    public FList<T> Do(string method, params object[] args)
    {
        var methodInfo = GetType().GetMethod(method);

        if (methodInfo == null)
            throw new InvalidOperationException("I have no " + method + " method.");

        if (methodInfo.ReturnType != typeof(void))
            throw new InvalidOperationException("I'm only meant for void methods.");

        methodInfo.Invoke(this, args);
        return this;
    }
}

{
    var list = new FList<string>();

    list.Do("Add", "foo")
        .Do("Add", "remove me")
        .Do("Add", "bar")
        .Do("RemoveAt", 1)
        .Do("Insert", 1, "replacement");

    foreach (var item in list)
        Console.WriteLine(item);    
}

Output: 输出:

foo 
replacement 
bar

EDIT 编辑

You can slim down the syntax by exploiting C# indexed properties. 您可以通过利用C#索引属性来简化语法。

Simply add this method: 只需添加此方法:

public FList<T> this[string method, params object[] args]
{
    get { return Do(method, args); }
}

And the call now looks like: 现在,呼叫看起来像:

list = list["Add", "foo"]
           ["Add", "remove me"]
           ["Add", "bar"]
           ["RemoveAt", 1]
           ["Insert", 1, "replacement"];

With the linebreaks being optional, of course. 当然,换行符是可选的。

Just a bit of fun hacking the syntax. 修改语法有点有趣。

Another technique, straight from functional programming, is as follows. 直接来自函数编程的另一种技术如下。 Define an IO struct like this: 定义这样的IO结构:

/// <summary>TODO</summary>
public struct IO<TSource> : IEquatable<IO<TSource>> {
    /// <summary>Create a new instance of the class.</summary>
    public IO(Func<TSource> functor) : this() { _functor = functor; }

    /// <summary>Invokes the internal functor, returning the result.</summary>
    public TSource Invoke() => (_functor | Default)();

    /// <summary>Returns true exactly when the contained functor is not null.</summary>
    public bool HasValue => _functor != null;

    X<Func<TSource>> _functor { get; }

    static Func<TSource> Default => null;
}

and make it a LINQ-able monad with these extension methods: 并使用以下扩展方法使其成为LINQ可用的monad:

[SuppressMessage("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces")]
public static class IO {
    public static IO<TSource> ToIO<TSource>( this Func<TSource> source) {
        source.ContractedNotNull(nameof(source));
        return new IO<TSource>(source);
    }

    public static IO<TResult> Select<TSource,TResult>(this IO<TSource> @this,
        Func<TSource,TResult> projector
    ) =>
        @this.HasValue && projector!=null
             ? New(() => projector(@this.Invoke()))
             : Null<TResult>();

    public static IO<TResult> SelectMany<TSource,TResult>(this IO<TSource> @this,
        Func<TSource,IO<TResult>> selector
    ) =>
        @this.HasValue && selector!=null
             ? New(() => selector(@this.Invoke()).Invoke())
             : Null<TResult>();

    public static IO<TResult> SelectMany<TSource,T,TResult>(this IO<TSource> @this,
        Func<TSource, IO<T>> selector,
        Func<TSource,T,TResult> projector
    ) =>
        @this.HasValue && selector!=null && projector!=null
             ? New(() => { var s = @this.Invoke(); return projector(s, selector(s).Invoke()); } )
             : Null<TResult>();

    public static IO<TResult> New<TResult> (Func<TResult> functor) => new IO<TResult>(functor);

    private static IO<TResult> Null<TResult>() => new IO<TResult>(null);
}

and now you can use the LINQ comprehensive syntax thus: 现在您可以使用LINQ 综合语法,因此:

using Xunit;
[Fact]
public static void IOTest() {
    bool isExecuted1 = false;
    bool isExecuted2 = false;
    bool isExecuted3 = false;
    bool isExecuted4 = false;
    IO<int> one = new IO<int>( () => { isExecuted1 = true; return 1; });
    IO<int> two = new IO<int>( () => { isExecuted2 = true; return 2; });
    Func<int, IO<int>> addOne = x => { isExecuted3 = true; return (x + 1).ToIO(); };
    Func<int, Func<int, IO<int>>> add = x => y => { isExecuted4 = true; return (x + y).ToIO(); };

    var query1 = ( from x in one
                   from y in two
                   from z in addOne(y)
                   from _ in "abc".ToIO()
                   let addOne2 = add(x)
                   select addOne2(z)
                 );
    Assert.False(isExecuted1); // Laziness.
    Assert.False(isExecuted2); // Laziness.
    Assert.False(isExecuted3); // Laziness.
    Assert.False(isExecuted4); // Laziness.
    int lhs = 1 + 2 + 1;
    int rhs = query1.Invoke().Invoke();
    Assert.Equal(lhs, rhs); // Execution.

    Assert.True(isExecuted1);
    Assert.True(isExecuted2);
    Assert.True(isExecuted3);
    Assert.True(isExecuted4);
}

When one desires an IO monad that composes but returns only void , define this struct and dependent methods: 当需要组成但仅返回void的IO monad时,请定义此struct和相关方法:

public struct Unit : IEquatable<Unit>, IComparable<Unit> {
    [CLSCompliant(false)]
    public static Unit _ { get { return _this; } } static Unit _this = new Unit();
}

public static IO<Unit> ConsoleWrite(object arg) =>
    ReturnIOUnit(() => Write(arg));

public static IO<Unit> ConsoleWriteLine(string value) =>
    ReturnIOUnit(() => WriteLine(value));

public static IO<ConsoleKeyInfo> ConsoleReadKey() => new IO<ConsoleKeyInfo>(() => ReadKey());

which readily allow the writing of code fragments like this: 可以轻松编写如下代码片段:

from pass  in Enumerable.Range(0, int.MaxValue)
let counter = Readers.Counter(0)
select ( from state in gcdStartStates
         where _predicate(pass, counter())
         select state )
into enumerable
where ( from _   in Gcd.Run(enumerable.ToList()).ToIO()
        from __  in ConsoleWrite(Prompt(mode))
        from c   in ConsoleReadKey()
        from ___ in ConsoleWriteLine()
        select c.KeyChar.ToUpper() == 'Q' 
      ).Invoke()
select 0;

where the old C comma operator is readily recognized for what it is: a monadic compose operation. 在旧的C 逗号操作符是容易识别它是什么:一个单子合成操作。

The true merit of the comprehension syntax is apparent when one attempts to write that fragment in the flunt style: 当人们尝试以直截了当的样式编写该片段时,理解语法的真正优点显而易见:

( Enumerable.Range(0,int.MaxValue)
            .Select(pass => new {pass, counter = Readers.Counter(0)})
            .Select(_    => gcdStartStates.Where(state => _predicate(_.pass,_.counter()))
                                          .Select(state => state)
                   )
).Where(enumerable => 
   ( (Gcd.Run(enumerable.ToList()) ).ToIO()
        .SelectMany(_ => ConsoleWrite(Prompt(mode)),(_,__) => new {})
        .SelectMany(_ => ConsoleReadKey(),          (_, c) => new {c})
        .SelectMany(_ => ConsoleWriteLine(),        (_,__) => _.c.KeyChar.ToUpper() == 'Q')
    ).Invoke()
).Select(list => 0);

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

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