StackOverflow.com

VN:F [1.9.22_1171]
Rating: 9.4/10 (10 votes cast)

Random snippets of all sorts of code, mixed with a selection of help and advice.

Downside of NOT using quotes for keys of associative array?

14 June 2025 @ 9:09 pm

Every example I have found tends to go aa["key"]="value" and even with constants, e.g.: declare -rA aa=( ["key1"]="value1" ["key2"]="value2" ) What is the purpose of this, i.e. does it guard against something I am not foreseeing with literals? The purpose is obvious to me when something is about to be expanded within the quotes, but with constants the code then starts to look less, not more readable. I checked the two best resources I can think of, the reference and Greg's wiki. It's not explicitly covered anywhere, the former conflates it with regular arrays where it was a non-issue.

Why is n mod n = n, instead of 0 in for loop, but correctly 0 in while loop?

14 June 2025 @ 9:07 pm

I have constructed a for loop that I though would given me a "circular" index that starts at some number (n) and cycles around again to that number through the series 0 to n, that is "mod n". Here is the code: n = 5 for ( int i = 0; i < n; i++ ) { for (int j = i + 1; j != i; j = ( (j + 1) % n ) ) { printf ( "i = %i, j = %i\n", i, j ); } printf("\n"); } Here is the output: i = 0, j = 1 i = 0, j = 2 i = 0, j = 3 i = 0, j = 4 i = 1, j = 2 i = 1, j = 3 i = 1, j = 4 i = 1, j = 0 i = 2, j = 3 i = 2, j = 4 i = 2, j = 0 i = 2, j = 1 i mod n = 4 i = 3, j = 4 i = 3, j = 0 i = 3, j = 1 i = 3, j = 2 **i = 4, j = 5** i = 4, j = 1 i = 4, j = 2 i = 4, j = 3 Note that the first j of the last i is 5, not 0. I substituted the inner for loop with what I think is the equivalent while loop: n

Why does ResteasyReactiveJaxbProcessor reject simple collections when upgrading from Quarkus 2 to Quarkus 3 using quarkus-rest?

14 June 2025 @ 9:03 pm

I want to upgrade a quarkus 2 (2.16.12) application to quarkus 3. (3.20.1). As part of that I replaced maven dependencies to quarkus-resteasy with the recommended quarkus-rest. Unfortunately the upgraded application does not start because of a DeploymentException from ResteasyReactiveJaxbProcessor. It seems all return values and parameters are checked and collections without XmlRootElement annotation will trigger the exception. This is inconvenient, because we have a number of APIs that use for example List as return value or as method parameter. public List<String> getServiceNames(List<String> systems) { Now I could define List-Implementations to make the return parameters quarkus-rest-jaxb compatible but that seems like a fruitless exercise and it can‘t be applied to the incoming method parameters. Rewriting all APIs and implementations with xmlrootelement-annotated wrapper objects seems a bit over the top, so I was hoping for a configuration approach, i

How to show content in components if user is authenticated?

14 June 2025 @ 8:58 pm

I've been following along with this Next tutorial, and added authentication. https://nextjs.org/learn/dashboard-app/adding-authentication It does work for routing, as I don't get access to the dasboard page, if I'm not logged in. I can't seem to understand how I for a specific component, can display details about the user (say email). The tutorial doesn't have any examples of it. ChatGpt and other tutorials, seems to point out that I am missing the route.ts file for app/api. That is not specified in the tutorial. I've been fiddling along with various route.ts configurations, but nothing seems to work as expected, and most examples are not of the latest NextAuth versions. Here's one example of route.ts which doesn't work import NextAuth from 'next-auth'; import { authConfig } from '../../../../../auth.config'; const handler = NextAuth(authCo

PyTorch Sharing CUDA tensors

14 June 2025 @ 8:56 pm

I have a question considering sharing gpu-tensors between processes using the torch.multiprocessing module. Here is a minimal example: import torch import torch.multiprocessing as mp from torch.nn import Embedding import time device = 'cuda' if torch.cuda.is_available() else 'cpu' def worker(rank, tensor): if rank == 0: tensor.weight.data[rank,rank] = 99 #tensor is on gpu if rank == 1: time.sleep(1) print("worker", rank, tensor.weight) #worker1 sees modified shared memory from worker0 if __name__ == '__main__': tensor = Embedding(2, 5, sparse=False) #init with random values tensor = tensor.to(device) #send to gpu print("main", tensor.weight) processes = [] for rank in range(2): # number of workers p = mp.Process(target=worker, args=(rank, tensor)) p.start() processes.append(p) for p in processes: p.join() Now what happens is, the main proc

How to autocomplete brackets in Vim only in some cases

14 June 2025 @ 8:51 pm

For auto-expanding function body I have in my .vimrc file :inoremap { {<CR>}<Esc>ko (which produces) fn main { .... } This works fine most of the time, but problem arises when curly brackets are used in different context, e. g. inside a string println!("hello {name}") For this case I would need to to have something like this in my .vimrc :inoremap { {}<Esc>i How can I make Vim distinguish between these cases and use appropriate command? I have this problem mainly when working with Rust.

Creating a tree of XSD elements without duplicates

14 June 2025 @ 8:50 pm

I'm building an XSD viewer using XSLT. The viewer should list all elements hierarchically, and I am currently stuck on deduplicating sub-elements which appear more than once in their parent (e.g. due to presence of choice or sequence clauses). Here's my current bare bones transformation: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:map="http://www.w3.org/2005/xpath-functions/map" xmlns:array="http://www.w3.org/2005/xpath-functions/array" exclude-result-prefixes="#all" > <xsl:output method="text" media-type="text/plain" omit-xml-declaration="yes"/> <xsl:template match="/"> <xsl:apply-templates select="xs:sc

htaccess mod-rewrite automatically

14 June 2025 @ 8:49 pm

I have this htaccess rewrite rule that's meant to rewrite /index.php?fuse=admin&view=snapin&controller=snapins&plugin=page&v=termsofservice to /termsofservice.html but it's not working correctly. /termsofservice.html link will work if you go to it, but I want it to redirect automatically to it but it's not doing it, I manually have to enter the shorter URL for it to work. Can anyone help. The code that I have is below. RewriteEngine on RewriteRule ^([^/]+)\.html$ /index.php?fuse=admin&view=snapin&controller=snapins&plugin=page&v=$1 [L]

React Native library for adding and editing shapes

14 June 2025 @ 8:45 pm

I am designing an app that will have a "canvas" and when the user taps the canvas a circle should be drawn. The shapes should be selectable after drawing, with a gesture like a long press. This applies to the most recent shape added, as well as any others previously added. I've tried Skia and it seems that the API is pretty clunky for doing things like this dynamically, and is much stronger using the declarative API. Also, it seems that interacting with shapes after they are drawn is difficult as well. Can anyone offer help with Skia, or another library that might also do what I am looking for? Thanks!

Updating view when SwiftData model changes

14 June 2025 @ 8:41 pm

Consider the following SwiftData setup: @Model class Team { @Attribute(.unique) var id: UUID var name: String @Relationship var players: [Player] } @Model class Player { @Attribute(.unique) var id: UUID var name: String var number: Int? @Relationship(inverse: \Team.players) var team: Team? } In my view I have something like this: @Query(sort: \Team.name) var teams: [Team] var team: Team? { teams.first(where: { $0.id == viewModel.teamId }) } This works fine to display data: if let team { Text(team.name) } But I also have code that displays player data: if let team { ForEach(team.players) { player in Text(\(player.number)) } } But the view does not update when I change one of the player's numbers. Here's an example of change in the player (in the view model): l