Ruby 中的 LINQ 功能

发布于 2024-12-14 16:43:41 字数 1340 浏览 1 评论 0原文

我想用 Ruby 编写一段代码,其行为类似于 C# 代码。

它接收候选拓扑集和世界集,并测试候选拓扑是否是关于世界的拓扑。

在使用 LINQ 功能的 C# 中,它看起来像这样:

public static bool IsTopology<T>(IEnumerable<IEnumerable<T>> candidate, IEnumerable<T> world)
{
    IEqualityComparer<IEnumerable<T>> setComparer =
        new SetComparer<T>();

    if (!candidate.Contains(Enumerable.Empty<T>(), setComparer) ||
        !candidate.Contains(world, setComparer))
    {
        return false;
    }

    var pairs =
        from x in candidate
        from y in candidate
        select new {x,y};

    return pairs.All(pair => candidate.Contains(pair.x.Union(pair.y), setComparer) &&
        candidate.Contains(pair.x.Intersect(pair.y), setComparer));
}

public class SetComparer<T> : IEqualityComparer<IEnumerable<T>>        
{
    public bool Equals (IEnumerable<T> x, IEnumerable<T> y)
    {
        return new HashSet<T>(x).SetEquals(y);
    }

    public int GetHashCode (IEnumerable<T> obj)
    {
        return 0;
    }
}

我正在寻找的功能如下:

  • 将相等比较器插入方法的能力

  • 一种能力使用嵌套映射(和匿名类型)

  • 将数组作为集合进行比较的能力(不是很重要,在 C# 中它缺少一点...)< /p>

我相信 ruby​​ 具有这些功能,并且非常有兴趣了解等效代码的外观。

I would like to write a code in Ruby that behaves like this C# code.

It receives a candidate topology set and a world set, and tests if the candidate topology is a topology in respect to the world.

In C# using LINQ features it looks like this:

public static bool IsTopology<T>(IEnumerable<IEnumerable<T>> candidate, IEnumerable<T> world)
{
    IEqualityComparer<IEnumerable<T>> setComparer =
        new SetComparer<T>();

    if (!candidate.Contains(Enumerable.Empty<T>(), setComparer) ||
        !candidate.Contains(world, setComparer))
    {
        return false;
    }

    var pairs =
        from x in candidate
        from y in candidate
        select new {x,y};

    return pairs.All(pair => candidate.Contains(pair.x.Union(pair.y), setComparer) &&
        candidate.Contains(pair.x.Intersect(pair.y), setComparer));
}

public class SetComparer<T> : IEqualityComparer<IEnumerable<T>>        
{
    public bool Equals (IEnumerable<T> x, IEnumerable<T> y)
    {
        return new HashSet<T>(x).SetEquals(y);
    }

    public int GetHashCode (IEnumerable<T> obj)
    {
        return 0;
    }
}

The features I am looking for are the following:

  • An ability to plug an equality comparer to methods

  • An ability to use nested maps (and anonymous types)

  • An ability to compare arrays as sets (not very important, in C# it lacks a bit...)

I believe that ruby has the features and am very interested to see how the equivalent code would look like.

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

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

发布评论

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

评论(1

十年九夏 2024-12-21 16:43:50

我将你的代码翻译成 ruby​​(并重写了一点):

  # candidate - Enumerable of Enumerable; world - Enumerable; &block - comparer of two sets.
  def topology? candidate, world, &block
    require 'set'
    # you can pass block to this method or if no block passed it will use set comparison
    comparer = block || lambda { |ary1,ary2| ary1.to_set.eql?(ary2.to_set) }
    # create lambda-function to find a specified set in candidate (to reuse code)
    candidate_include = lambda { |to_find| candidate.find {|item| comparer.(item, to_find) } }

    return false if( !candidate_include.( []) || !candidate_include.( world) )

    pairs = candidate.to_a.repeated_permutation(2)

    pairs.all? do |x,y| x_set = x.to_set; y_set = y.to_set
        candidate_include.(x_set & y_set) && # or x_set.intersection y_set
        candidate_include.(x_set | y_set) # or x_set.union y_set
    end
  end

希望这有帮助

I translated your code to ruby (and rewritten it a bit):

  # candidate - Enumerable of Enumerable; world - Enumerable; &block - comparer of two sets.
  def topology? candidate, world, &block
    require 'set'
    # you can pass block to this method or if no block passed it will use set comparison
    comparer = block || lambda { |ary1,ary2| ary1.to_set.eql?(ary2.to_set) }
    # create lambda-function to find a specified set in candidate (to reuse code)
    candidate_include = lambda { |to_find| candidate.find {|item| comparer.(item, to_find) } }

    return false if( !candidate_include.( []) || !candidate_include.( world) )

    pairs = candidate.to_a.repeated_permutation(2)

    pairs.all? do |x,y| x_set = x.to_set; y_set = y.to_set
        candidate_include.(x_set & y_set) && # or x_set.intersection y_set
        candidate_include.(x_set | y_set) # or x_set.union y_set
    end
  end

Hope this helps

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