2018-07-25 08:38:10 +03:00
|
|
|
// @flow
|
|
|
|
|
2017-11-18 12:38:34 +03:00
|
|
|
import * as assert from 'assert';
|
|
|
|
|
2017-11-21 15:28:59 +03:00
|
|
|
// I so much dream about the user guards...
|
|
|
|
// @see flow#112.
|
|
|
|
export const invariant = assert.ok;
|
|
|
|
|
2017-11-28 17:42:19 +03:00
|
|
|
export function last<T>(list: T[]): T {
|
|
|
|
invariant(list.length > 0);
|
|
|
|
|
|
|
|
return list[list.length - 1];
|
2017-11-21 15:28:59 +03:00
|
|
|
}
|
2017-12-16 17:03:08 +03:00
|
|
|
|
|
|
|
export function collect<T>(iter: Iterable<[string, T]>): {[string]: T} {
|
|
|
|
const result = {};
|
|
|
|
|
|
|
|
for (const [key, value] of iter) {
|
|
|
|
result[key] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2018-01-13 20:51:37 +03:00
|
|
|
|
|
|
|
export function partition<T>(iter: Iterable<T>, pred: T => boolean): [T[], T[]] {
|
|
|
|
const [left, right] = [[], []];
|
|
|
|
|
|
|
|
for (const item of iter) {
|
|
|
|
(pred(item) ? left : right).push(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [left, right];
|
|
|
|
}
|