Gists
Share and discover code snippetsArray shuffle function
This snippet randomly reorders array elements using the Fisher-Yates algorithm for an unbiased in-place shuffle.
unwrap and expect
Quickly panic on None or Err values with unwrap, or use expect to panic with a custom message for clearer debugging.
Responsive grid layout
This CSS snippet generates a responsive grid that automatically adjusts the number of columns based on the viewport width using auto-fit and minmax.
Read lines from file
Reads a file line by line with efficient buffering and error handling using Rust's BufReader and lines iterator.
Nice, that's a clean and idiomatic way to handle file I/O in Rust. BufReader and lines() together make it both efficient and easy to deal with errors.
Symbol to proc
This snippet converts a symbol into a proc that calls the matching method on each argument, letting you write concise enumerable operations like &:upcase.
debounce function implementation
This debounce function delays executing a callback until after a specified wait period has passed since the last invocation, preventing excessive calls from rapid events.
I had to debug a case last week where the trailing call was firing on unmount, so I ended up passing a cancel token through the closure.
flatten nested list
This snippet recursively flattens a nested list into a single-level list of all contained elements.
Flexbox centering trick
Use Flexbox's display: flex; justify-content: center; align-items: center; to perfectly center content both horizontally and vertically within a container.
Hash transform values
Transforms the values of a hash using a given block, returning a new hash with the same keys.
Nice, that's basically transformvalues in Ruby. Super handy for mapping values without touching the keys.
Array sample random
Returns a random element (or multiple random elements) from an array without repetition.
@diana49945 that's a clean approach for small arrays, but splicing each element can get slow with larger datasets.
swap two integers
This snippet efficiently swaps the values of two integer variables, often using a temporary variable or XOR to avoid extra memory.
Concurrent worker pool
Manage a fixed number of goroutines to process tasks concurrently, controlling resource usage and throughput.
Try-With-Resources
The try-with-resources statement automatically closes resources like streams and connections after use, making resource management cleaner and leak-free.
dig nested hash
This snippet uses Ruby's dig method to safely retrieve values from deeply nested hashes without raising errors for missing keys.
Deep clone object
This JavaScript snippet creates a deep clone of an object, copying all nested properties without referencing the original.
array flip example
Swap keys and values in any array with arrayflip to instantly enable reverse lookups.
nice tip, just keep in mind that arrayflip will overwrite duplicates silently, so only use it when values are unique.
type-safe deep clone
This TypeScript snippet performs a deep clone while preserving the original object's type, ensuring full type safety without relying on any.
print table contents
This snippet prints all key-value pairs in a Lua table, making it easy to inspect the table's contents during debugging.
@gregorytrujillo @gregory_trujillo that snippet works great for flat tables, but have you run into issues with nested tables where it just prints "table: 0x..." instead of recursing?
Flatten nested list
This code snippet recursively flattens any nested list into a single flat list of elements.
Database prepared statement
This PHP snippet uses a prepared statement to securely execute a database query, preventing SQL injection by separating SQL logic from user-supplied data.
Array#sample
Returns a random element (or multiple random elements if given an argument) from an array.
@stevenn totally with you on expect for debugging. I've saved hours tracing a panic back to a specific missing config field just by using expect with the variable name.