本文整理匯總了TypeScript中rxjs/operators.reduce函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript reduce函數的具體用法?TypeScript reduce怎麽用?TypeScript reduce使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了reduce函數的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: higherOrderReduce
export function reduce<T, R>(this: Observable<T>, accumulator: (acc: R, value: T, index?: number) => R, seed?: R): Observable<R> {
// providing a seed of `undefined` *should* be valid and trigger
// hasSeed! so don't use `seed !== undefined` checks!
// For this reason, we have to check it here at the original call site
// otherwise inside Operator/Subscriber we won't know if `undefined`
// means they didn't provide anything or if they literally provided `undefined`
if (arguments.length >= 2) {
return higherOrderReduce(accumulator, seed)(this);
}
return higherOrderReduce(accumulator)(this);
}
示例2: filterAny
function filterAny(values, getter) {
return of(...values).pipe(
map(x => getter(x)),
reduce((a, b) => a.union(b), new TypedFastBitSet()),
map(mask => results => results.intersection(mask))
);
}
示例3: type
type('should accept R typed reducers', () => {
let a: Observable<{ a: number; b: string }>;
a.pipe(reduce<{ a?: number; b?: string }>((acc, value) => {
value.a = acc.a;
value.b = acc.b;
return acc;
}, {}));
});
示例4: getTrainingSet
async function getTrainingSet() {
const peopleArray = await of(...range(5, 10), 2)
.pipe(map(x => `https://swapi.co/api/people/?format=json&page=${x}`))
.pipe(flatMap(x => Axios.get(x).catch(console.error)))
.pipe(map((x: AxiosResponse) => x.data))
.pipe(reduce((acc: any[], x: any) => [x, ...acc], []))
.toPromise();
console.log(JSON.stringify(peopleArray, null, 2));
}
示例5: type
type('should accept T typed reducers when T is an array', () => {
let a: Observable<number[]>;
const reduced = a.pipe(reduce((acc, value) => {
return acc.concat(value);
}, []));
reduced.subscribe(rs => {
rs[0].toExponential();
});
});
示例6: it
it('should reduce with index with seed', (done: MochaDone) => {
const idx = [0, 1, 2, 3, 4, 5];
range(0, 6).pipe(reduce((acc, value, index) => {
expect(idx.shift()).to.equal(index);
return value;
}, -1)).subscribe(null, null, () => {
expect(idx).to.be.empty;
done();
});
});
示例7: merge
proc.on('close', (code: number) => {
noClose = true;
let pipesClosed = merge(stdoutCompleted!, stderrCompleted!)
.pipe(reduce((acc) => acc, true));
if (code === 0) {
pipesClosed.subscribe(() => subj.complete());
} else {
pipesClosed.subscribe(() => subj.error(new Error(`Failed with exit code: ${code}`)));
}
});