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...
Slipnoose asked 14/4, 2011 at 8:49

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...
Banket asked 22/9, 2016 at 13:1

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....
Aedes asked 1/1, 2010 at 6:18

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...
Atmometer asked 21/3, 2010 at 21:6

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...
Feinberg asked 4/6, 2013 at 4:39

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....
Suppliant asked 26/1, 2010 at 6:9

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...
Lizbeth asked 23/7, 2023 at 5:34

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...
Hahnke asked 27/10, 2009 at 11:54

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(): ...
Ephod asked 12/12, 2008 at 19:56

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...
Concenter asked 1/3, 2012 at 19:45

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...
Siliceous asked 13/1, 2014 at 18:36

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

© 2022 - 2024 — McMap. All rights reserved.