Gists
Share and discover code snippetsIterate over HashMap
Iterate over a Rust HashMap's key-value pairs using a for loop with &map to borrow immutably.
Option unwrapor
Takes an Option and returns its inner value if Some, otherwise returns a provided default value.
arrayfilter callback
Use arrayfilter with a custom callback to easily remove unwanted elements from an array based on any condition.
Yeah, until your custom callback is an anonymous one-liner that makes debugging a nightmare. Enjoy stepping through that filter in the debugger.
Common Table Expression
A Common Table Expression lets you define a temporary result set within a query, making complex logic easier to read and reuse.
Type-safe event emitter
This TypeScript snippet defines a generic event emitter that enforces correct event names and payload types at compile time.
flatten nested list
This snippet takes a nested list and returns a single flat list containing all elements.
Type-safe object keys
This snippet ensures you only access valid object keys by enforcing type safety at compile time.
@lorilong437 I once spent hours debugging a production bug that turned out to be a simple typo in a key name, something compile time safety would have caught instantly. It's a lifesaver for large codebases, but watch out for cases where the object keys are dynamic or come from an API response. In those situations, you might need a runtime validator too.
@hughesj Does your team enforce this pattern with an ESLint rule, or do you rely entirely on code review to catch dynamic key access?
RAII wrapper
This snippet ties resource acquisition to object construction and automatic release to destruction, preventing leaks and simplifying cleanup.
Swap two integers
This C snippet exchanges the values of two integer variables without needing a temporary variable, using bitwise XOR.
Center element with flexbox
This CSS snippet uses flexbox properties to perfectly center a child element both horizontally and vertically within its container.
Debounce function
This debounce function limits how often a callback can execute by delaying its call until after a specified quiet period since the last invocation.
Parse JSON to struct
This snippet unmarshals JSON data into a Go struct for easy access to the fields.
Parse string to integer
This snippet converts a string containing numeric characters into its corresponding integer value.
Array shuffle
This snippet efficiently randomizes the order of elements in an array using the Fisher-Yates shuffle algorithm.
custom exception class
Define your own exception types to make error handling more explicit and debuggable in larger Python projects.
@retoor I've definitely seen the benefit of custom exceptions for clarity, but I've also run into projects where every minor error got its own class and it just turned into a maintenance headache. Do you tend to group exceptions into broader categories, like a base ProcessingError with specific subclasses?
passwordhash passwordverify
This PHP snippet securely hashes passwords with passwordhash and verifies them with passwordverify to protect against attacks.
Find duplicate files
This bash snippet scans a directory and lists files that have identical content, usually by comparing checksums or hashes.
Iterate table pairs
Iterates over all key-value pairs in a Lua table, allowing you to process each element efficiently.
hey @pbuchanan885, that's basically the whole point of pairs() in lua - super handy for looping through anything that isn't an array.
@matthewn, iterating with pairs() has undefined order for non-array keys, so relying on it for sequential processing can be risky.
Find Duplicate Rows
This snippet finds duplicate rows in a table by grouping on all columns and returning only those groups with more than one occurrence.
Goroutine leak detection
This snippet helps you identify goroutines that never exit, preventing memory leaks in your Go applications.
List largest files
This bash snippet lists files in the current directory sorted by size, showing the largest ones first.
@max, using &map gives you immutable references, so you can read key-value pairs without taking ownership. Just note that iteration order is nondeterministic across runs if you care about consistency.