1,242 questions
0
votes
1
answer
58
views
What's the OpenCL idiom for elementwise array-lookup / gather operation with vectorized types?
Consider the following OpenCL code in which each element in a vector-type variable gets its value via array lookup:
float* tbl = get_data();
int4 offsets = get_offsets();
float4 my_elements = {
...
34
votes
2
answers
4k
views
Why is "return by value" idiomatic in Rust (as opposed to out parameters)?
I am starting to learn Rust. In a lot of examples I have come across so far, I have noticed that functions are often implemented to return variables by value even if they are of a complex data type ...
0
votes
1
answer
33
views
How should I perform an elementwise cast of an OpenCL C vector value?
OpenCL C supports "vector data types" - a fixed number of scalar types which may be operated on together, as though they were a single scalar, mostly: we can apply arithmetic and logic ...
5
votes
0
answers
250
views
Am I over-encouraging people to use shared_ptr's? [closed]
In this earlier SO question:
What is a smart pointer and when should I use one?
Ten years ago, I gave a relative simple answer and got a bunch of upvotes. One paragraph in my answer reads:
Use std::...
-5
votes
1
answer
80
views
An idiom for async functions with "busy" indication and progress logging?
I'm working on some JS code (which will run within Thunderbird, but that's not the main point). This code is kind of old, and does asynchronous work using setTimeout(); I want to transition it to ...
0
votes
2
answers
54
views
Idiomatic way of checking the widget tree downward
Please note that the example we'll be looking at is purelly for illustration purposes. If you would like to answer using a different example that's ok, as we're trying to get to an idiomatic way of ...
-3
votes
3
answers
222
views
How should I idiomatically represent time intervals (not just durations) in C++?
I like C++11's std::chrono facilities, which let me work with:
time points
clocks
durations (= differences between time points)
but I occasionally need to work with time intervals, i.e. not just the ...
-1
votes
1
answer
28
views
In CMake, what's a good idiom for choosing between alternative locations of a header file?
In a C source file of mine, I need to choose between including <linux/uuid.h> and <uuid/uuid.h> (depending on what's available on the file system - it's typically/always just one and not ...
1
vote
1
answer
68
views
Is this Python code a kind of typecast, and, if so, what is going on under the hood? [closed]
I'm editing some Python code containing the line:
arr = (c_double * len(self.static_mapped[0, :]))(*self.static_mapped[0, :])
where field self.static_mapped is created using numpy.zeros and where ...
8
votes
6
answers
2k
views
C++ equivalent of Javascript's '?.' operator for optional-chaining?
In Javascript, if I have a potentially null object obj, which, if not null, will have a field x - I can write obj?.x. This is called "optional chaining" or "safe navigation": If ...
1
vote
1
answer
160
views
Idiomatic alternative for operator logical-and-equals?
C++ has 'accumulator' operations for arithmetic (+,-,*,/) , and for bitwise operations AND and OR - but not for logical operations.
Now, in our code, we sometimes need to conjunct many boolean values -...
1
vote
2
answers
146
views
Is there a Pandas idiom for reading a CSV file with categorical data that has spelling variants?
I have a CSV file with multiple categorical columns, but most of these columns contain messy data due to typing mistakes (e.g., 'spciulated', 'SPICULATED', etc. for the category 'spiculated' of the ...
10
votes
4
answers
867
views
Idiom for initializing an std::array using a generator function taking the index?
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 ...
2
votes
8
answers
543
views
Decent idiom for initializing an optional to either null or a value?
Motivation
With C++ having gotten optional's (in C++17), it is now common to want to write the equivalent of:
If a condition holds, initialize my variable with some expression; and if the condition ...
0
votes
1
answer
107
views
Functional way to implement this OO design with TypeScript
Suppose that I have the following design implemented in an OO language (e.g. Python). I know that the OO way is a bit unnatural and sometimes not preferred when using TypeScript. So what would be the ...
0
votes
1
answer
52
views
How to factor out code into methods in a Ruby script while retaining access to the outer scope?
Suppose I have a Ruby script like the following to generate YAML describing a list of services:
require 'yaml'
environment_slug = ENV.fetch('CI_ENVIRONMENT_SLUG')
YAML.dump([
{
'name' => '...
1
vote
1
answer
120
views
Preventing Derived Class calling Base class's public method
I am writing a base class that provides a public update() method. When someone derives from this base class, they must implement a pure virtual method that controls a tiny bit of the behavior of this ...
1
vote
1
answer
458
views
Is there an idiomatic way of accepting particular children in React?
Suppose you have a Card component that takes content for the header, body and footer. One way to design it would be for consumers to use it like this:
<Card
header="foo"
body="...
0
votes
1
answer
76
views
Need some help understanding a couple of SCILAB idioms
Title pretty much covers it - I need some help understanding a couple of SCILAB idioms (Ultimate goal: convert some SCILAB code into Python)
Until today, I'd never heard of, let alone seen, SCILAB or ...
1
vote
0
answers
108
views
Is there a way to use concise static dispatch inside a loop in Rust?
I have a for loop where the first iteration is a special case. The only difference is the type of one variable even though they both implement the same traits. I want to know if and how I can make it ...
0
votes
3
answers
100
views
How do I make a `Copy` value inaccessible after use?
struct S {
foo: i32,
// ... other fields
}
fn f(s: S) {
// transform foo
let new_foo = biz_logic(s.foo);
// from now on, the code should read `new_foo` whenever it needs `s.foo`,
...
1
vote
1
answer
80
views
Was is the idiomatic way to define partial functions in Perl?
What is the most common/idiomatic way to define partial functions in Perl, that is functions that are not defined on all inputs. For example, in Haskell we have
safeHead :: [a] -> Maybe a
safeHead [...
9
votes
2
answers
3k
views
Is it better to return an Option<Vec<_>> or just an empty Vec<_>?
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 filtered ...
1
vote
0
answers
133
views
Is this n-ary weighted tree implementation idiomatic rust? I find it too repetitive
Context and description of the encountered problem
Currently I'm learning the Rust Programming Language and last weekend I found myself implementing a generic n-Ary weighted tree data structure whose ...
3
votes
2
answers
261
views
Is it safe to swap two integers by `a, b = b, a` in golang?
package main
import "fmt"
func main() {
a := 1
b := 2
fmt.Printf("Before Swap: %v %v\n", a, b)
a, b = b, a
fmt.Printf(" After Swap: %v %v\n", a, b)
...