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:
- Create cache of already calculated items
- If certain item is found i cache - that's great, we have a match.
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 usingforeach
).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
Ruby: Combine Date and Time Objects into a Datetime
Generating an Instagram- or Youtube-Like Unguessable String Id in Ruby/Activerecord
Why Do I Get an Error Installing the JSON Gem in Ubuntu
Test Output to Command Line with Rspec
Replace "&" to "\&" in Ruby Seems Impossible
How Does This Ruby Injection Magic Work
How to Configure Ruby to Enter the Debugger on Ctrl-C (Sigint)
Do I Have to Manually Uninstall All Dependent Gems
Linkedin API for Company Directory
How to Traverse Symlinked Directories in Ruby with a "**" Glob
How to Have Rspec Test for My Default Scope
Detect If Application Was Started as Http Server or Not (Rake Task, Rconsole etc)
Parse Email Addresses for "From" and "To" Fields in Ruby
Rails Active Admin CSS Conflicting with Twitter Bootstrap CSS