Why Is Enumerable#Each_With_Object Deprecated

Why is Enumerable#each_with_object deprecated?

Well, that seems a bit weird.

Even Agile Rails writes somewhere:

"The Ruby 1.9 each_with_object method was found to be so handy that the Rails crew backported it to Ruby 1.8 for you".

This seems like an error in APIdock ? I don't see any reason why it would be.

Why aren't Enumerations Iterable?

Enumeration hasn't been modified to support Iterable because it's an interface not a concrete class (like Vector, which was modifed to support the Collections interface).

If Enumeration was changed to support Iterable it would break a bunch of people's code.

Accessing a object's non enumerable properties

You can write a function, let's call it getAllPropertyNames(), that iterates through the prototype chain of the object and accumulates the properties of each level:





function getAllPropertyNames (o) {

let propertyNames = []


for (let proto = o; proto !== null; proto = Object.getPrototypeOf(proto)) {

propertyNames = propertyNames.concat(Object.getOwnPropertyNames(proto))

}


return propertyNames

}


console.log(getAllPropertyNames({ mykey: 'value' }))

Scala example code and deprecation problems

I guess you used the Eclipse plugin for version 2.9/2.10 instead of the one for version 2.8, that's why it worked in your old Scala distribution, but not with Eclipse which ships with its own version.

The method in question was deprecated and removed subsequently.

Suggestion: Update to the latest version (2.9.1 currently).

Why is Java Vector (and Stack) class considered obsolete or deprecated?

Vector synchronizes on each individual operation. That's almost never what you want to do.

Generally you want to synchronize a whole sequence of operations. Synchronizing individual operations is both less safe (if you iterate over a Vector, for instance, you still need to take out a lock to avoid anyone else changing the collection at the same time, which would cause a ConcurrentModificationException in the iterating thread) but also slower (why take out a lock repeatedly when once will be enough)?

Of course, it also has the overhead of locking even when you don't need to.

Basically, it's a very flawed approach to synchronization in most situations. As Mr Brian Henk pointed out, you can decorate a collection using the calls such as Collections.synchronizedList - the fact that Vector combines both the "resized array" collection implementation with the "synchronize every operation" bit is another example of poor design; the decoration approach gives cleaner separation of concerns.

As for a Stack equivalent - I'd look at Deque/ArrayDeque to start with.

c++ mark enum value as deprecated?

You can use the [[deprecated]] attribute from C++14 on.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3760.html

So, you would write this:

enum MyEnum {
firstvalue = 0
secondvalue,
thirdvalue [[deprecated]],
fourthvalue
};

When you deprecate an enum value with value explicitly assigned, then it would look like this:

enum MyEnum {
firstvalue = 0
secondvalue,
thirdvalue [[deprecated]] = 2,
fourthvalue
};

lazily building up an enumerable with breaks in between

If your collections are really large and you really don't want to evaluate ToString for each item of data, you could use the following approach:

  1. Create cache of already calculated items
  2. If certain item is found i cache - that's great, we have a match.
  3. Otherwise - continue populating cache by iterating over data collection until we find a match. This can be efficiently be done with manually controlling **Enumerator** of data collection (instead of using foreach).

    IEnumerable<object> obs;
    IEnumerable<object> data;
    Dictionary<string, object> dataCache = new Dictionary<string, object>();

    var dataIterator = data.GetEnumerator();
    foreach (var ob in obs)
    {
    var obText = ob.ToString();
    object matchingDataItem = null;
    if (!dataCache.TryGetValue(obText, out matchingDataItem))
    {
    while (dataIterator.MoveNext())
    {
    var currentData = dataIterator.Current;
    var currentDataText = currentData.ToString();
    if (!dataCache.ContainsKey(currentDataText)) // Handle the case when data collection contains duplicates
    {
    dataCache.Add(currentDataText, currentData);
    if (currentDataText == obText)
    {
    matchingDataItem = currentData;
    break;
    }
    }
    }
    }
    if (matchingDataItem != null)
    {
    Console.WriteLine("Matching item found for " + obText);
    }
    else
    {
    throw new Exception("No matching data found.");
    }
    }

This way you can guarantee to iterate over data collection only to the point when all obs items are found and you won't evaluate ToString for each item more then once.

PS: I hope "ToString" is just for example and you have some complex calculations there, which is worth such complexities...



Related Topics



Leave a reply



Submit