ternary-operator

Conditional statement in a one line lambda function in python?

左心房为你撑大大i 提交于 2019-11-29 20:23:51
Apologies if this has been asked before, but I couldn't see it anywhere. Essentially I've come across a scenario where i need to make use of an if statement inside a lambda function. What makes it difficult is that ideally it needs to be in a single line of code (if thats even possible?) Normally, i would write this: T = 250 if (T > 200): rate = 200*exp(-T) else: rate = 400*exp(-T) return (rate) However i need it to look like this: rate = lambda(T) : if (T>200): return(200*exp(-T)); else: return(400*exp(-T)) I realize the easier thing to do would to take the decision making outside of the

Difference between using a ternary operator or just short-circuit evaluation?

爱⌒轻易说出口 提交于 2019-11-29 16:08:20
Recently came across short-circuit evaluation and was a little confused by it as i only got into programming the past week. From what i understand if what ever comes before the first double pipe is true then it will stop and not evaluate what comes after the double pipe. For Example: Example 1: var a = true; var b = a || {}; So i assume if a exists then assign a to b otherwise b is equal to an object. What i dont understand is where i will use this and how it differs to a ternary operator, isn't the short circuit evaluation the same as: Example 2: var a = true; var b = (a) ? a : {}; Why would

Calling functions in C# ternary operator [duplicate]

99封情书 提交于 2019-11-29 15:15:36
This question already has an answer here: C# Conditional Operator Not a Statement? 9 answers Why is this code not valid? Pretty sure it's legit in C /C++ Pseudocode: String s = Console.ReadLine(); int x = 0; Int32.TryParse(s, out x) ? Console.WriteLine("Foo") : Console.WriteLine("bar"); The ternary operator is used to return values and those values must be assigned. If you want to invoke void methods in a ternary operator, you can use delegates like this: String s = Console.ReadLine(); int x = 0; (Int32.TryParse(s, out x) ? new Action(() => Console.WriteLine("Foo")) : () => Console.WriteLine(

The Ternary Operator in PHP [duplicate]

天大地大妈咪最大 提交于 2019-11-29 14:49:06
This question already has an answer here: How to concatenate multiple ternary operator in PHP? 8 answers $chow = 3; echo ($chow == 1) ? "one" : ($chow == 2) ? "two" : "three"; output: three $chow = 1; echo ($chow == 1) ? "one" : ($chow == 2) ? "two" : "three"; output: two Can anyone explain why the output is "two" when $chow = 1 instead of "one"? Rizier123 This is because the ternary operator ( ?: ) is left associative so this is how it's getting evaluated: ((1 == 1) ? "one" : (1 == 2)) ? "two" : "three" So 1 == 1 -> TRUE means that then it's: "one" ? "two" : "three" And "one" -> TRUE so the

java: weird NullPointerException in ternary operator (? : )

天大地大妈咪最大 提交于 2019-11-29 13:38:32
问题 Please consider this code snippet: private static void doSomething(Double avg, Double min, Double sd) { final Double testMin; if (avg != null) { testMin = Math.max(min, avg - 3 * sd); } else { testMin = min; } System.out.println("testMin=" + testMin); final Double verwachtMin = avg != null ? Math.max(min, avg - 3 * sd) : min; System.out.println("verwachtMin=" + verwachtMin); } As far as I know (and for what my IDE can tell me), the variables testMin and verwachtMin should be equivalent. As

Does the VB.NET “If” operator cause boxing?

谁都会走 提交于 2019-11-29 13:24:16
Those of us who've worked in VB/VB.NET have seen code similar to this abomination: Dim name As String = IIf(obj Is Nothing, "", obj.Name) I say "abomination" for three simple reasons: IIf is a function , all of whose parameters are evaluated; hence if obj is nothing in the above call then a NullReferenceException will be thrown. This is unexpected behavior for someone who's accustomed to short-circuited ternary operators in languages like C#. Because IIf is a function, it thus incurs the overhead of a function call. Again, though this isn't a big deal, it just doesn't feel right to someone who

Ternary operators and variable reassignment in PHP

送分小仙女□ 提交于 2019-11-29 13:03:45
I've perused the questions on ternary operators vs. if/else structures , and while I understand that under normal circumstances there is no performance loss/gain in using ternary operators over if/else structures, I've not seen any mention of this situation. Language specific to PHP (but any language agnostic details are welcome) does the interpreter reassign values in situations like this: $foo = 'bar' $foo = strlen($foo) > 3 ? substr($foo, 0, 3) : $foo; Since this would evaluate to $foo = $foo; is this inefficient, or does the interpreter simply overlook/discard this evaluation? On a side

Ternary operator

自闭症网瘾萝莉.ら 提交于 2019-11-29 11:52:59
Is there any logical reason that will explain why in ternary optor both branches must have the same base type or be convertible to one? What is the problem in not having this rule? Why on earth I can't do thing like this (it is not the best example, but clarifies what I mean): int var = 0; void left(); int right(); var ? left() : right(); Expressions must have a type known at compile time. You can't have expressions of type "either X or Y", it has to be one or the other. Consider this case: void f(int x) {...} void f(const char* str) {...} f(condition ? 5 : "Hello"); Which overload is going to

Ternary operator associativity in C# - can I rely on it?

谁说胖子不能爱 提交于 2019-11-29 11:27:56
问题 Ahh, don't you just love a good ternary abuse? :) Consider the following expression: true ? true : true ? false : false For those of you who are now utterly perplexed, I can tell you that this evaluates to true . In other words, it's equivalent to this: true ? true : (true ? false : false) But is this reliable? Can I be certain that under some circumstances it won't come to this: (true ? true : true) ? false : false Some might say - well, just add parenthesis then or don't use it altogether -

C++11 constexpr function compiler error with ternary conditional operator (?:)

試著忘記壹切 提交于 2019-11-29 11:21:43
What is wrong with this piece of code? #include <iostream> template<unsigned int N, unsigned int P=0> constexpr unsigned int Log2() { return (N <= 1) ? P : Log2<N/2,P+1>(); } int main() { std::cout << "Log2(8) = " << Log2<8>() << std::endl; return 0; } When compiling with gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) , I get the following error: log2.cpp: In function ‘constexpr unsigned int Log2() [with unsigned int N = 0u, unsigned int P = 1023u]’: log2.cpp:5:38: error: template instantiation depth exceeds maximum of 1024 (use -ftemplate-depth= to increase the maximum) instantiating