Thursday, 17 May 2018

methods - Is Java "pass-by-reference" or "pass-by-value"?

I always thought Java was pass-by-reference.



However, I've seen a couple of blog posts (for example, this blog) that claim that it isn't.



I don't think I understand the distinction they're making.



What is the explanation?

java - How do I generate all possible numbers from this regular expression?





I want to get a list of all possible values for a regular expression.



Input :



2W
9WW

7W0W3


where W can be any digit from 0 to 9. i.e. W = [0-9]



Output:



20,21,22,....29
900,901,...910,911,...999
70003,70013,70023,...71003,72003,...79093



What I did :



I'm using Java and decided to create an ArrayList of Integers.



I created a method ArrayList getNumbers(String regex).



ArrayList getNumbers(String regex){


ArrayList fullList = new ArrayList();

char[] cArray = regex.toCharArray(); //converted the string into a character array.

for(int i=1;i
if(cArray[i] == 'W') {

for(int j=0;j<10;j++) {
//I'm not sure what goes here

fullList.add(the number with 'w' at this index replaced by 'j');
}
}

}
return fullList;
}


Is there any better way or library functions available to generate all such numbers?




How can I achieve this?



Any help please.


Answer



This is not quite a regex-based problem, but from an algorithmic perspective you can do the followings:




  • Count the number of W's in your string.

  • Based on the number of W's, create the product of range(0,9), for example if you have 2 W you need to create the products of two [0...9] lists that will be something like 0,0-0,1-0,2-...-9,9.


  • Loop over the combinations and replace them with a simple string formatting. For instance when you are iterating over a triple combination suppose with 3 variables i,j,k you want want to replace them in a string like 7W0W3W, you can do "7%d0%dW%d"%(i,j,k).



And if you are looking for a general regex to wrap all the cases you can use a regex like (w) (w in a capture group) then you need to first access to position of the match groups and replace them with combination items (i,j,k,..).


python - Negative list index?





Possible Duplicate:
Explain slice notation







I'm trying to understand the following piece of code:



# node list
n = []
for i in xrange(1, numnodes + 1):
tmp = session.newobject();
n.append(tmp)
link(n[0], n[-1])



Specifically, I don't understand what the index -1 refers to. If the index 0 refers to the first element, then what does -1 refer to?


Answer



Negative numbers mean that you count from the right instead of the left. So, list[-1] refers to the last element, list[-2] is the second-last, and so on.


c++ 2d array access speed changes based on [a][b] order?











I have been tinkering around with a program that I'm using to simply sum the elements of a 2d array. A typo led to what seem to me at least, some very strange results.



When dealing with array, matrix[SIZE][SIZE]:



for(int row = 0; row < SIZE; ++row)
for(int col = 0; col < SIZE; ++col)
sum1 += matrix[row][col];



Runs very quickly, however is the above line sum1... is modified:



sum2 += matrix[col][row]


As I did once on accident without realizing it, I notice that my runtime increases SIGNIFICANTLY. Why is this?


Answer



This is due to caching behaviour of your program.




Arrays are just consecutive blocks of memory, so when you access [row][column] you are accessing the memory sequentially. This means the data page you are accessing is on the same page, so the access is much faster.



When you do [column][row], you aren't accessing that memory sequentially anymore, so you will end up with more cache misses, so your program runs much slower.


Wednesday, 16 May 2018

r - How to change point size in lattice?



How can I modify "point" sizes proportionally to a given variable?



For example with ggplot I can do it with



ggplot(mydata,aes(x=x, y=y))+geom_point(aes(size=mysize))+geom_line(aes(group=id, color=id))


where mysize is the size of every point I want.
but it's very slow when the dataset is big.



I've tried with lattice:



xyplot(y~x , type="b", col="black", col.line="blue", data=mydata, pch=16, lwd=1 )


But I can't find a working way to use a size= option with lattice.



I've tried the cex option but it produces strange results.



