Enumerating Object.keys() in node.js - strange issue - node.js

There is an issue with enumerating Object.keys() in node.js that I do not understand. With the following code:
Object.prototype.tuple = function() {
var names = Object.keys(this);
console.log("Dump of names:");
console.log(names);
console.log("FOR loop using indexes:");
for (var k = 0; k < names.length; k++)
{
console.log(names[k]);
}
console.log("FOR loop using enumeration:");
for (var z in names)
{
console.log(z);
}
return this;
};
var x = {a:0, b:0, c:0}.tuple();
I get the following results on the console:
Dump of names:
[ 'a', 'b', 'c' ]
FOR loop using indexes:
a
b
c
FOR loop using enumeration:
0
1
2
tuple
Could somebody explain where does an extra "tuple" come from in the second loop? While defined as function in Object.prototype, it is neither an own property of x object, nor included in names array.
I am using node.js version 0.8.20.

The first loop goes over the properties of x (Object.keys() returns only own properties), while the second one goes over the properties or the array names, including the ones up in the prototype chain.
Thanks to Jonathan Lonowski for clarifications.

I think what #Kuba mentioned above is not correct.
Object.keys, Object.getOwnPropertyNames and other similar method would behave different sightly. Their behaviors are related to a property named enumerable.
I am going to dinner with my friends so I can only give you a helpful link illustrating it. So sorry.
https://developer.mozilla.org/en-US/docs/Enumerability_and_ownership_of_properties

Related

Why can't I use removeAll on a list of objects?

I am trying to create an app that let's you type in what you want to eat and drink. It calculates all of that and then when you press the print button, I want it to count how often each item's in the list and give it back like this:
"9x Juice /n
5x Steaks /n
4x Salads"
The drinks and foods are objects in the new class Edibles:
class Edibles(val name: String, val price: Double):Serializable {
}
I track all of the objects in the MutableList order and can access the different members of the list and their attributes, but when I try to removeAll duplicates in my list, android studio complains and I don't know how to fix it.
My try to calculate how many members are in the list order:
var totalOrder = ""
for(i in order){
var number = order.count {it == order[0]}
totalOrder = totalOrder + "$number" + "x" + order[0].name + "\n"
order.removeAll(order[0])
}
The problem as far as I saw so far is, that Edibles doesn't have the interface Collection and when I try to implement that, it wants me to override a bunch of functions where I don't know what to do with it...
If anyone has an explanation or even a fix or an idea on how to do it differently, I would be very grateful
removeAll is meant to take a list or a predicate, not a single element. If you convert your element to a predicate checking for equality, it will remove all elements equal to that one.
order.removeAll { it == order[0] }
However, you'll also need to remember rule number one of iteration: Never delete while iterating. So what you really want to do is accumulate all of the "deletion" candidates into a list and then delete them after-the-fact.
In fact, what you're doing here can be done without mutating the list at all, using a built-in list combinator called groupBy.
var totalOrder = ""
for (entry in order.groupBy { it }) {
val item = entry.key
val count = entry.value.size
totalOrder += "${count}x${item.name}\n"
}
You're not allowed to mutate a collection while iterating it in a for loop anyway. One way to remove duplicates would be to create a temporary MutableSet and compare each item to it in a removeAll operation. removeAll takes a lambda predicate that is called on each item and the Boolean you return from the predicate. When you call add on a MutableSet, it returns a Boolean to tell you if the item already was in the set, so you can remove duplicates with the following.
Assuming you just want to compare names of items to determine if they are duplicates, you can create a MutableSet<String>.
with (mutableSetOf<String>()) {
order.removeAll { add(it.name) }
}

Change all list items during iteration

Problem
I need to change all list items during a loop. Is it possible?
Code
List<WebElement> elements = driver.findElements(By.xpath('//*[#id="id1"]//tr[td/a]'))
elements.eachWithIndex { element, index ->
...
if(...) {
...
i = index+1
elements = driver.findElements(By.xpath('//*[#id="id1"]//tr[td/a][position()>' + i + ']')) // new list content which must be use by loop
}
}
However, new list is not used by the loop.
Can you help and explain me why?
Thanks
Regards
EDIT 1
I need to retrieve element everytime.
List<WebElement> elements = driver.findElements(By.xpath('//*[#id="dzA26"]//tr[td/a]'))
for(int i = 1; i <= elements.size(); i++) {
WebElement element = driver.findElement(By.xpath('//*[#id="dzA26"]//tr[td/a][' + i + ']'))
...
}
So first of all while iterating over a List, changing or removing elements is not safe to do. It can be possible, but you should avoid it.
That's because you are trying to change the element it is currently iterating at. So the Iterator behind the '.each' closure gets confused and doesn't know where to go on after the current iteration.
If you have to change all elements with the same operation, you could use the List.collect() closure provided by groovy, which will return whatever you like into a new List.
e.g.:
List<WebElement> elements = elements.collect { element ->
return element.doSomething()
}
Edit 1
After your update there is a new Problem, because it seems like you always want to update all Elements int the List.
So why don't you create the List inside the Loop, fill it, and use it, then go to the next iteration.
e.g.:
for(int i = 0;i < threshold; i++) {
List<WebElement> elements = useMethodToRetrieveElementsFori(i);
elements.each {
// Do whatever has to be done with this element.
}
}
Or after looking at it a little longer, it seems obvious to use code reflection at that point. Because you want to dig deeper into the WebElements, you should call a method that calls itself if it needs to go one step further. With your idea, you'd be stuck in an endless loop.
Or we are missing the the whole point of the question.

