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
7
votes
2 answers

How to create a TypeScript @enumerable(false) decorator for a property

I want to create a decorator in TypeScript in order to be able to make a class property not enumerable. I found an example of @enumerable here: https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators but that only seems to…
mvermand
  • 5,829
  • 7
  • 48
  • 74
7
votes
2 answers

Performance of Enumerable.Range vs for loop

I wondered what the performance overhead is of using Enumerable.Range was against using a foreach loop. For example: var stringArray = Enumerable.Range(0, 4).Select(i => string.Empty).ToArray(); VS. var stringArray = new string[4]; for (int i = 0;…
jolySoft
  • 2,948
  • 2
  • 30
  • 34
7
votes
4 answers

Enumerate existing text in Vim (make numbered list out of existing text)

I have a source document with the following text Here is a bunch of text ... Collect underpants ??? Profit! ... More text I would like to visually select the middle three lines and insert numbers in front of them: Here is a bunch of text ... 1.…
user1717828
  • 7,122
  • 8
  • 34
  • 59
7
votes
2 answers

Sort an enumerable in descending order

What's the best way to sort an Enumerable in descending order? I've been doing @array.sort.reverse or @array.sort_by{|song| song.title }.reverse I suppose I could do something like @array.sort{|a, b| b.title <=> a.title}, but I find this hard to…
Tom Lehman
  • 85,973
  • 71
  • 200
  • 272
7
votes
3 answers

How to get Enum object by value in C#?

I recently encountered a case when I needed to get an Enum object by value (to be saved via EF CodeFirst), and here is my Enum: public enum ShipmentStatus { New = 0, Shipped = 1, Canceled = 2 } So I needed to get ShipmentStatus.Shipped object…
mikhail-t
  • 4,103
  • 7
  • 36
  • 56
7
votes
3 answers

refer an enumerator value with specified index

Assume I have an enumerable object enum and now I want to get the third item. I know one of a general approach is convert into an array and then access with index like: enum.to_a[2] But this way will create a temporary array and it might be…
shouya
  • 2,863
  • 1
  • 24
  • 45
6
votes
1 answer

Returning the differences between two enumerables

I'm trying to determine the differences between two collections. private ObservableCollection _objectList = null; private ObservableCollection _cachedObjectList = null; SomeObject implements IEquatable I'm using…
Michael G
  • 6,695
  • 2
  • 41
  • 59
6
votes
1 answer

Why does repeated Enumerable to Observable conversion block

This is a rather educational, out of curiosity question. Consider the following snippet: var enumerable = Enumerable.Range(0, 5); var observable = enumerable.ToObservable(); var enu =…
Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
6
votes
3 answers

Can a Ruby method accept either a block OR an argument?

I'm doing the lessons on The Odin Project and now I have to write myself a new #count method (with another name) that behaves like the normal one from the Enumerable module. The documentation on count says the following…
Nikolay D
  • 329
  • 3
  • 11
6
votes
1 answer

Why would #each_with_object and #inject switch the order of block parameters?

#each_with_object and #inject can both be used to build a hash. For example: matrix = [['foo', 'bar'], ['cat', 'dog']] some_hash = matrix.inject({}) do |memo, arr| memo[arr[0]] = arr memo # no implicit conversion of String into Integer…
mbigras
  • 7,664
  • 11
  • 50
  • 111
6
votes
5 answers

Ruby Enumerable: first truthy value of a block

In ruby we can do something like: stuff_in_trash.detect(&:eatable?) => :pack_of_peanuts stuff_in_trash.detect(&:drinkable?) => nil But what if we are interested in the value of the block the first time it is truthy, rather than the first item for…
sunless
  • 597
  • 5
  • 19
6
votes
1 answer

C# infinite iteration

Is there anything similar in C# to Java's Stream.iterate? The closest thing I could find was Enumerable.Range but it is much different. The reason I'm asking is that I've been just watching some presentation about good programming principles and…
SOReader
  • 5,697
  • 5
  • 31
  • 53
6
votes
1 answer

Does `map` use `each` or not?

In Ruby, the Enumerable module mixes into collection classes and relies on the class serving up an each method which yields each item in the collection. Okay, so if I want to use Enumerable in my own class, I'll just implement each [1]: class…
Erik Trautman
  • 5,883
  • 2
  • 27
  • 35
6
votes
2 answers

Elixir: how to make struct enumerable

I have a struct: my_struct = %MyStruct{a: 1, b: 2} how do I make it enumerable, so I can use the Enum methods on it?
tldr
  • 11,924
  • 15
  • 75
  • 120
6
votes
3 answers

How does to_enum(:method) receive its block here?

This code, from an example I found, counts the number of elements in the array which are equal to their index. But how ? [4, 1, 2, 0].to_enum(:count).each_with_index{|elem, index| elem == index} I could not have done it only with chaining, and the…
Dean Radcliffe
  • 2,194
  • 22
  • 29