This is a simplified version of my data: (it's a data.table but you can use a data.frame if you want)



structure(list(id = c(3059L, 1161L, 3996L, 6330L, 6675L, 1511L, 
3678L, 294L, 596L, 2440L, 446L, 2635L, 6073L, 3709L, 744L, 4997L,
3495L, 6447L, 6693L, 1040L, 1031L, 690L, 352L, 6311L, 2599L,
6425L, 3758L, 690L, 6742L, 3025L, 6348L, 214L, 222L, 8192L, 615L,
2939L, 5351L, 255L, 1531L, 6426L, 1686L, 2677L, 1919L, 3665L,
6514L, 630L, 820L, 2138L, 6695L, 1323L, 6246L, 2102L, 2600L,
3663L, 3851L, 970L, 1124L, 4071L, 1806L, 4579L, 3395L, 4371L,
1466L, 201L, 2112L, 8653L, 4407L, 1959L, 6341L, 2214L, 6515L,
1390L, 6346L, 5373L, 662L, 2198L, 1971L, 6177L, 4652L, 4420L,
6527L, 2704L, 6366L, 1111L, 6156L, 151L, 734L, 4286L, 5085L,
2359L, 2818L, 339L, 8486L, 5303L, 5076L, 8490L, 1230L, 1884L,
5204L, 2880L, 8463L, 215L, 6778L, 6329L, 5797L, 584L, 4831L,
4806L, 2581L, 3972L, 2298L, 3136L, 335L, 5538L, 1528L, 518L,
3552L, 3874L, 1967L, 4333L, 3035L, 4112L, 215L, 1768L, 866L,
3545L, 8085L, 8622L, 2844L, 2663L, 1356L, 4902L, 880L, 8219L,
1486L, 3086L, 685L, 417L, 5966L, 221L, 8401L, 8378L, 1542L, 1815L,
515L, 6262L, 3522L, 2440L, 182L, 946L, 3924L, 6219L, 2282L, 4165L,
4969L, 5829L, 6707L, 4467L, 8645L, 8267L, 3960L, 1432L, 8203L,
1691L, 5515L, 2005L, 5578L, 6732L, 103L, 740L, 8239L, 2037L,
630L, 8286L, 4005L, 3874L, 1691L, 201L, 4191L, 1259L, 4476L,
5210L, 798L, 5829L, 6499L, 120L, 1725L, 2185L, 3795L, 5918L,
3474L, 936L, 4625L, 4449L, 5579L, 5906L, 2050L, 3376L, 6140L,
8021L, 5009L, 3545L, 346L, 8513L, 1529L, 4155L, 3029L, 5785L,
397L, 1448L, 1804L, 6209L, 3043L, 2448L, 8371L, 5729L, 1602L,
5600L, 1488L, 280L, 1986L, 2858L, 1742L, 1939L, 4869L, 751L,
2967L, 6350L, 612L, 3935L, 3972L, 8321L, 8225L, 8117L, 5181L,
2214L, 1461L, 8150L, 3218L, 3699L, 4906L, 4488L, 1317L, 866L,
6832L, 8394L, 527L, 1384L, 3356L, 3320L, 1951L, 2979L, 3884L,
1664L, 8446L, 1851L, 1105L, 816L, 6323L, 4490L, 1020L, 3888L,
1086L, 4804L, 1203L, 3464L, 836L, 1591L, 755L, 6332L, 221L, 3519L,
1565L, 1169L, 2841L, 3992L, 5296L, 6187L, 3001L, 3711L, 1394L,
3333L, 1728L, 4320L, 6670L, 4653L, 6409L, 2369L, 8428L, 3714L,
8383L, 2142L, 1803L, 589L, 1986L, 866L, 866L, 4558L, 8139L, 3996L,
3667L, 2939L, 4626L, 3950L, 4371L, 6036L, 3212L, 8534L, 1599L,
1801L, 5015L, 121L, 8258L, 3811L, 1277L, 1161L, 4854L, 4581L,
8150L, 3223L, 2939L, 8548L, 1691L, 2969L, 1705L, 3477L, 5351L,
1120L, 3921L, 3915L, 2599L, 3457L, 630L, 2296L, 1569L, 6378L,
3447L, 6438L, 2697L, 2236L, 675L, 6147L, 4842L, 6205L, 4453L,
4478L, 2394L, 2115L, 5799L, 4683L, 1699L, 4757L, 8566L, 2125L,
3365L, 5194L, 1448L, 2043L, 2549L, 8647L, 1542L, 6179L, 1706L,
4099L, 5996L, 5289L, 6383L, 5102L, 488L, 889L, 3323L, 8461L,
1673L, 919L, 661L, 6505L, 1624L, 8261L, 6118L, 3935L, 2288L,
1230L, 1221L, 8359L, 8611L, 5710L, 6064L, 5238L, 2251L, 72L,
678L, 1649L, 8304L, 6056L, 8294L, 1967L, 5914L, 536L, 6330L,
8548L, 1111L, 3773L, 1381L, 3522L, 8461L, 1163L, 3111L, 3035L,
8101L, 1705L, 1690L, 2988L, 2243L, 3921L, 6348L, 4683L, 8424L,
3117L, 2015L, 559L, 8596L, 4235L, 280L, 8197L, 6096L, 3130L,
2880L, 1224L, 3636L, 8291L, 108L, 3213L, 2386L, 3350L, 3526L,
8116L, 2213L, 3519L, 1387L, 3672L, 4096L, 4288L, 905L, 3348L,
380L, 876L, 1685L, 8636L, 5559L, 1630L, 4696L, 5068L, 2351L,
757L, 3842L, 3517L, 8270L, 2880L, 8572L, 3384L, 6056L, 3884L,
1259L, 1053L, 6262L, 3965L, 4401L, 5295L, 4467L, 8216L, 8197L,
3500L, 5475L, 6662L, 2117L, 1092L, 5500L, 1447L, 6283L, 5930L,
2486L, 3258L, 4272L, 1616L, 3024L, 4104L, 3760L, 4431L, 5830L,
6366L, 5202L, 2868L, 5097L, 8647L, 6701L, 1167L, 5521L, 1209L,
5979L, 6178L), cars = structure(c(11L, 12L, 12L, 11L, 12L, 11L,
12L, 9L, 1L, 9L, 8L, 13L, 12L, 12L, 12L, 9L, 12L, 10L, 8L, 10L,
9L, 12L, 13L, 8L, 10L, 9L, 7L, 9L, 11L, 13L, 9L, 3L, 12L, 13L,
13L, 12L, 11L, 13L, 13L, 8L, 7L, 13L, 13L, 13L, 13L, 12L, 7L,
9L, 11L, 10L, 11L, 9L, 12L, 12L, 12L, 13L, 13L, 11L, 6L, 12L,
13L, 9L, 9L, 2L, 11L, 11L, 13L, 12L, 13L, 8L, 11L, 9L, 11L, 11L,
7L, 12L, 10L, 7L, 13L, 5L, 12L, 10L, 7L, 13L, 13L, 12L, 7L, 9L,
10L, 12L, 13L, 13L, 7L, 13L, 12L, 13L, 13L, 9L, 13L, 9L, 10L,
10L, 13L, 11L, 13L, 13L, 13L, 13L, 13L, 11L, 11L, 7L, 6L, 13L,
11L, 7L, 12L, 10L, 4L, 11L, 13L, 12L, 6L, 8L, 12L, 7L, 12L, 12L,
12L, 10L, 8L, 9L, 11L, 13L, 12L, 9L, 13L, 10L, 12L, 8L, 7L, 12L,
13L, 13L, 13L, 13L, 13L, 10L, 12L, 3L, 13L, 13L, 12L, 13L, 13L,
9L, 6L, 11L, 11L, 12L, 13L, 12L, 13L, 6L, 8L, 13L, 10L, 13L,
11L, 13L, 11L, 7L, 12L, 13L, 13L, 11L, 4L, 11L, 11L, 5L, 11L,
12L, 13L, 11L, 8L, 9L, 10L, 11L, 12L, 3L, 10L, 8L, 10L, 13L,
8L, 13L, 6L, 12L, 13L, 12L, 13L, 6L, 12L, 12L, 11L, 13L, 10L,
12L, 10L, 12L, 8L, 10L, 10L, 13L, 13L, 13L, 8L, 13L, 8L, 12L,
7L, 8L, 11L, 1L, 11L, 12L, 7L, 12L, 13L, 11L, 7L, 13L, 12L, 9L,
13L, 13L, 2L, 4L, 13L, 12L, 9L, 10L, 11L, 12L, 9L, 13L, 10L,
12L, 9L, 7L, 12L, 13L, 11L, 13L, 7L, 4L, 7L, 13L, 13L, 13L, 13L,
12L, 8L, 5L, 8L, 13L, 6L, 13L, 12L, 8L, 12L, 6L, 8L, 6L, 13L,
12L, 8L, 6L, 11L, 8L, 4L, 12L, 10L, 12L, 12L, 11L, 8L, 12L, 10L,
9L, 13L, 12L, 10L, 11L, 13L, 12L, 12L, 12L, 11L, 7L, 13L, 12L,
11L, 7L, 8L, 13L, 12L, 13L, 12L, 13L, 13L, 13L, 13L, 11L, 13L,
13L, 6L, 5L, 9L, 13L, 10L, 8L, 13L, 8L, 10L, 12L, 10L, 11L, 11L,
13L, 13L, 8L, 13L, 13L, 12L, 2L, 12L, 1L, 5L, 13L, 4L, 12L, 3L,
3L, 13L, 8L, 13L, 13L, 10L, 10L, 5L, 11L, 11L, 12L, 10L, 8L,
13L, 5L, 13L, 11L, 7L, 12L, 9L, 13L, 12L, 5L, 12L, 12L, 7L, 10L,
3L, 12L, 11L, 6L, 11L, 12L, 12L, 12L, 11L, 7L, 8L, 11L, 10L,
11L, 13L, 13L, 13L, 12L, 6L, 12L, 11L, 13L, 9L, 8L, 13L, 7L,
4L, 13L, 9L, 9L, 10L, 12L, 1L, 12L, 12L, 13L, 9L, 12L, 7L, 6L,
13L, 9L, 13L, 10L, 12L, 12L, 13L, 13L, 12L, 13L, 12L, 7L, 13L,
3L, 12L, 12L, 13L, 2L, 5L, 9L, 12L, 10L, 7L, 12L, 12L, 11L, 10L,
13L, 13L, 13L, 11L, 13L, 13L, 12L, 10L, 7L, 13L, 12L, 6L, 13L,
8L, 11L, 8L, 13L, 10L, 13L, 13L, 11L, 8L, 11L, 8L, 7L, 11L, 5L,
11L, 13L, 11L, 13L, 13L, 12L, 6L, 13L, 9L, 13L, 8L, 6L, 8L, 6L,
13L, 9L, 13L, 12L, 6L, 8L, 12L, 10L, 13L, 12L, 12L, 11L, 8L,
11L, 11L, 2L, 11L, 12L, 11L, 11L, 6L, 12L), .Label = c("FORD",
"VW", "PEUGEOT", "RENAULT", "TOYOTA", "BMW", "NISSAN", "MB",
"AUDI", "HONDA", "FIAT", "LR", "SKODA", "MAZDA", "MINI", "KIA",
"VOLVO", "SEAT", "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, 2L, 2L, 1L, 1L, 1L, 1L), mysize = c(0.196195449459157, 0.259604625139873,
0.259604625139873, 0.196195449459157, 0.259604625139873, 0.0982578397212544,
0.259604625139873, 0.0761772853185596, 0.00627177700348432, 0.0759581881533101,
0.0655052264808362, 0.198433420365535, 0.259604625139873, 0.259604625139873,
0.167701863354037, 0.0759581881533101, 0.212543554006969, 0.120477433793361,
0.0655052264808362, 0.0664819944598338, 0.061917195076464, 0.183673469387755,
0.307479224376731, 0.0512465373961219, 0.120477433793361, 0.061917195076464,
0.0484893696381947, 0.0759581881533101, 0.0982578397212544, 0.198433420365535,
0.142857142857143, 0.0115628496829541, 0.259604625139873, 0.34402332361516,
0.375, 0.259604625139873, 0.0982578397212544, 0.198433420365535,
0.198433420365535, 0.0481163744871317, 0.0522648083623693, 0.282229965156794,
0.282229965156794, 0.198433420365535, 0.34402332361516, 0.5,
0.1, 0.061917195076464, 0.196195449459157, 0.120477433793361,
0.196195449459157, 0.0759581881533101, 0.212543554006969, 0.259604625139873,
0.212543554006969, 0.198433420365535, 0.198433420365535, 0.196195449459157,
0.0662020905923345, 0.259604625139873, 0.307479224376731, 0.0759581881533101,
0.061917195076464, 0.0625, 0.196195449459157, 0.0982578397212544,
0.282229965156794, 0.259604625139873, 0.282229965156794, 0.0481163744871317,
0.0982578397212544, 0.061917195076464, 0.0982578397212544, 0.196195449459157,
0.0699708454810496, 0.212543554006969, 0.120477433793361, 0.0484893696381947,
0.198433420365535, 0.00857888847444983, 0.212543554006969, 0.0682926829268293,
0.0522648083623693, 0.444444444444444, 0.198433420365535, 0.212543554006969,
0.0875, 0.061917195076464, 0.120477433793361, 0.259604625139873,
0.307479224376731, 0.282229965156794, 0.0522648083623693, 0.375,
0.259604625139873, 0.198433420365535, 0.282229965156794, 0.0759581881533101,
0.307479224376731, 0.0759581881533101, 0.0682926829268293, 0.120477433793361,
0.282229965156794, 0.0982578397212544, 0.307479224376731, 0.307479224376731,
0.34402332361516, 0.198433420365535, 0.282229965156794, 0.0982578397212544,
0.196195449459157, 0.0496894409937888, 0.054016620498615, 0.198433420365535,
0.196195449459157, 0.0484893696381947, 0.259604625139873, 0.0682926829268293,
0.0229965156794425, 0.196195449459157, 0.307479224376731, 0.259604625139873,
0.0662020905923345, 0.0481163744871317, 0.4, 0.0699708454810496,
0.259604625139873, 0.212543554006969, 0.21606648199446, 0.120477433793361,
0.0655052264808362, 0.0761772853185596, 0.196195449459157, 0.198433420365535,
0.259604625139873, 0.0759581881533101, 0.5, 0.120477433793361,
0.259604625139873, 0.0728862973760933, 0.0522648083623693, 0.259604625139873,
0.322981366459627, 0.198433420365535, 0.282229965156794, 0.282229965156794,
0.282229965156794, 0.120477433793361, 0.259604625139873, 0.0115628496829541,
0.198433420365535, 0.307479224376731, 0.21606648199446, 0.307479224376731,
0.198433420365535, 0.0759581881533101, 0.0354345393509884, 0.0775623268698061,
0.196195449459157, 0.259604625139873, 0.282229965156794, 0.259604625139873,
0.282229965156794, 0.054016620498615, 0.0728862973760933, 0.282229965156794,
0.120477433793361, 0.282229965156794, 0.196195449459157, 0.307479224376731,
0.196195449459157, 0.0522648083623693, 0.1, 0.198433420365535,
0.198433420365535, 0.0775623268698061, 0.0125, 0.032069970845481,
0.196195449459157, 0.0229965156794425, 0.196195449459157, 0.259604625139873,
0.307479224376731, 0.196195449459157, 0.0655052264808362, 0.061917195076464,
0.120477433793361, 0.196195449459157, 0.259604625139873, 0.0204081632653061,
0.120477433793361, 0.0481163744871317, 0.0682926829268293, 0.282229965156794,
0.0655052264808362, 0.198433420365535, 0.054016620498615, 0.259604625139873,
0.282229965156794, 0.212543554006969, 0.198433420365535, 0.0683229813664596,
0.259604625139873, 0.259604625139873, 0.196195449459157, 0.282229965156794,
0.0375, 0.212543554006969, 0.120477433793361, 0.183673469387755,
0.0481163744871317, 0.0664819944598338, 0.0682926829268293, 0.198433420365535,
0.307479224376731, 0.282229965156794, 0.0481163744871317, 0.307479224376731,
0.0655052264808362, 0.212543554006969, 0.0522648083623693, 0.0655052264808362,
0.196195449459157, 0.0138504155124654, 0.196195449459157, 0.259604625139873,
0.0522648083623693, 0.212543554006969, 0.198433420365535, 0.0982578397212544,
0.0484893696381947, 0.307479224376731, 0.259604625139873, 0.0759581881533101,
0.282229965156794, 0.307479224376731, 0.0152354570637119, 0.0193905817174515,
0.198433420365535, 0.183673469387755, 0.061917195076464, 0.0682926829268293,
0.0982578397212544, 0.2, 0.0759581881533101, 0.198433420365535,
0.120477433793361, 0.21606648199446, 0.061917195076464, 0.0522648083623693,
0.259604625139873, 0.198433420365535, 0.196195449459157, 0.198433420365535,
0.0484893696381947, 0.0193905817174515, 0.0875, 0.307479224376731,
0.282229965156794, 0.5, 0.307479224376731, 0.212543554006969,
0.0481163744871317, 0.0318559556786704, 0.0481163744871317, 0.198433420365535,
0.0354345393509884, 0.282229965156794, 0.259604625139873, 0.0655052264808362,
0.212543554006969, 0.0354345393509884, 0.0728862973760933, 0.0662020905923345,
0.282229965156794, 0.21606648199446, 0.0512465373961219, 0.0662020905923345,
0.196195449459157, 0.0655052264808362, 0.0229965156794425, 0.259604625139873,
0.0664819944598338, 0.259604625139873, 0.259604625139873, 0.196195449459157,
0.0512465373961219, 0.212543554006969, 0.0664819944598338, 0.061917195076464,
0.282229965156794, 0.259604625139873, 0.120477433793361, 0.196195449459157,
0.307479224376731, 0.259604625139873, 0.183673469387755, 0.259604625139873,
0.0982578397212544, 0.0522648083623693, 0.282229965156794, 0.212543554006969,
0.196195449459157, 0.0522648083623693, 0.0512465373961219, 0.34402332361516,
0.212543554006969, 0.198433420365535, 0.259604625139873, 0.198433420365535,
0.282229965156794, 0.198433420365535, 0.34402332361516, 0.196195449459157,
0.307479224376731, 0.282229965156794, 0.0354345393509884, 0.0291545189504373,
0.142857142857143, 0.322981366459627, 0.0664819944598338, 0.0655052264808362,
0.5, 0.0481163744871317, 0.0664819944598338, 0.183673469387755,
0.120477433793361, 0.0982578397212544, 0.196195449459157, 0.282229965156794,
0.282229965156794, 0.0728862973760933, 0.5, 0.198433420365535,
0.183673469387755, 0.0132404181184669, 0.259604625139873, 0.00484893696381947,
0.00857888847444983, 0.34402332361516, 0.00484893696381947, 0.259604625139873,
0.0115628496829541, 0.0132404181184669, 0.198433420365535, 0.0655052264808362,
0.307479224376731, 0.198433420365535, 0.120477433793361, 0.031055900621118,
0.0248447204968944, 0.196195449459157, 0.0982578397212544, 0.259604625139873,
0.120477433793361, 0.0481163744871317, 0.375, 0.0318559556786704,
0.307479224376731, 0.196195449459157, 0.0699708454810496, 0.259604625139873,
0.0761772853185596, 0.282229965156794, 0.259604625139873, 0.0229965156794425,
0.259604625139873, 0.259604625139873, 0.0484893696381947, 0.120477433793361,
0.0115628496829541, 0.259604625139873, 0.196195449459157, 0.0354345393509884,
0.196195449459157, 0.212543554006969, 0.259604625139873, 0.259604625139873,
0.196195449459157, 0.0484893696381947, 0.0481163744871317, 0.196195449459157,
0.120477433793361, 0.196195449459157, 0.322981366459627, 0.282229965156794,
0.198433420365535, 0.21606648199446, 0.0662020905923345, 0.212543554006969,
0.0775623268698061, 0.282229965156794, 0.0787172011661808, 0.0728862973760933,
0.282229965156794, 0.0484893696381947, 0.0229965156794425, 0.282229965156794,
0.0759581881533101, 0.061917195076464, 0.0682926829268293, 0.259604625139873,
0.0222222222222222, 0.259604625139873, 0.212543554006969, 0.282229965156794,
0.061917195076464, 0.212543554006969, 0.0484893696381947, 0.0662020905923345,
0.198433420365535, 0.061917195076464, 0.34402332361516, 0.0682926829268293,
1, 0.183673469387755, 0.282229965156794, 0.375, 0.259604625139873,
0.282229965156794, 0.21606648199446, 0.0699708454810496, 0.307479224376731,
0.0124223602484472, 0.259604625139873, 0.259604625139873, 0.375,
0.0152354570637119, 0.0248447204968944, 0.0761772853185596, 0.259604625139873,
0.120477433793361, 0.0595567867036011, 0.259604625139873, 0.259604625139873,
0.196195449459157, 0.120477433793361, 0.282229965156794, 0.198433420365535,
0.198433420365535, 0.0982578397212544, 0.307479224376731, 0.307479224376731,
0.259604625139873, 0.120477433793361, 0.0522648083623693, 0.198433420365535,
0.259604625139873, 0.0662020905923345, 0.307479224376731, 0.0655052264808362,
0.196195449459157, 0.0481163744871317, 0.282229965156794, 0.120477433793361,
0.282229965156794, 0.282229965156794, 0.196195449459157, 0.0481163744871317,
0.0982578397212544, 0.0481163744871317, 0.0522648083623693, 0.196195449459157,
0.05, 0.196195449459157, 0.198433420365535, 0.0982578397212544,
0.198433420365535, 0.322981366459627, 0.259604625139873, 0.05,
0.34402332361516, 0.061917195076464, 0.198433420365535, 0.0655052264808362,
0.054016620498615, 0.0481163744871317, 0.0662020905923345, 0.198433420365535,
0.061917195076464, 0.282229965156794, 0.259604625139873, 0.05,
0.0481163744871317, 0.21606648199446, 0.043731778425656, 0.198433420365535,
0.212543554006969, 0.21606648199446, 0.196195449459157, 0.062111801242236,
0.0982578397212544, 0.196195449459157, 0.0132404181184669, 0.0982578397212544,
0.212543554006969, 0.196195449459157, 0.196195449459157, 0.0354345393509884,
0.259604625139873)), .Names = c("id", "cars", "numb", "mysize"
), class = c("data.table", "data.frame"), row.names = c(NA, -500L
), .internal.selfref = )


and this is the resulting plot with ggplot



ggplot(mydata,aes(x=numb, y=cars))+geom_point(aes(size=mysize))+geom_line(aes(group=id, color="blue"),  show.legend = FALSE)+theme_bw()


enter image description here



and the plot with Lattice



xyplot(cars~numb , type="b", col="black", col.line="blue",  data=mydata, pch=16, cex= mydata$mysize*3,  lwd=1 , groups= mydata$id)


enter image description here



As you can see the circles aren't equivalent.
I don't know which one is wrong.



PD2:
I've summarized the data keeping only unique pairs.



poi <- unique(mydata, by=c("cars","numb"))



structure(list(id = c(3059L, 1161L, 1511L, 294L, 596L, 2440L, 
446L, 2635L, 744L, 3495L, 6447L, 1040L, 1031L, 690L, 352L, 6311L,
3758L, 6348L, 214L, 8192L, 615L, 6426L, 1686L, 2677L, 630L, 820L,
1806L, 201L, 662L, 4420L, 2704L, 1111L, 734L, 3136L, 335L, 1967L,
866L, 2844L, 685L, 221L, 1542L, 6707L, 4467L, 630L, 1691L, 201L,
1259L, 5918L, 3545L, 3029L, 1939L, 1461L, 8150L, 866L, 4804L,
4581L, 630L, 6378L, 6438L, 675L, 6205L, 4683L, 1699L, 8304L,
1381L, 6348L, 8197L, 2386L, 1053L, 8197L, 4104L, 5202L), cars = structure(c(11L,
12L, 11L, 9L, 1L, 9L, 8L, 13L, 12L, 12L, 10L, 10L, 9L, 12L, 13L,
8L, 7L, 9L, 3L, 13L, 13L, 8L, 7L, 13L, 12L, 7L, 6L, 2L, 7L, 5L,
10L, 13L, 7L, 7L, 6L, 4L, 12L, 12L, 13L, 8L, 13L, 6L, 11L, 12L,
4L, 11L, 5L, 3L, 6L, 10L, 1L, 2L, 4L, 12L, 5L, 5L, 13L, 2L, 1L,
4L, 3L, 10L, 5L, 9L, 1L, 12L, 3L, 7L, 5L, 6L, 10L, 8L), .Label = c("FORD",
"VW", "PEUGEOT", "RENAULT", "TOYOTA", "BMW", "NISSAN", "MB",
"AUDI", "HONDA", "FIAT", "LR", "SKODA", "MAZDA", "MINI", "KIA",
"VOLVO", "SEAT", "SUZUKI", "MITSU", "JAGUAR", "ROVER", "SAAB",
"LEXUS", "CHEVRO", "MG", "PORSCHE"), class = "factor"), numb = c(1L,
1L, 2L, 3L, 2L, 2L, 2L, 1L, 5L, 2L, 1L, 3L, 1L, 4L, 3L, 3L, 1L,
5L, 1L, 4L, 6L, 1L, 2L, 2L, 12L, 9L, 2L, 6L, 4L, 1L, 2L, 7L,
6L, 5L, 3L, 2L, 10L, 3L, 8L, 4L, 5L, 1L, 3L, 8L, 6L, 4L, 2L,
4L, 5L, 6L, 3L, 3L, 3L, 7L, 3L, 4L, 13L, 2L, 1L, 1L, 2L, 5L,
5L, 4L, 7L, 16L, 5L, 3L, 6L, 6L, 4L, 5L), mysize = c(0.196195449459157,
0.259604625139873, 0.0982578397212544, 0.0761772853185596, 0.00627177700348432,
0.0759581881533101, 0.0655052264808362, 0.198433420365535, 0.167701863354037,
0.212543554006969, 0.120477433793361, 0.0664819944598338, 0.061917195076464,
0.183673469387755, 0.307479224376731, 0.0512465373961219, 0.0484893696381947,
0.142857142857143, 0.0115628496829541, 0.34402332361516, 0.375,
0.0481163744871317, 0.0522648083623693, 0.282229965156794, 0.5,
0.1, 0.0662020905923345, 0.0625, 0.0699708454810496, 0.00857888847444983,
0.0682926829268293, 0.444444444444444, 0.0875, 0.0496894409937888,
0.054016620498615, 0.0229965156794425, 0.4, 0.21606648199446,
0.5, 0.0728862973760933, 0.322981366459627, 0.0354345393509884,
0.0775623268698061, 0.1, 0.0125, 0.032069970845481, 0.0229965156794425,
0.0204081632653061, 0.0683229813664596, 0.0375, 0.0138504155124654,
0.0152354570637119, 0.0193905817174515, 0.2, 0.0318559556786704,
0.0291545189504373, 0.5, 0.0132404181184669, 0.00484893696381947,
0.00484893696381947, 0.0132404181184669, 0.031055900621118, 0.0248447204968944,
0.0787172011661808, 0.0222222222222222, 1, 0.0124223602484472,
0.0595567867036011, 0.05, 0.05, 0.043731778425656, 0.062111801242236
)), class = c("data.table", "data.frame"), row.names = c(NA,
-72L), .Names = c("id", "cars", "numb", "mysize"))


Lattice doesn't produce the same result with this uniquefied dataset but at least is near what we want.



p1 <- xyplot(cars~numb , type="p", col="black",  data=poi, pch=16, cex= poi$mysize*3)
p2 <- xyplot(cars ~ numb , type = "l", col.line = "blue", data = mydata, lwd = 1 , groups = mydata$id)
p1+as.layer(p2)


enter image description here


Answer



You can use the cex parameter to set the size of the points in lattice.



The code may look like this, with some invented data:



library(lattice)

## some data invented on the spot
mydata <- data.frame(x = 1:5,
y = 6:10,
mysize = 1:5,
id = c(1,1,1,2,2))

xyplot(y ~ x , type = c("b"), col = c("black"), col.line = c("blue"),
data = mydata, pch = 21, cex = mydata$mysize, lwd = 1 )


This yields the following plot:



enter image description here



if you also want to use the grouping (as in your ggplot example), add a groups parameter:



xyplot(y~x , type=c("b"), col=c("black"), col.line=c("blue"), 
data=mydata, pch=21, cex= mydata$mysize, lwd=1 , groups= mydata$id)


Please let me know whether this is what you want.



UPDATE



We can see that one dot is the result of overplotting of several data points: e.g. the car "LR" with numb "1" occurs 61 times.



library(dplyr) ; nrow(mydata %>% filter(cars=="LR" & numb<2))
# 61


Let us remove these LR--1 combinations (after saving on for later) and make sure there is just one of them present. Store in mydata2



OneRow <- head(mydata %>% filter(cars=="LR" & numb<2), 1)
mydata2 <- mydata %>% filter( !(cars=="LR" & numb<2))
mydata2 <- rbind(mydata2, OneRow)


Now plot with ggplot



ggplot(mydata2, aes(x = numb, y = cars)) + 
geom_point(aes(size = mysize)) +
geom_line(aes(group=id, color="blue"), show.legend = FALSE) +
theme_bw()


enter image description here



and with lattice xyplot()



xyplot(cars ~ numb , type = "b", col = "black", col.line = "blue",  
data = mydata2, pch = 16,
cex = mydata$mysize*3, lwd = 1 , groups = mydata$id)


enter image description here



Comparing the two lattice plots makes it clear that, the multiplicity of the LR--1 combination plays a role in the size of the dot. If we want to - **and we need to know whether we want this ** - get the same result as with ggplot we need to have unique rows.


javascript - Promises - How to make asynchronous code execute synchronous without async / await?



  var p1 = new Promise(function(resolve, reject) {  
setTimeout(() => resolve("first"), 5000);

});
var p2 = new Promise(function(resolve, reject) {
setTimeout(() => resolve("second"), 2000);
});
var p3 = new Promise(function(resolve, reject) {
setTimeout(() => resolve("third"), 1000);
});

console.log("last to print");


p1.then(()=>p2).then(()=>p3).then(()=> console.log("last to be printed"))


As I was reading about promises, I know that I can print promises synchronous (in this case print: first, second, third, last to print) when I use async /await. Now I have also been reading that the same thing can be achieved using .then chaining and async/await is nothing 'special'. When I try to chain my promises, however, nothing happens except for the console.log of "last to be printed". Any insight would be great! Thanks!!



Edit to question:



  var p1 = new Promise(function (resolve, reject) {
setTimeout(() => console.log("first"), 5000);
resolve("first resolved")

});
var p2 = new Promise(function (resolve, reject) {
setTimeout(() => console.log("second"), 2000);
resolve("second resolved")
});
var p3 = new Promise(function (resolve, reject) {
setTimeout(() => console.log("third"), 0);
resolve("third resolved")
});


console.log("starting");
p1.then((val) => {
console.log("(1)", val)
return p2
}).then((val) => {
console.log("(2)", val)
return p3
}).then((val) => {
console.log("(3)", val)
})



Loggs:



starting
(1) first resolved
(2) second resolved
(3) third resolved
third
second

first


1: if executor function passed to new Promise is executed immediately, before the new promise is returned, then why are here promises resolved ()synchronously) first and after the setTimeouts (asynchronously) gets executed?




  1. Return value vs. resolve promise:



    var sync = function () {
    return new Promise(function(resolve, reject){

    setTimeout(()=> {
    console.log("start")
    resolve("hello") //--works
    // return "hello" //--> doesnt do anything
    }, 3000);
    })
    }
    sync().then((val)=> console.log("val", val))



Answer




The executor function you pass to new Promise is executed immediately, before the new promise is returned. So when you do:



var p1 = new Promise(function(resolve, reject) {  
setTimeout(() => resolve("first"), 5000);
});


...by the time the promise is assigned to p1, the setTimeout has already been called and scheduled the callback for five seconds later. That callback happens whether you listen for the resolution of the promise or not, and it happens whether you listen for resolution via the await keyword or the then method.



So your code starts three setTimeouts immediately, and then starts waiting for the first promise's resolution, and only then waiting for the second promise's resolution (it'll already be resolved, so that's almost immediate), and then waiting for the third (same again).




To have your code execute those setTimeout calls only sequentially when the previous timeout has completed, you have to not create the new promise until the previous promise resolves (using shorter timeouts to avoid lots of waiting):





console.log("starting");
new Promise(function(resolve, reject) {
setTimeout(() => resolve("first"), 1000);
})
.then(result => {

console.log("(1) got " + result);
return new Promise(function(resolve, reject) {
setTimeout(() => resolve("second"), 500);
});
})
.then(result => {
console.log("(2) got " + result);
return new Promise(function(resolve, reject) {
setTimeout(() => resolve("third"), 100);
});

})
.then(result => {
console.log("(3) got " + result);
console.log("last to print");
});





Remember that a promise doesn't do anything, and doesn't change the nature of the code in the promise executor. All a promise does is provide a means of observing the result of something (with really handy combinable semantics).




Let's factor out the common parts of those three promises into a function:



function delay(ms, ...args) {
return new Promise(resolve => {
setTimeout(resolve, ms, ...args);
});
}



Then the code becomes a bit clearer:





function delay(ms, ...args) {
return new Promise(resolve => {
setTimeout(resolve, ms, ...args);
});
}


console.log("starting");
delay(1000, "first")
.then(result => {
console.log("(1) got " + result);
return delay(500, "second");
})
.then(result => {
console.log("(2) got " + result);
return delay(100, "third");
})

.then(result => {
console.log("(3) got " + result);
console.log("last to print");
});





Now, let's put that in an async function and use await:






function delay(ms, ...args) {
return new Promise(resolve => {
setTimeout(resolve, ms, ...args);
});
}

(async() => {
console.log("starting");

console.log("(1) got " + await delay(1000, "first"));
console.log("(2) got " + await delay(500, "second"));
console.log("(3) got " + await delay(100, "third"));
console.log("last to print");
})();





Promises make that syntax possible, by standardizing how we observe asynchronous processes.







Re your edit:




1: if executor function passed to new Promise is executed immediately, before the new promise is returned, then why are here promises resolved ()synchronously) first and after the setTimeouts (asynchronously) gets executed?




