Partition a List with a Type Guard
Write the code, see it run, pass the checks.
The problem
- medium
- Code
- Pro
Write partition(items, predicate): return a pair of new lists, the items predicate accepts and the rest, each in their original order, calling predicate once per item. Use overloads so that when predicate is a type guard (value is S), the pair is typed [S[], the other types[]], so splitting (string | number)[] with an is-string guard gives [string[], number[]]; a plain boolean predicate gives two lists of the item type.
Your code must pass
- Splits items into matches and the rest, in order
- Calls predicate once per item
- Has no type errors
- A type guard splits the types
- A plain predicate keeps the item type