Gists
Share and discover code snippetsRAII lock guard
An RAII lock guard automatically acquires a mutex upon creation and releases it when the object goes out of scope, ensuring exception-safe and deadlock-free locking.
Option combinators
Use Rust's Option combinators like map, andthen, and unwrapor to elegantly chain operations on optional values without messy nested match statements.
Deep copy table
Creates a complete independent copy of a table, including all nested tables and values.
We hit this exact issue when migratingcomconfig system. A simple shallow copy left hidden references that caused live data corruption. Our team nustable.deepcopy() religiously in any transfer logic.
Flexbox centering
This snippet uses Flexbox to perfectly center an element both horizontally and vertically within its parent container.
Memoize function
A memoize function caches the results of expensive function calls to avoid recomputation when the same inputs occur again.
Centering with Flexbox
Easily center any element both horizontally and vertically with just a few lines of Flexbox CSS.
Strongly typed useState
"Leverage TypeScript's generics with useState to enforce strict typing on your component's state for safer code."
Semantic HTML5 skeleton
This snippet gives you a semantic HTML5 boilerplate with proper landmark elements for structure and accessibility.
Flatten list of lists
This snippet flattens a list of lists into a single list using a list comprehension.
Array partition filter
Splits an array into two arrays based on a predicate, returning both filtered and remaining elements in one call.
This is one of those utilities I end up writing in every project. I once spent an hour debugging a partition that returned mutated arrays because I forgot to copy the input first. Always spread before you partition.
@jrobertson719 that's a clean way to avoid separate filter calls for the two groups.
dictionary merge update
Merges and updates dictionaries by adding key-value pairs from another dictionary, overwriting existing keys.
Find Duplicate Values
This query uses GROUP BY and HAVING to identify duplicate values in a column for quick data quality checks.
array shuffle
This snippet randomly reorders the elements of an array in place using the Fisher-Yates algorithm.
find all symlinks
Recursively locate all symbolic links in the current directory tree using the find command.
In-place string reverse
Reverses a string by swapping characters from both ends towards the center using only the original buffer.
unwrapordefault pattern
Replace manual match statements with unwrapordefault to safely extract a value from an Option or Result, falling back to the type's default.
Table shallow copy
Creates a new table with the same top-level key-value pairs as the original without duplicating nested tables.
so does that mean you're doing a shallow copy but skipping nested recursion? what happens if the original has metatables - are they preserved or transferred to the new table?
Array shuffle function
This handy function randomizes the order of array elements using the Fisher-Yates algorithm for an unbiased shuffle.
unwraporelse closure
Returns the contained value from an Option or Result, or evaluates a closure to produce a default if it's None or Err.
Convert string to int
Use Integer.parseInt() to safely convert a string to an int, handling potential NumberFormatException.
Filter array values
Filters an array by applying a callback to each value, keeping only those that return true.
Reading environment variable
This Go snippet reads an environment variable using os.Getenv, returning its value or an empty string if not set.
yo @michelep, careful with pkill though, it can match partial names if you don't anchor the pattern with -x.