10
votes
Fortnightly Programming Q&A Thread
General Programming Q&A thread! Ask any questions about programming, answer the questions of other users, or post suggestions for future threads.
Don't forget to format your code using the triple backticks or tildes:
Here is my schema:
```sql
CREATE TABLE article_to_warehouse (
article_id INTEGER
, warehouse_id INTEGER
)
;
```
How do I add a `UNIQUE` constraint?
I'm curious if anyone here has ever implemented Perlin noise? I've looked at it over the years and every time I sat down and tried to read a description of how it works, the descriptions were just so bad that they made no sense. That seems to have changed recently and I found some articles that made sense to me, so I did a simple implementation for fun. One thing that struck me is that I see a lot of different variants of the output. What I'm generating looks a lot like what I see on the Wikipedia article for Perlin noise.
However, I recently went through the various "Ray Tracing In a Weekend" tutorials. In the 2nd book, the author introduces Perlin noise. If I use my own code to generate the base level without adding in any additional octaves, it looks like image 11 in section 5.5. If I add in successive octaves, however, I do not get similar results as in image 12 in section 5.6. It's not clear to me why. One thing I notice that I'm doing differently is that I'm not scaling and adding the same random vectors for each octave, but am generating new random vectors at each octave. Would that be why I see the difference? If so, is one way correct and the other not? (That is, if the vectors are different, am I technically not generating Perlin noise, but something else?)
The other thing I noticed that's different is that the code from the ray tracing tutorial is implemented as 2D Perlin noise, whereas I'm generating straight 2D noise. It's not clear to me whether that makes a difference or if a slice through 3D noise should look like 2D noise.
Well, I finally figured it out! The issue is that all the calculations for Perlin noise are done in floating point in roughly the range -1..+1. Generally you want to take the result and either use it for a lookup in a table or output it as a grayscale value, so you convert it to an 8-bit integer. This can be done any number of ways, but I was doing something like this:
result = (uint8_t)(255.0 * (1.0 + noise) / 2.0);
Looking back at the code from the book, the author chose to instead just take the absolute value, like this:
result = (uint8_t)(255.0 * fabs(noise));
Once I made a similar change in my code, I started getting the expected results. It's not clear to me that's "more correct", but at least now I know why it's different!
I just read through that section — my intuition is that, when you add more octaves, the variance shrinks and the noise crosses 0 more often. So the marbling becomes a more distinctive feature with an absolute value because you can see the contour lines. Also, depending on the output color space, numbers around 0 might be visually stronger.
So, I'm trying to futz around with a Sapper/Svelte web app connected to a Fauna database and I'm realizing I'm a little lost. I get the front-end principles of Svelte the back-end principles of Fauna on their own, but I find myself scratching my head as to how to get them connected*. Mostly, that is, how to structure the app and where to place my database connection logic. I've been looking at this starter Svelte project for some ideas, though they connect to MongoDB.
I guess my troubles are more generally described as "how do I full-stack?" Each of these front-end/back-end technologies have nice lil tutorials, but I'd like to see a tutorial that ties everything together and explains how to structure an application appropriately.
Any recommendations where I can find such a tutorial? I've found Full Stack Open, which looks promising... but I'm very open to other suggestions.
* : I was able to connect to a Fauna DB instance and run their example describing how to add to a collection, though that logic was placed in my
server.js
file... which meant every time the server restarted that same database query was re-run, continually adding the same item to the collection. 🤦I built BadukClub using Svelte, Sapper, the Google Maps API, and Hasura, which is a GraphQL endpoint for Postgres. This is what I did:
The DB File
I created a single file where I took care of initializing the connections and providing functions to interact with them. In your case, I would try something like:
Then, from your
server.js
, add this:Using the DB File
Now that you have a DB file, you can call methods on it. For example, if you wanted to list things out on a page of your sapper app, you'd do something like this:
src/routes/[collectionName].svelte
Similarly, if you want to create new documents, I would have created that function in
db.js
as well, then put it in the svelte component I needed and made it accessible via a button, i.e.Ultimately, I'd recommend trying to do as much as you can in the Svelte REPL so you get quick feedback. I set up a REPL instance that imports the Fauna library and initializes a client and the query object. Good luck!
Oh, and on the tutorial front...honestly, I don't see much value in tutorials as long as I have a bit of code to get started. The standards and frameworks for full-stack web dev change so fast that it's very difficult to find an up-to-date tutorial. I find my time better spent playing around with the libraries myself, I end up learning more that way.
Hey! I'm a go player and have definitely checked out your site a few times. Wanted to say thanks for building it and great work! It's super clean and well put-together.
Hey, thanks! Very helpful! And cool website!
Yea, the code itself is fairly straightforward, but the organization of the code and where to inject what makes me a lil 🥴. I agree, though. Having a basic codebase to read from and draw examples from helps immensely.
I ended up buying Pluralsight this year as a way to try and cover any gaps in my coding knowledge when I'm bored of Netflix and gaming.
So far, it was pretty neat - at least with the Ruby and RoR tutorials. Those were incredibly short and sweet.
Now I've tried a Swift course (as one of my weaknesses was Swift and iOS, and I want to be able to tackle at least one of them and do a hello world on iOS in pure native code rather than relying on React Native) and honestly it feels like my brain is going to escape my head. Not because it is hard, but because the topics covered in the nearly 2 hours I've done so far have been incredibly rudimentary. So much so that I've given up today and went to the https://docs.swift.org and I'm just learning the bits through the language guide. Much faster and efficient.
So I guess my question is does anyone use any other aids like Pluralsight to keep on top of their learning, or how do you keep on top of languages and/or gaps in the knowledge?