gpt4 book ai didi

c# - 有没有一种简单的方法可以在 C# 中堆叠比较运算符?

转载 作者:太空狗 更新时间:2023-10-29 21:31:53 24 4
gpt4 key购买 nike

在 c# 中,任何实现比较运算符的类型,如 < > ,很容易比较。例如我可以这样做:

var date1 = new DateTime(1000);
var date2 = new DateTime(2000);
var date3 = new DateTime(3000);
var result = date1 < date2; // true

但是,我无法执行以下操作

var result = date1 < date2 < date3; // error

this doesn't compile, since the first comparison returns a boolean, which isn't further compareable to other dates

所以我必须改为这样做(如果第一个 DateTime.CompareTo(DateTime) 更早,则 DateTime 返回 -1:

var result = date1.CompareTo(date2) + date2.CompareTo(date3) == -2; // true

或者简单地这样做:

var result = date1 < date2 && date2 < date3; // true

但是,我想知道是否有可能链接 <多次运算,为了写一些更易读的代码,当这个用于更复杂的场景时。

例如我需要这样做(当然不会编译):

result = 
date1 < date2 < date3 < date4 ||
date3 < date4 < date1 < date2 ||
date4 < date1 < date2 < date3 ||
date2 < date3 < date4 < date1

这将导致比上述可行的可能性更容易阅读的代码。

有没有简单的方法可以做到这一点,我需要自己实现吗?

最佳答案

这是我要做的:

public static class Extensions
{
public static bool InOrderAscending<T>(this IEnumerable<T> values)
where T : struct, IComparable
=>
!values.Zip(values.Skip(1), (value, nextValue) => value.CompareTo(nextValue))
.Any(x => x >= 0);

public static bool InOrderAscending<T>(params T[] values) where T : struct, IComparable
=> values.InOrderAscending();
}

工作原理如下:Zip()采用两个 IEnumerables 并枚举其中的项目它们是配对的:

var a = new[] { 1, 2, 3 };
var b = new[] { 4, 5, 6, 7 };

var zipped = a.Zip(b, (aitem, bitem) => $"{aitem},{bitem}").ToList();

压缩后将包含 { "1, 4", "2, 5", "3, 6" } .

请注意 7未使用:没有匹配项,因此被丢弃。这符合永远不必进行范围检查的 LINQ 哲学。

接下来,Skip(1)跳过一项并枚举其余项。

所以我正在做的是压缩两个序列:原始序列和原始序列的第二个到最后一个项目。

{a, b, c}
{b, c}

所以这将为我们提供 (a, b) 和 (b, c) 的序列。

这比比较 arg[i] 的可读性差至 arg[i+1] ,但它可以让您免于处理索引。

所以我们的 zip 表达式返回一系列比较结果。对于每个相邻的项目对,我们调用 CompareTo() 并返回结果。

public static bool InOrderDescending<T>(params T[] values) where T : struct, IComparable
{
List<int> comparisons =
values.Zip(values.Skip(1), (value, nextValue) => value.CompareTo(nextValue))
.ToList();

// Now we finish by checking that sequence of integers for any positive values,
// where a positive value means that `value` was greater than `nextValue`
var haveOutOfOrderItems = comparisons.Any(x => x >= 0);

// If none of the values were positive, we're in order.
return !haveOutOfOrderItems;
}

我只为值类型编写了这个方法,所以我不必担心空值。是null大于或小于 new Button()this.SettingsPage ?这取决于调用者,所以我会编写一个引用类型的重载,它采用 IComparer<T> 类型的参数。 ,或者只是一个 lambda(编辑:也许我们实际上应该编写一个扩展方法来执行自偏移压缩,但从 lambda 返回一些任意返回类型的序列;我们将使用它来编写这个)。

public static bool InOrderAscending<T>(this IEnumerable<T> values, Func<T, T, int> compare) 
where T : class
=>
!values.Zip(values.Skip(1), (value, nextValue) => compare(value, nextValue))
.Any(x => x >= 0);

关于c# - 有没有一种简单的方法可以在 C# 中堆叠比较运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56025567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com