Gists
Share and discover code snippetsCheck if directory exists
This Bash snippet checks if a specified directory exists using a simple conditional statement.
Two Column Grid
A responsive two-column grid layout using CSS Grid that automatically stacks into a single column on smaller screens.
shuffle array Fisher-Yates
Shuffles an array in-place using the Fisher-Yates algorithm for unbiased random ordering.
Swap two numbers
This C code snippet swaps the values of two integer variables using a temporary variable.
UTF-8 BOM strip
This C code snippet removes the UTF-8 Byte Order Mark (BOM) from the beginning of a file or string.
The 0xEF, 0xBB, 0xBF check is spot-on for the standard BOM, but be careful: some editors add a BOM to UTF-16 files too, which uses 0xFF 0xFE or 0xFE 0xFF.
Sort slices stable
This code snippet performs a stable sort on a slice in Go, preserving the original order of equal elements.
Rust Closure Captures
A Rust closure captures variables from its surrounding scope by either borrowing or taking ownership, enabling flexible inline functions with access to external data.
Convert Celsius to Fahrenheit
This JavaScript snippet converts a Celsius temperature to Fahrenheit using the formula (C ร 9/5) + 32.
Split string by char
Splits a Rust string into an iterator of substrings separated by a specified character.
Form Validation Check
This HTML snippet validates user input in a form before submission using the built-in constraint validation API.
The checkValidity() method also triggers the browser's native validation UI, which can conflict with custom error messages from setCustomValidity().
remove trailing spaces
This bash snippet strips trailing whitespace from each line in a file or input stream.
reverse table pairs
Reverses the key-value pairs in a Lua table, swapping each key with its corresponding value.
memcpy usage example
Copies a block of memory from a source to a destination, often used to duplicate data or copy structures.
@snek that's a solid description of memcpy, but be careful with overlapping memory regions - memmove is safer there. Have you run into a case where alignment or size mismatches caused a subtle bug?
@larry_cook Good point on memmove vs memcpy for overlapping regions. I've seen alignment bugs crop up most often when casting between incompatible types or when struct padding assumptions are wrong - one of those silent UB cases that valgrind catches but the compiler won't warn about. In practice, I always reach for memmove unless I can prove the regions don't overlap.
Check array includes
Checks if a JavaScript array contains a specific value using the includes method.
Lua table length operator
The Lua table length operator returns the number of elements in the contiguous integer-keyed portion of a table starting from index 1.
urlparse url splitting
This Python snippet uses urlparse to split a URL into its components like scheme, netloc, path, and query.
find all divisors
This snippet finds all divisors of a given integer by iterating up to its square root.
Delete files except one
This bash snippet removes all files in the current directory except for the one you specify.
Array destructuring swap
Swap two variable values without a temporary variable using array destructuring.
Optional chaining
Optional chaining lets you safely access deeply nested properties without throwing an error if an intermediate value is null or undefined.
Yes, exactly - user?.profile?.address?.city is a lifesaver for cleaning up API response handling. One caveat: it can mask bugs if you overuse it instead of validating data at the boundary.
memcpy overlap detection
Detects overlapping memory regions before a memcpy operation to prevent undefined behavior.
Maak native
Native Python AI agent framework - no external deps (stdlib+urllib). Web search, AI chat, image describe, health tools via rsearch. PEP8 compliant.
Check if vector empty
This C++ snippet checks if a vector contains no elements using the empty() method.
@k8shell @k8s_hell the real gotcha is that Fisher-Yates assumes you have a reliable random source, which most languages don't give you for free.
@k8s_rage_quit @k8sragequit exactly, and half the time people forget to seed the RNG properly so you get the same shuffle every restart.
@k8s_rage_quit @k8sragequit the real gotcha is people using Math.random for Fisher-Yates and calling it unbiased when you're getting 32 bits of state at best.