Skip to content

Commit

Permalink
Use a Symbol in favor of a special string constant
Browse files Browse the repository at this point in the history
  • Loading branch information
nvie committed Apr 2, 2018
1 parent d6f631f commit 1d641d3
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/itertools.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { flatten } from './more-itertools';
import type { Maybe, Predicate, Primitive } from './types';
import { primitiveIdentity } from './utils';

const SENTINEL = Symbol();

function composeAnd(f1: number => boolean, f2: number => boolean): number => boolean {
return (n: number) => f1(n) && f2(n);
}
Expand Down Expand Up @@ -105,11 +107,11 @@ export function* groupby<T>(
iterable: Iterable<T>,
keyFn: T => Primitive = primitiveIdentity
): Iterable<[Primitive, Iterable<T>]> {
const SENTINEL = '__groupby_SENTINEL__';
const it = iter(iterable);

let currentValue;
let currentKey = SENTINEL;
// $FlowFixMe - deliberate use of the SENTINEL symbol
let currentKey: Primitive = SENTINEL;
let targetKey = currentKey;

const grouper = function* grouper(tgtKey) {
Expand All @@ -127,6 +129,7 @@ export function* groupby<T>(
while (currentKey === targetKey) {
const nextVal = it.next();
if (nextVal.done) {
// $FlowFixMe - deliberate use of the SENTINEL symbol
currentKey = SENTINEL;
return;
}
Expand Down

0 comments on commit 1d641d3

Please sign in to comment.