Skip to content

Commit

Permalink
refactor: iterator as first param
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Apr 10, 2020
1 parent 03fbd5d commit 6425306
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/operators/operators.ts
Expand Up @@ -56,7 +56,7 @@ export function combine<T, U, V>(
* apply a mapping function to an Iterable.
*/
export function map<T, U>(fnMap: (t: T) => U): ChainFunction<T, U> {
return (i: IterableLike<T>) => op.map(fnMap, i);
return (i: IterableLike<T>) => op.map(i, fnMap);
}

export function scan<T>(fnReduce: (prevValue: T, curValue: T, curIndex: number) => T): ChainFunction<T>;
Expand Down
6 changes: 0 additions & 6 deletions src/operators/operatorsBase.test.ts
Expand Up @@ -2,12 +2,6 @@ import * as op from './operatorsBase';
import { toIterator } from '../util/util';

describe('Tests Operators', () => {
test('test the curring part of GS.map', () => {
const fnMap = op.map((a: number) => 2 * a);
expect(fnMap).toBeInstanceOf(Function);
expect([...fnMap([1, 2, 3])]).toEqual([2, 4, 6]);
});

test('tests scanMap -- running sum', () => {
// let only the first occurrence of a value through.
const result = [1, 2, 1, 3, 2, 1, 3]
Expand Down
14 changes: 3 additions & 11 deletions src/operators/operatorsBase.ts
Expand Up @@ -71,22 +71,14 @@ export function* combine<T, U, V>(
/**
* apply a mapping function to an Iterable.
*/
export function map<T, U>(fnMap: (t: T) => U): (i: IterableLike<T>) => IterableIterator<U>;
export function map<T, U>(fnMap: (t: T) => U, i: IterableLike<T>): IterableIterator<U>;
export function map<T, U>(fnMap: (t: T) => U, i?: IterableLike<T>): IterableIterator<U> | ((i: IterableLike<T>) => IterableIterator<U>) {
function* fn<T, U>(fnMap: (t: T) => U, i: IterableLike<T>): IterableIterator<U> {
export function map<T, U>(i: IterableLike<T>, fnMap: (t: T) => U): IterableIterator<U> {
function* fn<T, U>(i: IterableLike<T>, fnMap: (t: T) => U): IterableIterator<U> {
for (const v of i) {
yield fnMap(v);
}
}

if (i !== undefined) {
return fn(fnMap, i);
}

return function(i: IterableLike<T>) {
return fn(fnMap, i);
};
return fn(i, fnMap);
}

export function scan<T>(i: IterableLike<T>, fnReduce: (prevValue: T, curValue: T, curIndex: number) => T): IterableIterator<T>;
Expand Down

0 comments on commit 6425306

Please sign in to comment.