编程语言
首页 > 编程语言> > C#-Ruby中的LINQ功能

C#-Ruby中的LINQ功能

作者:互联网

我想用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#中缺乏一点…)

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

解决方法:

我将您的代码翻译成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

希望这可以帮助

标签:topology,linq,ruby,c
来源: https://codeday.me/bug/20191202/2086000.html