All files / seq/operator reduce.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 2/2
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21    1x 17x 34x 34x 34x 8x   26x 26x 26x 132x 132x   26x          
import {Reduction, Seq} from 'fnxt/fnxt-types';
 
export const reduce = <E>(fn: Reduction<E>) =>
  (seq: Seq<E>): E => {
    const iter = seq[Symbol.iterator]();
    let n = iter.next();
    if (n.done) {
      throw Error();
    }
    let state = n.value;
    n = iter.next();
    while (!n.done) {
      state = fn(state, n.value);
      n = iter.next();
    }
    return state;
 
  };