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
19
votes
6 answers

Ruby array of hash. group_by and modify in one line

I have an array of hashes, something like [ {:type=>"Meat", :name=>"one"}, {:type=>"Meat", :name=>"two"}, {:type=>"Fruit", :name=>"four"} ] and I want to convert it to this { "Meat" => ["one", "two"], "Fruit" => ["Four"]} I tried group_by…
jtomasrl
  • 1,430
  • 3
  • 13
  • 22
17
votes
2 answers

Ruby : Choosing between each, map, inject, each_with_index and each_with_object

When I started writing Ruby many years ago, it took me a while to understand the difference between each and map. It only got worse when I discovered all the other Enumerable and Array methods. With the help of the official documentation and many…
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
17
votes
4 answers

Why does `Enumerable` have `first` but not `last`?

Enumerable has first: (3..5).to_enum.first # => 3 but it does not have last: (3..5).to_enum.last # => NoMethodError: undefined method `last' for # Why is that?
sawa
  • 165,429
  • 45
  • 277
  • 381
16
votes
3 answers

Why is Enumerable#each_with_object deprecated?

According to APIdock, the Ruby method Enumerable#each_with_object is deprecated. Unless it's mistaken (saying "deprecated on the latest stable version of Rails" makes me suspicious that maybe it's Rails' monkey-patching that's deprecated), why is…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
16
votes
3 answers

Is there anything like Enumerable.Range(x,y) in Java?

Is there something like C#/.NET's IEnumerable range = Enumerable.Range(0, 100); //.NET in Java?
Simon
  • 9,255
  • 4
  • 37
  • 54
15
votes
4 answers

When is the Enumerator::Yielder#yield method useful?

The question "Meaning of the word yield" mentions the Enumerator::Yielder#yield method. I haven't used it before, and wonder under what circumstances it would be useful. Is it mainly useful when you want to create an infinite list of items, such as…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
15
votes
5 answers

Python equivalent of C#'s .Select?

I've got an list of objects in Python, and they each have an id property. I want to get a list of those IDs. In C# I'd write myObjects.Select(obj => obj.id); How would I do this in Python?
mpen
  • 272,448
  • 266
  • 850
  • 1,236
15
votes
2 answers

Does Enumerable's group_by preserve the Enumerable's order?

Does Enumerable#group_by preserve the original order within each value? When I get this: [1, 2, 3, 4, 5].group_by{|i| i % 2} # => {1=>[1, 3, 5], 0=>[2, 4]} is it guaranteed that, for example, the array [1, 3, 5] contains the elements in this order…
sawa
  • 165,429
  • 45
  • 277
  • 381
15
votes
2 answers

Does Enumerable.Repeat() do a deep copy?

If I use the following: var myList = Enumerable.Repeat(myCustomObject, 2); Will the Second element in the list be a deep copy of the first one? Note: myCustomObject can be any Object Edit: Could you also please let me know the potential use of…
Mahesh Velaga
  • 21,633
  • 5
  • 37
  • 59
14
votes
1 answer

C# records constructor parameter default value empty IEnumerable

I am converting this class public class MyClass { public IEnumerable Strings { get; } public MyClass(IEnumerable? strings = null) { Strings = strings ?? new List(); } } To a record. Currently I have…
user2154768
  • 890
  • 3
  • 8
  • 16
14
votes
1 answer

Entity Framework: Precompiled Query for Enumerable.Contains

Entity Framework 5+ is supposed to precompile all queries. However, for queries such as List ids; var entities = context.MyEntities.Where(x => ids.Contains(x.Id)).ToArray(); Entity Framework cannot precompile the query, and depending on the…
Roland Buergi
  • 1,157
  • 9
  • 23
13
votes
1 answer

Do LINQ's Enumerable Methods Maintain Relative Order of Elements?

Say I have List foos where the current order of elements is important. If I then apply a LINQ Enumerable method such as GroupBy, Where or Select, can I rely on the resulting IEnumerable to iterate in the same relative order as the original…
verdesmarald
  • 11,646
  • 2
  • 44
  • 60
13
votes
7 answers

How to split an array by a condition on adjacent elements into a limited number of partitions

Let's say I have an array of numbers, e.g. ary = [1, 3, 6, 7, 10, 9, 11, 13, 7, 24] I would like to split the array between the first point where a smaller number follows a larger one. My output should be: [[1, 3, 6, 7, 10], [9, 11, 13, 7,…
Stefan
  • 109,145
  • 14
  • 143
  • 218
13
votes
5 answers

Select records which has no day-off throughout the week in List - C#

I have an Employee class which defined as this: Employee { public int Id { get; set; } public string Name { get; set; } public DateTime WorkDate { get; set; } public bool isOff { get; set; } } This is my class implementation and…
Shift 'n Tab
  • 8,808
  • 12
  • 73
  • 117
13
votes
1 answer

Iterating over non-enumerable properties

I have used Object.defineProperty and enumerable: false to define a few properties on a config object. There is however one place in my module where I would like to iterate over the non-enumerable properties as well as the enumerable ones. Is it…
wheresrhys
  • 22,558
  • 19
  • 94
  • 162
1 2
3
40 41