swift-atoms/swift-standard-library-extensions
Use this package when code needs a convenience the Swift standard library leaves out, and should get it without importing Foundation: result-builder initializers for the standard containers (Swift.Array.Builder, Swift.Dictionary.Builder, Swift.Set.Builder, `Swift.String.Bui
Laws
- Builders evaluate every component and never short-circuit,
Swift.Bool.Builder.AllandSwift.Optional.Builderincluded. Swift.Dictionary.Builder: when a key repeats, the later value wins, within one expression and across components.Swift.Set.Buildertakes the union of its components.Swift.String.BuilderandSwift.Substring.Builderjoin components with"\n". An empty string or a skippedifafter the first non-empty component adds an empty line. Empty components before it are dropped.Swift.Optional.Buildergives the first non-nilcomponent.Swift.Result.Builder.Firstgives the first success, or the last failure if every component fails. A block whose only components are skippedifs givesnil.Swift.Result.Builder.Allkeeps successes in source order, and the first failure wins.- An empty block or a skipped
ifcontributes the identity:trueforallandnone,falseforany,0forcount.oneistrueexactly when one component istrue. Swift.ClosedRange.Builderturns a bare boundxintox...x.element(at:)andelements(in:)returnnilfor any index or range outside the collection and never trap.elements(in:)checks against the receiver's own indices, so a slice rejects indices of its base that lie outside the slice.chunks(of:)returns slices that share the receiver's indices and storage. Every chunk is full except possibly the last. A size of zero or less gives no chunks.chunked(into:)copies the chunks into arrays.move(offsets:to:)sorts and de-duplicates the offsets first. The moved elements keep their relative order and land in front of the element that was atdestination. A destination inside the moved block leaves the collection unchanged. A destination outsidestartIndex...endIndextraps.range(of:)compares UTF-8 bytes. It is case-sensitive, does not apply canonical equivalence (a precomposedédoes not matchefollowed by U+0301), and returns the first match. An empty needle matches the empty range atstartIndex.Swift.Rangeoverlap(_:)andclamped(to:)returnnilwhen the intersection is empty, so ranges that only touch givenil. OnSwift.ClosedRange, ranges that touch give a single point.split(at:)returnsnilunless the point lies strictly between the bounds.power(_:)returns0for a negative exponent, not the reciprocal.rounded(to:)returns the value unchanged for a negative number of places and otherwise rounds half away from zero.max(count:)returns the largest elements in descending order, andmin(count:)the smallest in ascending order. Both return at mostcountelements.mean()returnsnilfor an empty sequence. On integers it divides with truncation, and it traps when the sum or the element count does not fit the element type.uniqued()keeps the first occurrence of each element, in order.- If two values are equal,
inverted()keeps one of their keys, chosen by dictionary iteration order. The same goes formapKeys(:)andcompactMapKeys(:)when two keys map to the same new key. replace(:with:)inserts the replacement only if the member was present.toggle(:)removes a present member and inserts an absent one.- Setting
isPresenttofalseclears the optional. Setting it totruedoes nothing. lowercasingLeadingUppercaselowercases a leading run of capitals. When the run has two or more capitals and more text follows it, its last capital stays, because that capital starts the next word (URLRequestbecomesurlRequest). A single leading capital is lowercased (Requestbecomesrequest).Swift.Set<String>.Swift.escape(_:)adds backticks only to words inSwift.Set<String>.Swift.keywords.Swift.Actor.runwith a synchronous body runs it as one isolated step, so no other work on the actor runs in between. The async overload can interleave at everyawait.eraseToThrowingStream()runs the iteration in a new unstructuredTask. When the stream terminates, that task is cancelled. If the loop sees cancellation between elements, it finishes the stream without throwing. Any error the base sequence throws is passed on to the stream.
Package Metadata
Repository: swift-atoms/swift-standard-library-extensions
Default branch: main
README: README.md