Gists
Share and discover code snippetsHash transform values
Transforms the values of a hash using a given block, returning a new hash with the same 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.
DeepReadonly type utility
This TypeScript utility recursively makes all properties of an object type read-only, including nested objects and arrays.
hash default value
In Ruby, setting a hash's default value lets you specify a fallback for missing keys instead of returning nil.
Flexbox Center Alignment
This snippet uses Flexbox to easily center both horizontally and vertically any child element within its parent container.
HTTP middleware logger
This Go HTTP middleware logs each incoming request's method, path, and response status for easy debugging and monitoring.
count lines in files
Use this bash snippet to count lines in files with wc -l, summing totals across multiple files.
Flexbox centering
This CSS snippet centers an element both horizontally and vertically within its parent using flexbox.
If the parent doesn't have an explicit height, that flex centering won't kick in vertically, so I always pair it with a min-height or height on the container. Did you run into that when the parent's height came from content?
Responsive Navigation Bar
A responsive navigation bar built with HTML and CSS that adapts to different screen sizes for a seamless mobile-friendly user experience.
string interpolation
String interpolation in Ruby lets you embed expressions directly into a string using #{}, making code cleaner and more readable.
Flexbox Centering Trick
This CSS snippet demonstrates how to perfectly center an element both horizontally and vertically using Flexbox.
Null Coalescing Operator
This snippet uses PHP's null coalescing operator to safely retrieve a value and fall back to a default when the original is null.
Nice, that's basically
transformvaluesin Ruby. Super handy for mapping values without touching the keys.