idioms Questions
2
Suppose I have a function T foo(size_t i). What would be an elegant and succinct way of constructing an object arr, of type std::array<T, N>, so that we have arr[i] == foo(i)?
If possible, I ...
Amagasaki asked 26/6 at 12:28
6
Solved
Is there an idiomatic C++ way to reserve and recycle identifiers that are guaranteed to be unique? My requirements are:
Assuming there exists an ID not currently reserved, reserve_id(void) return...
3
Solved
I have recently gotten into Go and have seen a lot of discussion about how to do error handling.
the pattern that I have seen laid out is the following:
err := DoSomething()
if err != nil {
//h...
10
Solved
Powershell's array notation has rather bizarre, albeit documented, behavior for slicing the end of arrays. This section from the official documentation sums up the bizarreness rather well:
Negat...
Vespertine asked 11/2, 2015 at 17:7
6
Is there an equivalent in Golang to raising a NotImplementedException in Python when you define an interface with methods that you don't want to implement yet? Is this idiomatic Golang?
For exampl...
Coerce asked 14/12, 2016 at 16:11
2
Solved
If we have the following method in GO:
GetCustomer(id string) (*Customer, error)
A customer for a given id may not exist in DB. When a customer is not found, the code can
return nil, nil
Is this ...
Velarde asked 17/7, 2020 at 5:57
22
Solved
How to implement Named Parameter idiom in Java? (especially for constructors)
I am looking for an Objective-C like syntax and not like the one used in JavaBeans.
A small code example would be fine....
11
Solved
Are there any idioms for returning multiple values from a bash function within a script?
http://tldp.org/LDP/abs/html/assortedtips.html describes how to echo multiple values and process the result...
8
Solved
To create all possible combinations of two sets of parameters and perform an action on them, you can do:
setOf(foo, bar, baz).forEach { a ->
setOf(0, 1).forEach { b ->
/* use a and b */
}...
Unreserve asked 12/12, 2018 at 18:38
3
When taking a substring of a string in Go, no new memory is allocated. Instead, the underlying representation of the substring contains a Data pointer that is an offset of the original string's Dat...
5
Solved
In the following code, I iterate over a string rune by rune, but I'll actually need an int to perform some checksum calculation. Do I really need to encode the rune into a []byte, then convert it t...
Octangular asked 24/1, 2014 at 0:27
11
Solved
Which is preferred ("." indicating whitespace)?
A)
def foo():
x = 1
y = 2
....
if True:
bar()
B)
def foo():
x = 1
y = 2
if True:
bar()
My intuition would be B (that's also what...
Dateline asked 28/4, 2010 at 8:53
48
Solved
I am trying to reverse an int array in Java.
This method does not reverse the array.
for(int i = 0; i < validData.length; i++)
{
int temp = validData[i];
validData[i] = validData[validData....
7
Solved
I'm looking for a more idiomatic way to filter out nil-or-empty elements of an array.
I have many methods of the form:
def joined
[some_method, some_other_method].compact.reject(&:empty?).jo...
Volute asked 21/11, 2012 at 18:52
2
Solved
I'm still a beginner in C++ trying to learn more about the language. I recently read about the concept of ADL (Argument-Dependent Lookup) and Hidden Friends idiom (https://www.modernescpp.com/index...
2
Suppose I am writing a function that takes a bunch of strings and filters out the "bad" ones.
The function then returns some strings, but there is a chance that all the strings are filter...
Nedranedrah asked 18/7, 2023 at 1:12
5
Solved
I like the fact that Go doesn't give me a million ways to do simple things – to borrow from The Zen of Python, “There should be one – and preferably only one – obvious way to do it.”
However, I'm ...
Shimmery asked 11/7, 2013 at 23:48
46
Solved
What does this do, and why should one include the if statement?
if __name__ == "__main__":
print("Hello, World!")
If you are trying to close a question where someone should b...
Taskmaster asked 7/1, 2009 at 4:11
34
Solved
How can I treat the last element of the input specially, when iterating with a for loop? In particular, if there is code that should only occur "between" elements (and not "after&quo...
24
Solved
I'm calling a bunch of methods that return a list. The list may be empty. If the list is non-empty, I want to return the first item; otherwise, I want to return None. This code works:
def main():
...
5
Is there a pattern in C to execute a while loop one more time.
Currently I'm using
while(condition) {
condition = process();
// process() could be multiple lines instead of a function call
// s...
Unlive asked 6/4, 2015 at 16:25
9
I have a Python function which requires a number of parameters, one of which is the type of simulation to perform. For example, the options could be "solar", "view" or "both.
What is a Pythonic wa...
4
Solved
Various Clojure style guides recommend avoiding lines longer than 80 characters. I am wondering if there is an idiomatic way to avoid long String literals.
While it's common these days to have wi...
3
Solved
What is the idiomatic F# way of handling an asynchronous while loop accumulation?
I'm working with the new (still in preview) Azure Cosmos DB SDK. Querying the database returns a CosmosResultSetIt...
Coma asked 27/3, 2019 at 15:9
2
Solved
In C++23, the ranges (sub)library has gained std::ranges::zip, which zips multiple ranges into a single range of std::tuple's (or pairs). This is nice, and precludes requiring implementing this our...
Karakalpak asked 23/11, 2022 at 21:43
1 Next >
© 2022 - 2024 — McMap. All rights reserved.