shorthand

Other Ruby Map Shorthand Notation

僤鯓⒐⒋嵵緔 提交于 2019-11-29 10:59:02
问题 I am aware of the shorthand for map that looks like: [1, 2, 3, 4].map(&:to_s) > ["1", "2", "3", "4"] I was told this is shorthand for: [1, 2, 3, 4].map{|i| i.to_s} This makes perfect sense. My question is this: It seems there should be an easier way to write: [1, 2, 3, 4].map{|x| f.call(x)} for some procedure f. I know the way I just typed isn't all that long to begin with, but I'd contend that neither is the previous example for which the shorthand exists. This example just seems like the

Conditionally passing arbitrary number of default named arguments to a function

旧时模样 提交于 2019-11-29 09:47:43
Is it possible to pass arbitrary number of named default arguments to a Python function conditionally ? For eg. there's a function: def func(arg, arg2='', arg3='def') Now logic is that I have a condition which determines if arg3 needs to be passed, I can do it like this: if condition == True: func('arg', arg2='arg2', arg3='some value') else: func('arg', arg2='arg2') Question is, can I have a shorthand like: func('arg', 'arg2', 'some value' if condition == True else # nothing so default gets picked ) The only way I can think of would be func("arg", "arg2", **({"arg3": "some value"} if condition

Overview of PHP shorthand

微笑、不失礼 提交于 2019-11-28 16:05:59
I've been programming in PHP for years now, but I've never learned how to use any shorthand. I come across it from time to time in code and have a hard time reading it, so I'd like to learn the different shorthand that exists for the language so that I can read it and start saving time/lines by using it, but I can't seem to find a comprehensive overview of all of the shorthand. A Google search pretty much exclusively shows the shorthand for if/else statements, but I know there must be more than just that. By shorthand, I am talking about stuff like: ($var) ? true : false; Click Upvote Here are

Shorthand if/else statement Javascript

佐手、 提交于 2019-11-28 03:48:49
I'm wondering if there's a shorter way to write this: var x = 1; if(y != undefined) x = y; I initially tried x = y || 1 , but that didn't work. What's the correct way to go about this? var x = y !== undefined ? y : 1; Note that var x = y || 1; would assign 1 for any case where y is falsy (e.g. false , 0 , "" ), which may be why it "didn't work" for you. Also, if y is a global variable, if it's truly not defined you may run into an error unless you access it as window.y . As vol7ron suggests in the comments, you can also use typeof to avoid the need to refer to global vars as window.<name> :

Conditionally passing arbitrary number of default named arguments to a function

非 Y 不嫁゛ 提交于 2019-11-28 03:03:36
问题 Is it possible to pass arbitrary number of named default arguments to a Python function conditionally ? For eg. there's a function: def func(arg, arg2='', arg3='def') Now logic is that I have a condition which determines if arg3 needs to be passed, I can do it like this: if condition == True: func('arg', arg2='arg2', arg3='some value') else: func('arg', arg2='arg2') Question is, can I have a shorthand like: func('arg', 'arg2', 'some value' if condition == True else # nothing so default gets

Constructor behaving differently using ES6 shorthand notation

落花浮王杯 提交于 2019-11-27 22:21:46
ES6 introduced a shorthand notation to initialize objects with functions and properties. // ES6 shorthand notation const obj1 = { a(b) { console.log("ES6: obj1"); } }; // ES5 var obj2 = { a: function a(b) { console.log("ES5: obj2"); } }; obj2.a(); obj1.a(); new obj2.a(); new obj1.a(); However, these different notations behave differently, as you can see. If I do new obj1.a() in the browser (tested Chrome and Firefox), I get a TypeError: obj1.a is not a constructor . new obj2.a() behaves completely normally. What happens here? Does anyone have an explanation, and/or links to documentation

Difference in C# between different getter styles

天涯浪子 提交于 2019-11-27 18:54:21
I do sometimes see abbreviations in properties for the getter. E.g. those two types: public int Number { get; } = 0 public int Number => 0; Can someone please tell me if there are any differences between those two. How do they behave? Are both of them read-only? Yes, both of them are read-only, but there is a difference. In the first one, there's a backing field which is initialized to 0 before the constructor is executed. You can change the value only in the constructor , just like a regular read-only field. The getter itself just returns the value of the field. In the second one, the getter

PHP shorthand for isset()? [duplicate]

我的未来我决定 提交于 2019-11-27 11:25:08
This question already has an answer here: What is the PHP shorthand for: print var if var exist 11 answers Is there a shorthand way to assign a variable to something if it doesn't exist in PHP? if(!isset($var) { $var = ""; } I'd like to do something like $var = $var | ""; hek2mgl Update for PHP 7 (thanks shock_gone_wild ) PHP 7 introduces the so called null coalescing operator which simplifies the below statements to: $var = $var ?? "default"; Before PHP 7 No, there is no special operator or special syntax for this. However, you could use the ternary operator: $var = isset($var) ? $var :

Which “if” construct is faster - statement or ternary operator?

*爱你&永不变心* 提交于 2019-11-27 11:24:28
There are two types of if statements in java - classic: if {} else {} and shorthand: exp ? value1 : value2 . Is one faster than the other or are they the same? statement: int x; if (expression) { x = 1; } else { x = 2; } ternary operator: int x = (expression) ? 1 : 2; There's only one type of "if" statement there. The other is a conditional expression. As to which will perform better: they could compile to the same bytecode, and I would expect them to behave identically - or so close that you definitely wouldn't want to choose one over the other in terms of performance. Sometimes an if

javascript shorthand if statement, without the else portion

≡放荡痞女 提交于 2019-11-27 07:58:33
So I'm using a shorthand javascript if / else statement (I read somewhere they're called Ternary statements?) this.dragHandle.hasClass('handle-low') ? direction = "left" : direction = "right" This works great, but what if later I want to use just a shorthand if , without the else portion. Like: direction == "right" ? slideOffset += $(".range-slide").width() is this possible at all? you can use && operator - second operand expression is executed only if first is true direction == "right" && slideOffset += $(".range-slide").width() in my opinion if(conditon) expression is more readable than