There are two parts to that question:




A) "...why are here promises resolved ()synchronously) first..."



B) "...why are here promises resolved...after the setTimeouts (asynchronously) gets executed"



The answer to (A) is: Although you resolve them synchronously, then always calls its callback asynchronously. It's one of the guarantees promises provide. You're resolving p1 (in that edit) before the executor function returns. But the way you're observing the resolutions ensures that you observe the resolutions in order, because you don't start observing p2 until p1 has resolved, and then you don't start observing p3 until p2 is resolved.



The answer to (B) is: They don't, you're resolving them synchronously, and then observing those resolutions asynchronously, and since they're already resolved that happens very quickly; later, the timer callbacks run. Let's look at how you create p1 in that edit:



var p1 = new Promise(function (resolve, reject) {

setTimeout(() => console.log("first"), 5000);
resolve("first resolved")
});


What happens there is:




  1. new Promise gets called

  2. It calls the executor function


  3. The executor function calls setTimeout to schedule a callback

  4. You immediately resolve the promise with "first resolved"

  5. new Promise returns and the resolved promise is assigned to p1

  6. Later, the timeout occurs and you output "first" to the console



Then later you do:



p1.then((val) => {
console.log("(1)", val)

return p2
})
// ...


Since then always calls its callback asynchronously, that happens asynchronously — but very soon, because the promise is already resolved.



So when you run that code, you see all three promises resolve before the first setTimeout callback occurs — because the promises aren't waiting for the setTimeout callback to occur.



You may be wondering why you see your final then callback run before you see "third" in the console, since both the promise resolutions and the console.log("third") are happening asynchronously but very soon (since it's a setTimeout(..., 0) and the promises are all pre-resolved): The answer is that promise resolutions are microtasks and setTimeout calls are macrotasks (or just "tasks"). All of the microtasks a task schedules are run as soon as that task finishes (and any microtasks that they schedule are then executed as well), before the next task is taken from the task queue. So the task running your script does this:





  1. Schedules a task for the setTimeout callback

  2. Schedules a microtask to call p1's then callback

  3. When the task ends, its microtasks are processed:


    1. The first then handler is run, scheduling a microtask to run the second then handler

    2. The second then handler runs and schedules a micro task to call the third then handler

    3. Etc. until all the then handlers have run



  4. The next task is picked up from the task queue. It's probably the setTimeout callback for p3, so it gets run and "third" appears in the console





  1. Return value vs. resolve promise:





The part you've put in the question doesn't make sense to me, but your comment on this does:




I read that returning a value or resolving a promise is same...




What you've probably read is that returning a value from then or catch is the same as returning a resolved promise from then or catch. That's because then and catch create and return new promises when they're called, and if their callbacks return a simple (non-promise) value, they resolve the promise they create with that value; if the callback returns a promise, they resolve or reject the promise they created based on whether that promise resolves or rejects.



So for instance:




.then(() => {
return 42;
})


and



.then(() => {
return new Promise(resolve => resolve(42));
})



have the same end result (but the second one is less efficient).



Within a then or catch callback:




  1. Returning a non-promise resolves the promise then/catch created with that value

  2. Throwing an error (throw ...) rejects that promise with the value you throw

  3. Returning a promise makes then/catch's promise resolve or reject based on the promise the callback returns



json - Convert java.util.Date to java.sql.Date with 'yyyy-mm-dd hh mm ss'

I need to pass json string to one of the rest web service. There is a property called 'amortizationdate' which will have format 'yyyy-mm-dd hh mm ss'.




for e.g. {amortizationdate:2015-07-31 00:00:00}



we are having AmortizationVO.java with a property amortizationdate.



for e.g



private java.sql.Date amortizationDate.

public void setAmortizationDate(java.sql.Date date){


}


We need to set the date by calling setAmortizationDate(..date) method and using Jackson to convert AmortizationVO.java to Json.



but in JSON I m getting {amortizationdate:2015-07-31}. But expected result should be with timestamp.(amortizationdate:2015-07-31 00:00:00)



note: I don't want to use util date in my Value Object.



Pls help.




What I've tried:



java.util.Date utilDate = new java.util.Date();
java.sql.Timestamp sq = new java.sql.Timestamp(utilDate.getTime());


ExcelEntityAddressVO entityAddressVO = new ExcelEntityAddressVO();
entityAddressVO.setAmortizationDate(new java.sql.Date(sq.getTime()));



This is my JSON:



{
"amortizationdate" : "2015-07-31",
}

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...