variable-assignment

How to modify just one field of a record without rewriting it completely? [duplicate]

两盒软妹~` 提交于 2019-12-14 03:56:08
问题 This question already has answers here : Shorthand way for assigning a single field in a record, while copying the rest of the fields? (3 answers) Is there a Haskell idiom for updating a nested data structure? (3 answers) Closed 8 months ago . It's the second time I'm tackling this problem... And for the second time this is while working with the State monad, apparently a state likes to consist of many fields, not just one I have a tuple: type CurrentState = (Int, Int, String, [Int],

Parallel list assignment in Red language

大城市里の小女人 提交于 2019-12-14 03:51:00
问题 I have 2 lists: alist: [a b c d] blist: [1 2 3 4] (In reality they are long lists). How can I assign variables in alist to corresponding values in blist in one go? Hence a becomes 1, b becomes 2 and so on. I tried: foreach i alist j blist [i: j] But it give following error: *** Script Error: j has no value *** Where: foreach *** Stack: I also tried: i: 1 while [true] [ if i > (length? alist) [break] alist/i: blist/i i: i + 1 ] But it also does not work: *** Script Error: cannot set none in

Python vars nested assignment

倾然丶 夕夏残阳落幕 提交于 2019-12-14 02:33:08
问题 An example of what the title talks about: #seen in a demo of PyOpenGL # http://PyOpenGL.sourceforge.net/ size = self.size = self.GetClientSize() What is this used for? How does it works and when using it? My idea is this allows to assign the value of the third item to the first and the second... If that's it, we can nest an infinite number of vars. 回答1: It is a chained assignment. You set both size and self.size to the return value of self.GetClientSize() . You can chain assignments with

Can I conditionally choose what variable I assign a value to?

不羁岁月 提交于 2019-12-14 01:36:26
问题 I know that you can do things like this in Python: var = value1 if( booleanCheck() ) else value2 What I want to know is if I can conditionally choose which var I place a value into with a similar sort of structure? something like this: (var1 if( booleanCheck() ) else var2) = value In my case specifically I'm trying to assign a child node to the correct side in a Binary Search Tree, I know I can just do a normal if block but I was trying to challenge myself to using minimal space. 回答1: Well

Compound assignment E1 op= E2 is not equivalent to E1 = E1 op E2

不羁的心 提交于 2019-12-13 16:41:09
问题 cppreference says: the behavior of every builtin compound-assignment expression E1 op= E2 (where E1 is a modifiable lvalue expression and E2 is an rvalue expression or a braced-init-list (since C++11)) is exactly the same as the behavior of the expression E1 = E1 op E2 , except that the expression E1 is evaluated only once and that it behaves as a single operation with respect to indeterminately-sequenced function calls (e.g. in f(a += b, g()) , the += is either not started at all or is

Why does the Java compiler complain about a local variable not having been initialized here?

萝らか妹 提交于 2019-12-13 14:39:04
问题 int a = 1, b; if(a > 0) b = 1; if(a <= 0) b = 2; System.out.println(b); If I run this, I receive: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The local variable b may not have been initialized at Broom.main(Broom.java:9) I know that the local variables are not initialized and is your duty to do this, but in this case, the first if doesn't initialize the variable? 回答1: If you change the second if to else , then the compiler would be happy. int a = 1, b; if(a > 0

Warning: Assignment in condition

非 Y 不嫁゛ 提交于 2019-12-13 13:24:31
问题 One thing that has always bugged me is that when checking my php scripts for problems I get the warning "bool-assign : Assignment in condition" and I get them a lot. e.g.: $guests = array(); $sql = "SELECT * FROM `guestlist`"; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)) $guests[] = $row['name']; Is there a different way to get multiple or all rows into an object or array? Or is there nothing wrong with this method? 回答1: try doing this instead: $guests = array(); $sql

Why use a closure for assignment instead of directly assigning a value to a key?

我的未来我决定 提交于 2019-12-13 12:27:26
问题 I was watching this video and at 7:10 he's adding a db dependency and is using a closure to assign the value. My question is why not just use direct assignment instead, I mean isn't doing this: $container['db'] = $capsule; equivalent to doing this: $container['db'] = function ($container) use ($capsule) { return $capsule; } If not, what is the difference and which way is better? 回答1: TLDR it's because defining dependencies as closures makes it possible for dependency injection container to

When you assign-by-value an array variable in PHP, is the array really deep-copied?

孤街浪徒 提交于 2019-12-13 10:36:45
问题 It seems really inefficient that in PHP, when an array is assigned by value all of its internal elements are recursively copied to the new variable. Is this what really happens internally? 回答1: No, the internally array is not deep-copied on assignment. Consider the following snippet: $a = array(111, 222, 333); $b = $a; $b[0] = 999; If a picture is worth a thousand words, then here is what happens internally when arrays are assigned and then their array elements are modified: 来源: https:/

Why can I assign an existing reference to a literal value in C++?

为君一笑 提交于 2019-12-13 09:46:03
问题 Consider the following: int ival = 1.01; int &rval = 1.01; // error: non-const reference to a const value. int &rval = ival; rval = 1.01; The first assignment of &rval to a literal value fails as expected. If I comment out that line the code compiles and runs. I understand why the initialization fails, but I'm confused why the assignment to rval works in the last line. I didn't think it was allowed to assign a reference to a literal value. EDIT: Thanks for the quick answers. I'm tempted to