Возможности LINQ в Ruby - PullRequest
       1

Возможности LINQ в Ruby

3 голосов
/ 09 ноября 2011

Я хотел бы написать код на Ruby, который ведет себя как этот код C #.

Он получает набор топологии-кандидата и мировой набор и проверяет, является ли топология-кандидат топологией по отношению к миру.

В C # с использованием функций LINQ это выглядит так:

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 ​​есть функции, и мне очень интересно посмотреть, как будет выглядеть эквивалентный код.

1 Ответ

3 голосов
/ 14 ноября 2011

Я перевел ваш код на 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

Надеюсь, это поможет

...