syntactic-sugar Questions
1
Solved
Let a = [-1, -2, -3]. I want to modify the list a so that a == [-1, -2, -3, 1, 2, 3], and I want to use map to achieve that.
I have the following different pieces of code I've written to do t...
Mulberry asked 19/3 at 7:46
7
I've noticed that the new ExpandoObject implements IDictionary<string,object> which has the requisite IEnumerable<KeyValuePair<string, object>> and Add(string, object) methods and...
Pettaway asked 6/5, 2011 at 10:54
5
Solved
As I've understood it there are two ways to do a Python decorator, to either use the __call__ of a class or to define and call a function as the decorator. What's the advantages/disadvantages of th...
Weatherproof asked 24/4, 2012 at 8:2
6
Solved
I've been puzzling about this for a while and I've looked around a bit, unable to find any discussion about the subject.
Lets assume I wanted to implement a trivial example, like a new looping con...
Polito asked 31/7, 2012 at 21:24
2
Solved
I need to build a list from a string in python using the [f(char) for char in string] syntax and I would like to be able to ignore (not insert in the list) the values of f(x) which are equal to Non...
Aeronautics asked 10/10, 2010 at 12:2
2
Solved
Lists or numpy arrays can be unpacked to multiple variables if the dimensions match. For a 3xN array, the following will work:
import numpy as np
a,b = [[1,2,3],[4,5,6]]
a,b = np.array([[1,2,3],[...
Whittemore asked 7/7, 2018 at 17:6
4
Solved
JavaScript has a nifty feature where you can assign several variables from properties in an object using one concise line. It's called destructuring assignment syntax which was added in ES6.
// Ne...
Nester asked 2/3, 2016 at 17:22
5
Solved
A common way to assign multiple variables is often expressed in programming languages such as C or Python as:
a = b = c = value;
Is there an equivalent to this in Rust, or do you need to write i...
Exonerate asked 7/8, 2016 at 4:51
3
I find myself doing this kind of thing somewhat often:
$foo = true;
$foo = $foo && false; // bool(false)
With bitwise operators, you can use the &= and |= shorthand:
$foo = 1;
$foo ...
Oersted asked 9/7, 2013 at 16:5
29
Solved
Consider:
List<String> someList = new ArrayList<>();
// add "monkey", "donkey", "skeleton key" to someList
for (String item : someList) {
System.out.pr...
Christchurch asked 17/9, 2008 at 16:44
6
Solved
Note: I've seen this question asked sometimes before (a, b, c), but neither of these was in C#, nor helpful.
Assume I'm using the ? : ternary operator like this (to do nothing when false is the ca...
Outroar asked 14/12, 2016 at 15:57
6
Solved
Let's say that I want to write something like this (the {1, 3, 7, 42, 69, 550123} set is known before compilation):
int x;
...
if (x == 1 || x == 3 || x == 7 || x == 42 || x == 69 || x == 5550123)
...
Acrogen asked 24/7, 2018 at 10:33
4
Solved
suppose you have a function that can return some object or None:
def foobar(arg):
if arg == 'foo':
return None
else:
return 'bar'
Now you call this method and you want to do something with t...
Boxfish asked 24/1, 2014 at 9:53
2
Solved
What is new without type in C#?
I met the following code at work:
throw new("some string goes here");
Is the new("some string goes here") a way to create strings in C# or is it ...
Flense asked 4/10, 2021 at 8:6
4
Solved
Is there idiomatic way of applying and assigning object variable method call, but only if it's defined (both method and the result)?
Like using safe call operator .? and defined-or operator //, and...
Sanitarium asked 30/7, 2021 at 20:21
4
Solved
Haskell's record syntax is considered by many to be a wart on an otherwise elegant language, on account of its ugly syntax and namespace pollution. On the other hand it's often more useful than the...
Edmond asked 20/3, 2011 at 6:51
5
Solved
Let's take a look at the following code:
int arr[n];
// s.t. i<n
arr[i] = 12;
// s.t. i<n
*(arr + i) = 12;
Is arr[i] is a syntactic sugar for *(arr+ i) ?
Luminiferous asked 18/2, 2018 at 12:21
2
In python using // for division forces the result to be an integer. Is there an equivalent for multiplication?
For example, assume I have an integer W which I scale by a float f. It could be nice...
Goldiegoldilocks asked 27/1, 2020 at 8:22
8
Solved
In VB.NET there is the WITH command that lets you omit an object name and only access the methods and properties needed. For example:
With foo
.bar()
.reset(true)
myVar = .getName()
End With
...
Octangular asked 29/9, 2009 at 20:38
4
Solved
Consider the example below:
m = [{'a':1},{'b':2}]
I wanted to find a short way of forming a list of the keys in m, just like ['a','b']. What would be the shortest or the easiest way rather th...
Brute asked 17/5, 2019 at 9:18
5
Solved
The compiler compiles a foreach loop into something like a for loop when the foreach is used with an array. And the compiler compiles a foreach loop into something like a while loop when the foreac...
Hardden asked 28/4, 2011 at 9:55
3
I just noticed this construct somewhere on web:
val list = List(someCollection: _*)
What does _* mean? Is this a syntax sugar for some method call? What constraints should my custom class satisf...
Palila asked 14/11, 2010 at 6:22
5
Solved
Are the two identical?
Suppose you have:
var x = true;
And then you have one of either:
x && doSomething();
or
if(x) doSomething();
Is there any differene whatsoever between the t...
Mneme asked 30/9, 2012 at 19:16
10
Solved
I have been looking into Ruby and find its keywords "until" and "unless" very interesting. So I thought what was a good way to add similar keywords into C/C++. This is what I came up with:
#define...
Cila asked 9/4, 2011 at 19:23
2
Solved
Scala only sometimes desugars
a += b
to
a = a + b
but not always. For example, some mutable collections define a += method, where instead it becomes
a.+=(b)
Is this behaviour
entirely de...
Brame asked 21/8, 2018 at 15:28
1 Next >
© 2022 - 2024 — McMap. All rights reserved.