ternary-operator

Are ternary statements faster than if/then/else statements in javascript?

一笑奈何 提交于 2019-12-10 03:57:57
问题 I see a lot of: var something = (is_something_true()) ? 3 : 4; in javascript. Is this faster than var something; if (is_something_true()) { something = 3; } else { something = 4; } Or is it written concisely for convenience? 回答1: Please enjoy this -- if difference is statistically valid then the result (true or false) also matters -- clearly this is just other stuff on the machine having an impact on browser performance: Here is the link There is a fundamental difference between the two, the

Initializing reference variables with the conditional operator

十年热恋 提交于 2019-12-10 03:39:56
问题 The following C++ is invalid because reference variables require initializers: int& a; // illegal if (isfive) { a = 5; } else { a = 4; } However, MSVC seems to think this is okay: int& a = isfive ? 5 : 4; This implies to me that MSVC is actually treating the conditional operator like a single expression and not expanding it into an if-else statement. Is it always valid C++ to initialize a reference using the conditional operator? 回答1: MSVC has a non-standard "extension". What it means is that

Can I use GCC's __builtin_expect() with ternary operator in C

安稳与你 提交于 2019-12-09 14:54:41
问题 The GCC manual only shows examples where __builtin_expect() is placed around the entire condition of an 'if' statement. I also noticed that GCC does not complain if I use it, for example, with a ternary operator, or in any arbitrary integral expression for that matter, even one that is not used in a branching context. So, I wonder what the underlying constraints of its usage actually are. Will it retain its effect when used in a ternary operation like this: int foo(int i) { return __builtin

Incompatible operand types when using ternary conditional operator

こ雲淡風輕ζ 提交于 2019-12-09 14:16:44
问题 This code: bool contains = std::find(indexes.begin(), indexes.end(), i) != indexes.end(); CardAbility* cardAbility = contains ? new CardAbilityBurn(i) : new CardAbilityEmpty; gives me the following error: Incompatible operand types CardAbilityBurn and CardAbilityEmpty However if I write the code like this: if (contains) { cardAbility = new CardAbilityBurn(i); } else { cardAbility = new CardAbilityEmpty; } then the compiler doesn't mind. Why so? I want to use ternary conditional operator

Alternative to nested ternary operator in JS

孤街醉人 提交于 2019-12-09 14:03:52
问题 I personally love ternary operators, and in my humble opinion, they make complicated expressions very easy to digest. Take this one: word = (res.distance === 0) ? 'a' : (res.distance === 1 && res.difference > 3) ? 'b' : (res.distance === 2 && res.difference > 5 && String(res.key).length > 5) ? 'c' : 'd'; However in our project's ESLINT rules nested ternary operators are forbidden, so I have to get rid of the above. I'm trying to find out alternatives to this approach. I really don't want to

Ruby ternary operator in erb?

非 Y 不嫁゛ 提交于 2019-12-09 02:41:55
问题 How can I make this code look better: <%=raw manuscript.uploaded_to_s3? ? "<span style=\"color:green;\">" : "<span style=\"color:red;\">" %> That is, can the HTML go outside of the ERB block making this easier to read? 回答1: <span style="color:<%= manuscript.uploaded_to_s3? ? 'green' : 'red' %>"> I would advocate a CSS class rather than style attribute 8P: <span class="<%= manuscript.uploaded_to_s3? ? 'green' : 'red' %>"> 来源: https://stackoverflow.com/questions/5248625/ruby-ternary-operator-in

Ternary Operator Associativity

我的梦境 提交于 2019-12-08 17:06:55
I am having trouble understanding the concept of associativity in the context of ternary operators. In most cases, ternary operators look like this: a ? b : c In this case, no associativity is needed to evaluate the expression. Sometimes though, Ternary Operators are nested: a ? b : c ? d : e a ? b : (c ? d : e) // : is right-associative However, the nesting could also be inverted a ? b ? c : d : e a ? (b ? c : d) : e // : is left-associative What is the best way to explain this phenomenon? Could you consider the associativity of the : operator to be context-dependant, or am I missing

Unexpected output using Pythons' ternary operator in combination with lambda

这一生的挚爱 提交于 2019-12-08 16:13:50
问题 I have a specific situation in which I would like to do the following (actually it is more involved than this, but I reduced the problem to the essence): >>> (lambda e: 1)(0) if (lambda e: True)(0) else (lambda e: 2)(0) True which is a difficult way of writing: >>> 1 if True else 2 1 but in reality '1','True' and '2' are additional expressions that get evaluated and which require the variable 'e', which I set to '0' for this simplified code example. Note the difference in output from both

Can the conditional operator lead to less efficient code?

不想你离开。 提交于 2019-12-08 16:05:33
问题 Can ?: lead to less efficient code compared to if/else when returning an object? Foo if_else() { if (bla) return Foo(); else return something_convertible_to_Foo; } If bla is false, the returned Foo is directly constructed from something_convertible_to_Foo . Foo question_mark_colon() { return (bla) ? Foo() : something_convertible_to_Foo; } Here, the type of the expression after the return is Foo , so I guess first some temporary Foo is created if bla is false to yield the result of the

Initialize arrays using ternary operator

让人想犯罪 __ 提交于 2019-12-08 15:58:40
问题 i tried something like this: boolean funkyBoolean = true; int array[] = funkyBoolean ? {1,2,3} : {4,5,6}; But this code won't even compile. Is there any explanation for this? isn't funkyBoolean ? {1,2,3} : {4,5,6} a valid expression? thank's in advance! 回答1: You can only use the {1, 2, 3} syntax in very limited situations, and this isn't one of them. Try this: int array[] = funkyBoolean ? new int[]{1,2,3} : new int[]{4,5,6}; By the way, good Java style is to write the declaration as: int[]