Questions tagged [enumerable]

"enumerable" refers to an ordering scheme that enables items in a set, sequence or collection to be readily addressed or traversed.

Enumerable is a Ruby module which can be included in other Ruby classes in order to allow iteration via #map/#collect, #select, #each, and so on.

Enumerable also manifests as the class Enumerator (documentation), which is returned when an iteration method is called without a block (e.g., array.each instead of array.each { |x| puts x }).

Enumerable objects may also utilize chained iteration, e.g., array.each.with_indices instead of array.each_with_indices.

Ruby 2.0 also introduces the concept of lazy enumeration.

In .NET, Enumerable is the class that wraps most common LINQ extension methods.

603 questions
11
votes
2 answers

Why tuples are not enumerable in Elixir?

I need an efficient structure for array of thousands of elements of the same type with ability to do random access. While list is most efficient on iteration and prepending, it is too slow on random access, so it does not fit my needs. Map works…
raacer
  • 5,302
  • 3
  • 27
  • 46
11
votes
4 answers

Why is Enumerable.Range faster than a direct yield loop?

The code below is checking performance of three different ways to do same solution. public static void Main(string[] args) { // for loop { Stopwatch sw = Stopwatch.StartNew(); int accumulator = 0; …
Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230
11
votes
3 answers

Why does Enumerable not have a length attribute in Ruby?

At least in Ruby 1.9.3, Enumerable objects do not have a length attribute. Why is this?
kdbanman
  • 10,161
  • 10
  • 46
  • 78
11
votes
2 answers

Enumerable range in descending order

I am binding a combobox using enumerable.range() and it works fine. Now I am trying to display the results in descending order, how can I do that? cboYearList.ItemsSource = Enumerable.Range(…
user4376581
11
votes
6 answers

Random array using LINQ and C#

I was reading an article on MSDN Magazine about using the Enumerable class in LINQ to generate a random array. The article uses VB.NET and I'm not immediately sure what the equivalent is in C#: Dim rnd As New System.Random() Dim numbers =…
Ryan
  • 7,835
  • 2
  • 29
  • 36
11
votes
5 answers

Why does Enumerable#detect need a Proc/lambda?

Enumerable#detect returns the first value of an array where the block evaluates to true. It has an optional argument that needs to respond to call and is invoked in this case, returning its value. So, (1..10).detect(lambda{ "none" }){|i| i == 11}…
Maxim Schmidt
  • 147
  • 12
11
votes
3 answers

Reversing enumerable in Ruby

I'm trying to reverse Enumerable (like Array) without using reverse method, but using reverse_each iterator. I hoped, that following code is enough: p [1,2,3].reverse_each {|v| v } however the block doesn't return array in reversed orded. Of course…
Mateusz Chromiński
  • 2,742
  • 4
  • 28
  • 45
10
votes
1 answer

IEnumerable.Cast() vs casting in IEnumerable.Select()

Suppose I have an IEnumerable and I want these to be converted into their ASCII-equivalent characters. For a single integer, it would just be (char)i, so there's always collection.Select(i => (char)i), but I thought it would be a tad cleaner to…
hehewaffles
  • 582
  • 5
  • 17
10
votes
5 answers

Is there an inverse 'member?' method in ruby?

I often find myself checking if some value belongs to some set. As I understand, people normally use Enumerable#member? for this. end_index = ['.', ','].member?(word[-1]) ? -3 : -2 However, this feels a little less elegant than most of things in…
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
10
votes
2 answers

Code contracts, forall and custom enumerable

I am using C# 4.0 and Code Contracts and I have my own custom GameRoomCollection : IEnumerable. I want to ensure, that no instances of GameRoomCollection will ever contain a null value element. I don't seem to be able to this, though.…
Stephan
  • 101
  • 4
10
votes
1 answer

rails - when using Group_by - How to get an Index?

I have the following: sets = DataSet.all.group_by{ |data| [data.project_id, "-", data.thread_id].join(" ") } <% sets.each do |range, datas| %>

<%= range %>:

<% datas.each do |data| %> <%=data%>

Last Post<%=…

AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
10
votes
3 answers

Ruby block taking array or multiple parameters

Today I was surprised to find ruby automatically find the values of an array given as a block parameter. For example: foo = "foo" bar = "bar" p foo.chars.zip(bar.chars).map { |pair| pair }.first #=> ["f", "b"] p foo.chars.zip(bar.chars).map { |a, b|…
mbigras
  • 7,664
  • 11
  • 50
  • 111
10
votes
1 answer

Why does Enumerable.ToLookup<>() return an ILookup<,> and not a Lookup<,>?

There is one method in Lookup<,> that is not in ILookup<,>: public IEnumerable ApplyResultSelector( Func, TResult> resultSelector); Why is the return type of Enumerable.ToLookup<>() declared to be…
Timwi
  • 65,159
  • 33
  • 165
  • 230
10
votes
3 answers

Clean solution to this ruby iterator trickiness?

k = [1,2,3,4,5] for n in k puts n if n == 2 k.delete(n) end end puts k.join(",") # Result: # 1 # 2 # 4 # 5 # [1,3,4,5] # Desired: # 1 # 2 # 3 # 4 # 5 # [1,3,4,5] This same effect happens with the other array iterator, k.each: k =…
Justin L.
  • 13,510
  • 5
  • 48
  • 83
10
votes
4 answers

ruby array (enumerable) method to select and reject into 2 arrays in 1 operation

# this code works list = (0..20).to_a # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] odd = list.select { |x| x.odd? } # => [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] list.reject! { |x| x.odd? } # => [0, 2, 4, 6, 8, 10,…
house9
  • 20,359
  • 8
  • 55
  • 61