Gists
Share and discover code snippetsBind socket to port
This snippet binds a socket to a specified port using the bind function and a sockaddrin structure.
Useful pipe tricks
This snippet demonstrates useful pipe tricks in Bash for chaining commands efficiently.
Get nested value safely
This TypeScript snippet safely retrieves a nested value from an object without throwing an error if any intermediate property is undefined.
Reverse a string
This Python snippet reverses a string in place using slicing with a step of negative one.
constexpr auto factorial
Computes the factorial of a compile-time constant using constexpr for zero overhead.
Merge dicts safely
This snippet merges two dictionaries, preserving existing keys and avoiding overwrites, using {dict1, dict2} with dict2 taking priority.
That unpacking trick is clean but only works in Python 3.5+. I once spent an hour debugging a production script that silently dropped keys because someone used {a, b} on a Python 3.4 runtime.
Find duplicates using GROUP BY
Groups rows by a column and filters for those with more than one occurrence to find duplicate values.
Flexbox Centering Trick
This CSS snippet centers any child element both vertically and horizontally within its parent using flexbox.
Generate date series
Generate a sequence of dates within a specified range for use in reporting or data analysis.
Parse hex string to bytes
Converts a hexadecimal string into a byte array for efficient data handling in C.
Calculate array length
This snippet determines the number of elements in a C array by dividing the total array size by the size of a single element.
Default From/Into impls
Default From and Into implementations in Rust allow automatic bidirectional conversions between compatible types, reducing boilerplate code.
Coalesce Null Default
Returns the first non-null value from a list of expressions, using a default if all are null.
Ternary null coalescing
Assigns a default value when a variable is null, using PHP's shorter null coalescing operator.
The null coalescing operator (??) works well for undefined keys too, not just null values. In your example, $username ?? 'guest' avoids an undefined index notice if $username isn't set, which the ternary operator (isset($username) ? $username : 'guest') also handles but with more syntax. Just watch for nested coalescing, as it evaluates left to right and can mask deeper null issues in chained calls like $a ?? $b ?? $c.
Strict Type Predicate
A TypeScript utility that creates a type guard function to verify if a value matches a specific type at runtime, ensuring type safety when working with uncertain data.
concurrent HTTP requests
A Go code snippet that fires off multiple HTTP requests in parallel using goroutines and a sync.WaitGroup to handle them concurrently.
Sort array descending
This Rust code snippet sorts an array in descending order using the sortby method with a comparison closure.
Array destructuring swap
Swaps two variables without a temporary variable using array destructuring.
Center with Flexbox
This CSS snippet centers any child element both horizontally and vertically inside a flex container.
Array Shuffle Fisher Yates
Shuffles an array in place using the Fisher-Yates algorithm for uniform random distribution.
Array destructuring swap
Swaps two values without a temporary variable by using array destructuring assignment.
Hash String Comparison
Compares two strings by their hash values to check for equality efficiently.
Hashing for equality works great until you hit a collision, and then you're stuck debugging a false positive that looks like a bug in your comparison logic.
Iterate with enumerate
Enumerate in Rust lets you iterate over a collection while automatically tracking the index of each element.
imo this is strictly C