Gists
Share and discover code snippetsbash function example
A bash function example defines a reusable block of commands that can be called by name to execute those commands in sequence.
delete pointer
Deletes a dynamically allocated object to prevent memory leaks and free its resources.
Sort Map by Values
This snippet sorts a Java Map by its values, returning the entries in a List ordered by value.
merge dicts with update
Python's update method merges one dictionary into another, modifying the original in place.
The in-place mutation can cause subtle bugs when the source dictionary is reused elsewhere in the call stack.
Flexbox Center Trick
This CSS snippet centers a child element both horizontally and vertically inside its parent using Flexbox.
Check NoneType before access
This snippet prevents AttributeError by checking if a variable is None before accessing its attributes or methods.
Yeah, that pattern works well for simple cases, but if the attribute access is deep, I've found it's cleaner to use a try/except block to avoid a chain of None checks.
@rustycurmudgeon @rusty_curmudgeon doesn't that approach silently swallow AttributeErrors that might actually indicate a genuine bug in the non-None path, which a deliberate None check would surface immediately?
Check if nil
Checks whether a variable is nil in Lua to avoid errors when accessing its value.
yeah @aellis, that's one of those things that trips up people moving from other languages where nil is handled differently. i'd add that sometimes you want to check for false too, since in lua false and nil are distinct but both can cause issues in conditionals.
CSS Grid Center
A CSS Grid utility that perfectly centers any child element both horizontally and vertically within its parent container.
@aellis i've found that using place-items: center on the parent can sometimes bleed into unintended child alignment if you're not careful with nested grids.
LEFT JOIN with Filter
Filters rows from the right table in a LEFT JOIN by applying conditions in the WHERE clause to exclude unmatched records.
find duplicates by column
This SQL snippet finds rows with duplicate values in a specified column by grouping and counting occurrences.
Array map callback
PHP's arraymap applies a callback function to each element of an array, returning a new array with the transformed values.
print table contents
This snippet prints all key-value pairs in a Lua table for debugging or inspection.
Autocomplete input field
An HTML autocomplete input field suggests matching options as a user types, improving form efficiency.
@jessicaosborn just make sure your list isn't so long that autocomplete becomes a laggy mess on mobile.
Lua string interp macro
A Lua macro that substitutes variables directly into string literals for cleaner, more readable code.
Row Number Over Partition
Splits data into groups and assigns a sequential number to each row within its partition.
Generate UUID v4
PHP function to generate a random UUID v4 using cryptographically secure random bytes.
extract array unique values
This snippet removes duplicate values from an array, returning only the unique elements.
Lazy Loading Images
Lazy loading defers image loading until users scroll near them, speeding up initial page load.
Array destructuring swap
Swaps two variables without a temporary variable by using array destructuring assignment.
Ternary operator
The ternary operator provides a shorthand way to write an if-else statement in a single line, evaluating a condition and returning one of two values based on whether it's true or false.
Check if empty
This function returns true when a Lua table or string contains no elements or characters.
Yeah, empty string and empty table both being truthy in Lua can trip people up, especially since {} is often used as a default argument. That #t == 0 check won't work on a table with holes or one that's been set up as a map, though.
@samuel just to clarify, that delete only works if the pointer was allocated with new, using malloc or new[] would need free or delete[] instead.