Gists
Share and discover code snippets419 gists
binary search
Finds a target value in a sorted array by repeatedly dividing the search interval in half, achieving O(log n) time complexity.
Deep clone utility
Creates a complete independent copy of nested objects and arrays, preventing unintended mutations to the original data.
dictionary merge update
Use the update method or the merge operator to combine two dictionaries, with later values overwriting earlier ones for duplicate keys.
Center anything flexbox
This snippet centers any element both vertically and horizontally using Flexbox with just three lines of CSS.
Heap allocation example
Allocate and deallocate memory on the heap with malloc and free in this handy C snippet.
RAII File Handle
Use this RAII File Handle wrapper to automatically close file handles when they go out of scope, preventing resource leaks.
Delete Duplicate Rows
This SQL snippet removes duplicate rows from a table, retaining only one instance of each duplicate set for cleaner data.
array filter callback
This snippet filters an array by passing each element through a custom callback function to include only those that return true.
Type-safe deep clone
A deep clone function that preserves TypeScript types, so you can safely copy nested objects without losing type information.
Safe navigation operator
The safe navigation operator (`&.`) calls a method on an object only if that object is not `nil`, preventing `NoMethodError` and returning `nil` otherwise.
Table deep clone
Creates a recursive copy of a Lua table, duplicating all nested tables and values to produce an independent deep clone.
Flatten nested list
This Python snippet transforms a list of arbitrarily nested lists into a single flat list by recursively extracting all elements.
HTML5 Semantic Elements
This snippet shows how to use HTML5 semantic elements like <header>, <nav>, <main>, <article>, <section>, and <footer> to structure a webpage for better readability and accessibility.
Find duplicate files
This bash snippet finds duplicate files by computing and comparing their MD5 checksums.
Optional orElseThrow
The `orElseThrow` method on an `Optional` returns the value if present, otherwise throws a custom or default exception.
Symbol to proc
Converts a symbol into a proc that calls the corresponding method on each element, enabling concise enumerable operations.
array filter callback
Filters an array by keeping only elements that pass a custom callback test, making data cleanup effortless.
Debounce function
A debounce function delays invoking a callback until after a specified wait period has elapsed since the last time it was called.
Array sample method
This Ruby method picks a random element from an array using sample, perfect for quick random selection.
RAII scope guard
An RAII scope guard ensures automatic cleanup of resources when execution leaves the current scope.
Debounce function
A debounce function limits how often a given callback can fire, ensuring it only executes after a specified delay since the last invocation.
HTML5 boilerplate template
This snippet provides a minimal, standards-compliant HTML5 template with essential meta tags, ensuring cross-browser compatibility and a solid foundation for any web project.
Snippet: generate UUID v4
Generates a random version 4 UUID in PHP using a secure source of randomness.