In my node application, why the same thing gives the different outputs?

var b = "pp.specifications.full_specs.";
var c = arr[i];
here the value of arr[i] is Memory
var a = b+c;
console.log(a);
it prints pp.specifications.full_specs.Memory on console
but when I use
console.log(pp.specifications.full_specs.Memory);
then prints an json object as:
{ Series: 'Inspiron',
Model: 'A562103SIN9',
Utility: 'Everyday Use',
OS: 'Windows 10 Home (64-bit)',
Dimensions: '274.73 x 384.9 x 25.44 mm',
Weight: '2.62 Kg',
Warranty: '1 Year Onsite Warranty' }
whenever the value of a contains pp.specifications.full_specs.Memory;
So what is the reason for getting different outputs?
There's a elementary difference between
console.log(pp.specifications.full_specs.Memory);
and
console.log("pp.specifications.full_specs.Memory");
Note the quotes!
So the expression: console.log(pp.specifications.full_specs.Memory); can be read from left to right:
Print to the output value of pp.specifications.full_specs.Memory and this is a value of pp object after taking its property specifications and then its property full_specs etc.
And the "pp.specifications.full_specs.Memory" means only a piece of text.
What is happening in your code should now be clearer:
// B is piece of text
var b = "pp.specifications.full_specs.";
// C is some other value - let's say also textual one
var c = arr[i]
// So b+c will mean add text to some other text
// It's expected that the result of such operations is concatenated text
// So then console.log(b+c) will mean
// Print the concatenated text from b and c
// And it's just plain text
console.log(b+c);
//It's the same as:
console.log("pp.specifications.full_specs.Memory");
// And this one means print some specific object attributes
// which is different operation!
console.log(pp.specifications.full_specs.Memory);
If you want to access objects by text you can do the following:
var d = eval(b+c);
It's pretty dangerous and eval should be avoided but I just wanted to demonstrate the basic idea. Eval executes strings as if they were code.
So string "pp.specifications.full_specs.Memory" will be evaluates (yep!) as value of the actual object.
As The eval can execute anything it should be always avoided and moreover it's superslow!
Instead if you want to access some property of pp basic on some text input you can do:
pp['specifications']['full_specs']['Memory']
As the pp.x notation is equivalent to expression: pp['x']
I hope my answer will help you understand Javascript mechanisms better :)

Assign an array to a property in a Chapel Class

Here is a Python-like pattern I need to re-create in Chapel.
class Gambler {
var luckyNumbers: [1..0] int;
}
var nums = [13,17,23,71];
var KennyRogers = new Gambler();
KennyRogers.luckyNumbers = for n in nums do n;
writeln(KennyRogers);
Produces the run-time error
Kenny.chpl:8: error: zippered iterations have non-equal lengths
I don't know how many lucky numbers Kenny will have in advance and I can't instantiate Kenny at that time. That is, I have to assign them later. Also, I need to know when to hold them, know when to fold them.
This is a good application of the array.push_back method. To insert lucky numbers one at a time you can do:
for n in nums do
KennyRogers.luckyNumbers.push_back(n);
You can also insert the whole array in a single push_back operation:
KennyRogers.luckyNumbers.push_back(nums);
There are also push_front and insert methods in case you need to put elements at the front or at arbitrary positions in the array.
I don't think I can help on when to hold them or when to fold them.
A way to approach this that simply makes things the right size from the start and avoids resizing/rewriting the array is to establish luckyNumbers in the initializer for Gambler. In order to do this without resizing, you'll need to declare the array's domain and set it in the initializer as well:
class Gambler {
const D: domain(1); // a 1D domain field representing the array's size
var luckyNumbers: [D] int; // declare lucky numbers in terms of that domain
proc init(nums: [?numsD] int) {
D = numsD; // make D a copy of nums's domain; allocates luckyNumbers to the appropriate size
luckyNumbers = nums; // initialize luckyNumbers with nums
super.init(); // mark the initialization of fields as being done
}
}
var nums = [13,17,23,71];
var KennyRogers = new Gambler(nums);
writeln(KennyRogers);

Delay output to make it appear as typing? (Java)

I hope I can word this properly.
I'm wondering if it is possible to delay the output of letters in a string so it appears as if someone is typing?
I'm currently using JCreator and outputting to the console.
I've used thread.sleep, but this delays the output of the full string.
Any ideas? Let me know if I need to change up the wording.
You need to slice the string and push each character to an array. Then, use for loop to apply your function to each element in your array.
Here's a sample function I made using AS3:
function splitString(str:String):Array{
var arr = [];
for(var i=0;i<str.length;i++){
arr.push(str.substr(i,1));
}
return arr;
}
to apply:
for(var a=0;a<str_split.length;a++){
//trace(str_split[a]);
//apply here your function
}

Resources