Blog Archive

Thursday, June 22

Ruby's Enumerable mixin again

On the topic of Ruby's Enumerable mixin, it might be interesting to compare them to C# 2.0's Array/List<T> methods—static for Array, instance for List<T>—and 3.0's Standard Query Operators (System.Query namespace).

In the cases where C# does not support the method, I'd recommend following the link to the ruby docs. There are some rather interesting methods, and a lot can be learned from looking at what other languages support. In fact, some of these methods (each_slice, each_with_index) might be worth implementing as extension methods in C# (a trivial excercise left to the reader).

Ruby Enumerable C# 2.0 Array/List<T> methods* C# 3.0 System.Query
all?
all? { |obj| block }
bool TrueForAll(Predicate<T> match) bool All<T>(this IEnumerable<T> source, Func<T, bool> predicate)
any?
any? { |obj| block }
bool Exists(Predicate<T> match) bool Any<T>(this IEnumerable<T> source, Func<T, bool> predicate)
collect { |obj| block }
map { |obj| block}
List<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter) IEnumerable<S> Select<T, S>(this IEnumerable<T> source, Func<T, S> selector)
detect(ifnone = nil) { |obj| block }
find(ifnone = nil) { |obj| block }
T Find(Predicate<T> match) T First<T>(this IEnumerable<T> source, Func<T, bool> predicate)
T FirstOrDefault<T>(this IEnumerable<T> source, Func<T, bool> predicate)
each_cons(n) {...}
each_slice(n) {...}
each_with_index { |obj, index| block }
to_a
entries
T[] ToArray<T>(this IEnumerable<T> source)
List<T> ToList<T>(this IEnumerable<T> source)
find_all { |obj| block }
select { |obj| block }
List<T> FindAll(Predicate<T> match) IEnumerable<T> Where(this IEnumerable<T> source, Func<T, bool> predicate)
grep(pattern) FindAll could certainly specify a predicate that applied a regular expression against each item of the collection. same for Where.
include?(obj) bool Contains(T item)
inject(initial) { |memo, obj| block }
inject { |memo, obj| block }
U Aggregate<T, U>(this IEnumerable<T> source, U seed, Func<U, T, U> func)
max / min
max / min { |a, b| block }
T Max/Min<T>(this IEnumerable<T> source), etc. Note that while there are a few overloads for this method, it looks like the delegates passed in merely provide value extraction. I don't see an overload where you can provide your own IComparer<T> (which is what the second Ruby method does)
partition { |obj| block }
reject { |obj| block } merely returns the collection of the inverse of find_all / select
sort
sort { |a, b | block }
sort_by { |obj| block }
Sort()
Sort(IComparer<T> comparison)
OrderedSequence<T, K> OrderBy/OrderByDescending/ThenBy/ThenByDescending(
  this IEnumerable<T> source, Func<T, K> keySelector)

OrderedSequence<T> OrderBy/OrderByDescending/ThenBy/ThenByDescending(
  this IEnumerable<T> source, Func<T, K> keySelector, IComparer<K> comparer)
to_set
zip(arg, ...)
zip(arg, ...) { |arr| block }

No comments: