variable-assignment

ambiguity of `<<-` when defining it for `x < y <- z`

最后都变了- 提交于 2019-12-06 00:53:21
问题 @g-grothendieck's answer to this question inspired me to play with some assignment functions such as ==<- or ><- . See the following : `><-` <- function(e1,e2,value) replace(e1, e1 > e2, value) x <- c(5,15) x > 10 <- 42 x # [1] 5 42 I can also define it for < : `<<-` <- function(e1, e2, value) replace(e1, e1 < e2, value) x <- c(5,15) x < 10 <- 42 x # [1] 42 15 But the problem is that now the <<- operator is redefined and this doesn't work anymore : x <<- "hello" Error in replace(e1, which(e1

Why does ruby define variables even if it never executes the variable assignment code?

依然范特西╮ 提交于 2019-12-06 00:03:35
Given the following code: a = true # let's assign `a` a value # and let's test if calling `b`, an unassigned variable, throws an error begin puts "The value of b is: #{b.inspect}" rescue NameError => e puts "Caught an error: #{e}" end a || b = true # the assignment should never be executed because `a` is `true` puts "The value of b is: #{b.inspect}" # will calling `b` still raise an error? We get the following result: Caught an error: undefined local variable or method `b' for main:Object The value of b is: nil Even though we expected calling b to raise an error the second time, we see that b

PHP assignment with a default value

北慕城南 提交于 2019-12-05 17:38:40
问题 What's a nicer way to do the following, that doesn't call f() twice? $x = f() ? f() : 'default'; 回答1: In PHP 5.3, you can also do: $a = f() ?: 'default'; See the manual on ?: operator. 回答2: This seems to work fine: $x = f() or $x = 'default'; 回答3: function f() { // conditions return $if_something ? $if_something : 'default'; } $x = f(); 回答4: $x = ($result = foo()) ? $result : 'default'; test 回答5: You could save it to a variable. Testcase: function test() { echo 'here'; return 1; } $t = test()

What's the difference between Initialization and Assignment in C# [duplicate]

隐身守侯 提交于 2019-12-05 16:08:15
Possible Duplicate: C# variable initializations vs assignment Just like in the title, could someone please explain what is the difference between Initialization and Assignment in C#? I'm preparing for a test and I wanted to know what's the best way to answer this type of question. Thanks Cheers, n1te When you initialize a variable you're declaring it into existence. PlasticCup mySippyCup = new PlasticCup(); When you assign, you're just saying "this water" goes into "this cup". The cup already exists. mySippyCup = new PlasticCup(); Initialization is assigning value while declaring the variable

replace() vs “[<-”?

旧城冷巷雨未停 提交于 2019-12-05 15:53:28
I recently stumbled across replace() and "[<-" . They seem to have similar functionality, for example with "[<-" I can do something like this: > x.tst <- array(1:6, c(2,3)) > s.tst <- array(0, c(2,3)) > s.tst [,1] [,2] [,3] [1,] 0 0 0 [2,] 0 0 0 > s.tst[1:3] <- 1 > "[<-"(x.tst, s.tst==1, 0) [,1] [,2] [,3] [1,] 0 0 5 [2,] 0 4 6 > x.tst [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 Can somebody help to clarify the difference? What are the strengths of replace vs "[<-" and vis versa? They're basically exactly the same thing. If you look at the source code of replace, you'll see : function (x, list, values

Difference between creating a local variable and assigning to ivar and directly assigning to ivar?

喜你入骨 提交于 2019-12-05 15:22:13
I have always wondered why all apple code samples use code like this: UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController]; self.navigationController = aNavigationController; [self.view addSubview:[navigationController view]]; [aNavigationController release]; They always create a local variable and assign it to the ivar why don't they simply do this: self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];; [self.view addSubview:[navigationController view]];

Unneeded parts when unwrapping tuple/list

*爱你&永不变心* 提交于 2019-12-05 14:30:01
Python is all about writing beautiful code. So, I was running pylint to check the "beautifulness" of my code, when I bump into something: Unused variable 'myvar1' From this part of my code: for myvar1, myvar2 in mylist: # Do stuff just using myvar2 mylist is a list of tuples, so I'm unwrapping the tuples into two variables ( myvar1 and myvar2 ). I'm defining those two variables just to unwrap the second one, because I don't need the other. So, here's my question: Is there a way to tell the interpreter to unwrap the tuple, but not assing the first part (for example). In some other languages you

Python variable assignment question

折月煮酒 提交于 2019-12-05 13:32:46
a,b = 0,1 while b < 50: print(b) a = b b = a+b outputs: 1 2 4 8 16 32 wheras: a,b = 0,1 while b < 50: print(b) a,b = b, a+b outputs (correct fibonacci sequence): 1 1 2 3 5 8 13 21 34 Aren't they the same? I mean a,b = b, a+b is essentially the same as a = b and b = a+b written separately -- no? No, they are not the same. When you write a,b = b, a+b , the assignments are done "simultaneously". a,b = b, a+b is same as (a, b) = (b, a+b) . So, after a, b = 5, 8 a=5 and b=8. When Python sees this (a, b) = (b, a+b) it first calculates the right side (b, a+b) which is (8,13) and then assigns (this

Assignment in R language

半腔热情 提交于 2019-12-05 12:16:42
问题 I am wondering how assignment works in the R language. Consider the following R shell session: > x <- c(5, 6, 7) > x[1] <- 10 > x [1] 10 6 7 > which I totally understand. The vector (5, 6, 7) is created and bound to the symbol 'x'. Later, 'x' is rebound to the new vector (10, 6, 7) because vectors are immutable data structures. But what happens here: > c(4, 5, 6)[1] <- 10 Error in c(4, 5, 6)[1] <- 10 : target of assignment expands to non-language object > or here: > f <- function() c(4, 5, 6)

println in scala for-comprehension

血红的双手。 提交于 2019-12-05 08:48:06
问题 In a for-comprehension, I can't just put a print statement: def prod (m: Int) = { for (a <- 2 to m/(2*3); print (a + " "); b <- (a+1) to m/a; c = (a*b) if (c < m)) yield c } but I can circumvent it easily with a dummy assignment: def prod (m: Int) = { for (a <- 2 to m/(2*3); dummy = print (a + " "); b <- (a+1) to m/a; c = (a*b) if (c < m)) yield c } Being a side effect, and only used (so far) in code under development, is there a better ad hoc solution? Is there a serious problem why I