ternary

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

Javascript Ternary operator with empty else

三世轮回 提交于 2019-12-12 07:23:03
问题 I'm trying to convert the following if-else to it's ternary operator representation in javascript as follows var x = 2; if (x === 2) {alert("2");} else { //do nothing} But when I do this: (t==2)?(alert("1")):(); Chrome throws a SyntaxError. My question is - How to have a ternary operator in javascript with an empty "else" branch - i.e the part that comes after ":". Also, is this allowed- using a ternary operator in javascript to execute statements - NOT do assignment. Also: the above code was

PHP bug with ternary operator?

醉酒当歌 提交于 2019-12-12 04:13:19
问题 function foo() { return $result = bar() ? $result : false; } function bar() { return "some_value"; } foo(); Notice: Undefined variable: result Is this a bug? bar() should be saved to $result, but it doesn't. However the condition works fine and it's trying to return $result or false statement (in case bar() is NULL or false) PHP 5.4.24 回答1: That's because operators precedence. Do function foo() { return ($result = bar()) ? $result : false; } -so assignment will be evaluated with higher

PHP nested ternary issue

空扰寡人 提交于 2019-12-12 01:52:50
问题 I've got nested ternary operators in my code like this: $error = $fault_all ? "ALL" : $fault_twothirds ? "TWOTHIRDS" : $fault_onethird ? "ONETHIRD" : "UNKNOWN"; echo 'STATEERROR: ' . $error . ';'; They are listed in order of my preference left to right so if $fault_all and $fault_twothirds are true, I want "ALL" to be assigned to $error; The same if all of them are true. If all are false "UNKNOWN" should be assigned to error. However, if any of them are true only "ONETHIRD" is returned, if

How to correctly use the ternary operator

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 22:50:04
问题 Given the the below two statements, could someone help me use only the ternary operator instead of if statements? I have tried a lot to do that: if(strstr(cv[i],a.c_str())) { o=1; p=cv[i]; p=p.substr(a.size()+1); } else o+=4; if (b^*s && c++ + 1 ^ *s) { cout << b; if (b^--c) cout << (c - b>1 ? "-" : ",") << c; if (a) cout << ","; b = c = *s; } 回答1: The ternary operator is an expression, which returns a value when evaluated: <result> = <condition> ? <when true> : <when false> In this pseudo

self-referencing ternary

前提是你 提交于 2019-12-11 19:22:42
问题 I've been doing this for a while: x = x if x else y In all sorts of contexts where x might be None , False , 0 , '' , [] , or {} . I know the purists would rather I: if not x: x = y but forget that, that's not my question. My question is: Is there anything 'wrong' with x = x if x else y besides the fact it's a ternary? Specifically, is it ok to have a ternary self-assign like that. NOTE My qustion is not is x = x if C else y ok. I know it is. Many thanks 回答1: Nope, nothing wrong with it. It's

React - Using ternary to apply CSS class in functional component

橙三吉。 提交于 2019-12-11 14:33:35
问题 I'm relatively new to React and working on a John Conway - Game of Life app. I have built a Gameboard.js functional component for the board itself (which is a child of App.js ) and a Square.js functional component which represents an individual square in the board (and is a child of Gameboard and a grandchild of App ). In App I have a function called alive which I want to change the color of an individual square when it is clicked by the user. App also has an 'alive' property in it's state

Ternary graph programming to retrieve the data

非 Y 不嫁゛ 提交于 2019-12-11 08:57:20
问题 I need help regarding a ternary graph programming what the idea is: A colorful triangle that has the word "Finances" in one corner, "Freedom" in another, and "Fulfillment" in the last corner. The user should be able to use their mouse cursor to hover over the triangle. Below the cursor should be a little bubble or dot that follows the cursor. When the mouse is clicked, the bubble or dot should stick to that location on the ternary graph. The user should be able to click in a new location to

Nitpicking: Tcl storing in a variable or not, for speed, in procedures

瘦欲@ 提交于 2019-12-11 05:34:30
问题 This is something of a general question. Assuming TCL 8.6, let's ay that I have a rather short procedure. I have 2 ways of returning the value of interest: 1. use some standard practice if/else code, store in a variable, and return the variable's value. For example: proc me { gop goo } { if { [ lomsa $gop ] { set ret [ foo $goo $gop ] } else { set ret [ bar $gop $goo ] } return $ret } 2. use the ternary argument, and basically, have the procedure no added private variable (that is, only the

Is there a simple way to test if you match one of a set of enumerations?

隐身守侯 提交于 2019-12-10 23:08:56
问题 Consider this code... switch(testValue) { case .ValueA, .ValueB, .ValueC: return 40 default: return 0 } Now if I was just checking for a single enumeration value, I could do this... return (testValue == .ValueA) ? 40 : 0; But I'm wondering how I can have something like the latter, but testing for multiples like the former, similar to this pseudo-code... return (testValue is in [.ValueA, .ValueB, .ValueC]) ? 40 : 0; I know I can do it with an inline array, like so... return ([SomeEnum.ValueA,