How to find the standard libraryWhen I started this, I knew that some of the functions I wanted would already exist, but I couldn't figure out how to find them.
More on Generator and friends
Warning: GeneratorOf<T> is a single type
Collection and friends
- LogicValue: getLogicValue (allows your type to be used in boolean contexts like if)
- Equatable: == (but not !=)
- Hashable: Equatable plus hashValue
- Comparable: Equatable plus <=, >, >= (but not <)
- IntegerArithmetic: Comparable plus +, -, *, /, %, toIntMax
- BitwiseOperations: &, |, ^, ~, allZeros
- SignedNumber: unary -
- AbsoluteValuable: SignedNumber plus abs
- FloatingPointNumber: nan/inf-related methods
- Integer: RandomAccessIndex
- SignedInteger: Integer
- UnsignedInteger: Integer
What itertools-like functionality is already there?
- enumerate: Always takes a Sequence, returns a Generator, of a custom type.
- filter: Takes a Collection and returns a forward-only Collection, or takes a Sequence and returns a (reusable) Sequence, both custom types (with View stuck in their names), along with a third custom type for a Generator.
- map: Identical to filter, except with a Collection it returns a similarly-indexable Collection. (Another overload also takes an Optional and returns an Optional.)
- PermutationGenerator: A single Generator struct without a wrapper function, which takes a Collection and a Sequence, but is so far from being understandable by a human or a compiler that I have no idea what they mean.
- Repeat: A single random-access-indexable Collection struct without a wrapper function (meaning of course that it can't handle infinite sequence, and that end is not optional, making it a lot less useful).
- reverse: Takes a Collection, returns a lazy bidirectional-only Collection of custom type.
- Zip2: A Sequence struct with a separate Generator struct (which isn't a Sequence), which takes two Sequences and zips them into a Sequence of pairs, ending as soon as the shorter Sequence ends. (If you happen to have a Generator that isn't a Sequence—which shouldn't be possible, but this very function shows that it can happen—you can use the Generator, named ZipGenerator2 rather than Zip2Generator, directly to zip together two Generators into another Generator.) (Oddly, the variable names here refer to "Streams" rather than "Generators". I'm guessing after a dev meeting where the Python fans and C++ fans came to blows over what "iterator" is supposed to mean, someone suggested "stream" for the Python thing and "index" for the C++ thing and that stuck for a while, and then there was a Great Renaming, possibly to punish the leader of the Pythonista faction for some transgression like forgetting to restart the coffee, and someone missed this function.)
- join: Takes a Sequence for the parts to be joined. And a Collection for the join-string (which, obviously, doesn't have to be a string) and returns a Collection of the same type.
- maxElement, minElement: Take a Sequence.
- reduce: Takes a Sequence.
- count: Just returns the size of a Range of RandomAccessIndexes, which hardly seems worth a builtin function.
- countElements: Sort of like ilen, in that it returns the length of anything, even if it has to count the whole thing, except for Collections, not Sequences. (And I'm not sure how this works; it returns T.IndexType.DistanceType without putting any requirements on T that would make it have such a thing. The compiler won't let me write code like that.)
- dropFirst: Like but_first, or islice(1), except that it only works on Sliceable types. So it's really just x[1..x.count]. I suppose the fact that Swift doesn't have open-ended slices (which sucks) makes this not completely useless, but only just barely.
- dropLast: As with dropFirst, only works on Sliceable types, meaning it's just a minor convenience.
- sort: Only works on arrays (not even randomly-accessible Collections?). Also, both mutates and returns the array, just to confuse people coming from other languages. There's also insertionSort for bidirectional Collections and quickSort for SignedInteger-indexed (?) Collections, with different APIs.
- split: Takes a Sliceable, returns an Array.