Results for “Promises & iteration protocols”
Small, practical examples. Search by task or narrow things down below.
Search the stream for a message marker
Arduino / C: find consumes input through a matching marker or until timeout.
Arduino / C✓ Tested
Separate records into two lists
Python: Build two explicit partitions so callers can use both matching and rejected values.
Python✓ Tested
Split a list into fixed-size batches
Python: Slice a sequence at regular offsets; the final batch can be smaller.
Python✓ Tested
Take a limited number of iterator values
Python: Use islice to consume only the requested prefix of a potentially unbounded iterator.
Python✓ Tested
Validate a complete numeric serial command
Arduino / C: Check for digits, an end pointer, and an application-specific range.
Arduino / C✓ Tested
Visit adjacent pairs
Python: Use itertools.pairwise to process neighboring items without indexing.
Python✓ Tested
Await several independent results
Preserve input order while awaiting multiple promises.
Promise.all✓ Tested
Count set bits in an unsigned integer
C: Clearing the lowest set bit on each iteration counts only one iteration per set bit.
C✓ Tested
Count Unicode code points in a string
JavaScript: String iteration combines surrogate pairs, unlike the UTF-16 length property.
JavaScript✓ Tested
Count Unicode code points in a string
TypeScript: String iteration combines surrogate pairs, unlike the UTF-16 length property.
TypeScript✓ Tested
List regular files recursively in sorted order
C++: Collect relative paths before sorting because directory iteration order is unspecified.
C++✓ Tested
Preserve map insertion order
Java: Use LinkedHashMap when iteration should follow the order keys were first added.
Java✓ Tested