Skip to content

Commit

Permalink
simplify definitions of polymorphic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed Oct 8, 2017
1 parent 37134e3 commit e732768
Show file tree
Hide file tree
Showing 19 changed files with 238 additions and 435 deletions.
577 changes: 181 additions & 396 deletions index.d.ts

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions test-ts/alt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ test('alt', () => {
eq(S.alt.toString(), 'alt :: Alt f => f a -> f a -> f a');

eq(S.alt([])([]), []);
const empty: number[] = []; // Microsoft/TypeScript#8944
eq(S.alt(empty)([1, 2, 3]), [1, 2, 3]);
eq(S.alt([])([1, 2, 3]), [1, 2, 3]);
eq(S.alt([1, 2, 3])([]), [1, 2, 3]);
eq(S.alt([1, 2, 3])([4, 5, 6]), [1, 2, 3, 4, 5, 6]);
eq(S.alt({})({}), {});
Expand Down
2 changes: 1 addition & 1 deletion test-ts/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test('chain', () => {
eq(S.chain.length, 2);
eq(S.chain.toString(), 'chain :: Chain m => (a -> m b) -> m a -> m b');

eq(S.chain<number[], number>(S.I)([[1, 2], [3, 4], [5, 6]]), [1, 2, 3, 4, 5, 6]);
eq(S.chain(S.I)([[1, 2], [3, 4], [5, 6]]), [1, 2, 3, 4, 5, 6]);
eq(S.chain(S.parseFloat)(S.Nothing), S.Nothing);
eq(S.chain(S.parseFloat)(S.Just('X')), S.Nothing);
eq(S.chain(S.parseFloat)(S.Just('0')), S.Just(0));
Expand Down
4 changes: 2 additions & 2 deletions test-ts/concat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ test('concat', () => {

eq(S.concat([])([]), []);
eq(S.concat([1, 2, 3])([]), [1, 2, 3]);
eq(S.concat<number>([])([4, 5, 6]), [4, 5, 6]);
eq(S.concat([])([4, 5, 6]), [4, 5, 6]);
eq(S.concat([1, 2, 3])([4, 5, 6]), [1, 2, 3, 4, 5, 6]);

eq(S.concat('')(''), '');
eq(S.concat('foo')(''), 'foo');
eq(S.concat('')('bar'), 'bar');
eq(S.concat('foo')('bar'), 'foobar');

eq(S.concat<S.Maybe<string>>(S.Nothing)(S.Nothing), S.Nothing);
eq(S.concat(S.Nothing)(S.Nothing), S.Nothing);
eq(S.concat(S.Just('foo'))(S.Nothing), S.Just('foo'));
eq(S.concat(S.Nothing)(S.Just('bar')), S.Just('bar'));
eq(S.concat(S.Just('foo'))(S.Just('bar')), S.Just('foobar'));
Expand Down
4 changes: 2 additions & 2 deletions test-ts/groupBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ test('groupBy', () => {

eq(S.groupBy(productsOf3)([]), []);
eq(S.groupBy(productsOf3)([1, 2, 3, 4, 5, 6, 7, 8, 9]), [[1], [2, 3], [4], [5, 6], [7], [8, 9]]);
eq(S.groupBy<number>(S.equals)([1, 1, 2, 1, 1]), [[1, 1], [2], [1, 1]]);
eq(S.groupBy(S.equals)([1, 1, 2, 1, 1]), [[1, 1], [2], [1, 1]]);
eq(S.groupBy(zeroSum)([2, -3, 3, 3, 3, 4, -4, 4]), [[2], [-3, 3, 3, 3], [4, -4], [4]]);

jsc.assert(jsc.forall('nat -> nat -> bool', 'array nat', function(f: (x: number) => (y: number) => boolean, xs: number[]) {
jsc.assert(jsc.forall('nat -> nat -> bool', 'array nat', function(f: (x: number) => (y: number) => boolean, xs: Array<number>) {
return S.equals(S.join(S.groupBy(f)(xs)))(xs);
}), {tests: 100});

Expand Down
2 changes: 1 addition & 1 deletion test-ts/on.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ test('on', () => {
eq(S.on.toString(), 'on :: (b -> b -> c) -> (a -> b) -> a -> a -> c');

eq(S.on(rem)(S.prop('x'))({x: 5, y: 5})({x: 3, y: 3}), 2);
eq(S.on(S.concat)(S.reverse)([1, 2, 3])([4, 5, 6]), [3, 2, 1, 6, 5, 4]);
eq(S.on<Array<number>, Array<number>, Array<number>>(S.concat)(S.reverse)([1, 2, 3])([4, 5, 6]), [3, 2, 1, 6, 5, 4]);

});
2 changes: 1 addition & 1 deletion test-ts/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ test('reduce', () => {
eq(S.reduce(S.concat)('x')({a: 'A', b: 'B', c: 'C'}), 'xABC');
eq(S.reduce(S.concat)('x')({c: 'C', b: 'B', a: 'A'}), 'xABC');
eq(S.reduce(S.concat)('x')(S.Just('A')), 'xA');
eq(S.reduce(S.lift2<string, string, string>(S.concat))(S.Just('x'))([S.Just('A'), S.Just('B'), S.Just('C')]), S.Just('xABC'));
eq(S.reduce(S.lift2(S.concat))(S.Just('x'))([S.Just('A'), S.Just('B'), S.Just('C')]), S.Just('xABC'));

});
18 changes: 9 additions & 9 deletions test-ts/traverse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as S from '..';
const S = require('../test/internal/sanctuary');

import Identity from './internal/Identity';
import eq from './internal/eq';
Expand All @@ -17,19 +17,19 @@ test('traverse', () => {
eq(S.traverse(S.Maybe)(S.parseInt(16))({a: 'A', b: 'B', c: 'C'}), S.Just({a: 10, b: 11, c: 12}));
eq(S.traverse(S.Maybe)(S.parseInt(16))({a: 'A', b: 'B', c: 'C', x: 'X'}), S.Nothing);

eq(S.traverse(Array)<Array<string>, string>(S.I)([]), [[]]);
eq(S.traverse(Array)<Array<string>, string>(S.I)([['A', 'a']]), [['A'], ['a']]);
eq(S.traverse(Array)<Array<string>, string>(S.I)([['A', 'a'], ['B']]), [['A', 'B'], ['a', 'B']]);
eq(S.traverse(Array)<Array<string>, string>(S.I)([['A', 'a'], ['B', 'b']]), [['A', 'B'], ['A', 'b'], ['a', 'B'], ['a', 'b']]);
eq(S.traverse(Array)(S.I)([]), [[]]);
eq(S.traverse(Array)(S.I)([['A', 'a']]), [['A'], ['a']]);
eq(S.traverse(Array)(S.I)([['A', 'a'], ['B']]), [['A', 'B'], ['a', 'B']]);
eq(S.traverse(Array)(S.I)([['A', 'a'], ['B', 'b']]), [['A', 'B'], ['A', 'b'], ['a', 'B'], ['a', 'b']]);

eq(S.traverse(Array)(S.words)(of('')), []);
eq(S.traverse(Array)(S.words)(of('foo')), [of('foo')]);
eq(S.traverse(Array)(S.words)(of('foo bar')), [of('foo'), of('bar')]);
eq(S.traverse(Array)(S.words)(of('foo bar baz')), [of('foo'), of('bar'), of('baz')]);

eq(S.traverse(Identity)<S.Applicative<number>, number>(S.I)([]), of([]));
eq(S.traverse(Identity)<S.Applicative<number>, number>(S.I)([of(1)]), of([1]));
eq(S.traverse(Identity)<S.Applicative<number>, number>(S.I)([of(1), of(2)]), of([1, 2]));
eq(S.traverse(Identity)<S.Applicative<number>, number>(S.I)([of(1), of(2), of(3)]), of([1, 2, 3]));
eq(S.traverse(Identity)(S.I)([]), of([]));
eq(S.traverse(Identity)(S.I)([of(1)]), of([1]));
eq(S.traverse(Identity)(S.I)([of(1), of(2)]), of([1, 2]));
eq(S.traverse(Identity)(S.I)([of(1), of(2), of(3)]), of([1, 2, 3]));

});
2 changes: 1 addition & 1 deletion test/typescript/append.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import {append} from '../..';
// $ExpectType number[]
append(3)([1, 2]);

// $ExpectError Argument of type 'number[]' is not assignable to parameter of type 'string[]'.
// $ExpectType Applicative<string>
append('foo')([1, 2]);
4 changes: 2 additions & 2 deletions test/typescript/elem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ elem('foo')(['foo', 'bar']);
// $ExpectType boolean
elem('foo')({a: 'foo', b: 'bar'});

// $ExpectError Argument of type 'string[]' is not assignable to parameter of type 'number[] | Foldable<number> | StrMap<number>'.
// $ExpectType boolean
elem(1)(['foo', 'bar']);

// $ExpectError Argument of type '{ a: string; b: string; }' is not assignable to parameter of type 'number[] | Foldable<number> | StrMap<number>'.
// $ExpectType boolean
elem(1)({a: 'foo', b: 'bar'});
4 changes: 2 additions & 2 deletions test/typescript/filterM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ filterM(odd)([1, 2, 3]);
// $ExpectError Argument of type '(n: number) => 0' is not assignable to parameter of type 'Predicate<number>'.
filterM((n: number) => 0)([1, 2, 3]);

// $ExpectType Maybe<number>
// $ExpectType Foldable<number>
filterM(odd)(Nothing);

// $ExpectType Maybe<number>
// $ExpectType Foldable<number>
filterM(odd)(Just(1));
2 changes: 1 addition & 1 deletion test/typescript/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import {find, odd} from '../..';
// $ExpectType Maybe<number>
find(odd)([1, 2, 3]);

// $ExpectError Argument of type 'string[]' is not assignable to parameter of type 'number[] | Foldable<number> | StrMap<number>'.
// $ExpectType Maybe<number>
find(odd)(['foo', 'bar']);
2 changes: 1 addition & 1 deletion test/typescript/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ map(mult(2))([1, 2, 3]);
// $ExpectError The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
map(n => n * 2)(['foo', 1, 2]);

// $ExpectError Argument of type '(string | number)[]' is not assignable to parameter of type 'number[]'.
// $ExpectType Functor<number>
map(mult(2))(['foo', 1, 2]);

// TODO: Add tests using Maybe, Either, Functor, placeholder params
31 changes: 26 additions & 5 deletions test/typescript/pipe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
import {add, sub, pipe} from '../..';
import {add, pipe} from '../..';

// $ExpectType any
pipe([add(1), sub(1)])(10);
// $ExpectType {}
pipe([])(0);

// $ExpectType any
pipe([add(1), sub(1)])('x');
// $ExpectType number
pipe([add(1)])(0);

// $ExpectType number
pipe([add(1), add(1)])(0);

// $ExpectType number
pipe([add(1), add(1), add(1)])(0);

// $ExpectType number
pipe([add(1), add(1), add(1), add(1)])(0);

// $ExpectType number
pipe([add(1), add(1), add(1), add(1), add(1)])(0);

// $ExpectType number
pipe([add(1), add(1), add(1), add(1), add(1), add(1)])(0);

// $ExpectType number
pipe([add(1), add(1), add(1), add(1), add(1), add(1), add(1)])(0);

// $ExpectType string
pipe([(s: string) => s.length, add(1), add(1), (n: number) => n.toFixed(2)])('');
4 changes: 2 additions & 2 deletions test/typescript/pluck.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {pluck} from '../..';

// $ExpectType any[]
// $ExpectType Functor<any>
pluck('x')([{x: 1}, {x: 2, y: 3}]);

// $ExpectType any[]
// $ExpectType Functor<any>
pluck('x')([{x: 'foo'}, {x: 'bar', y: 'baz'}]);
2 changes: 1 addition & 1 deletion test/typescript/prepend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import {prepend} from '../..';
// $ExpectType number[]
prepend(3)([1, 2]);

// $ExpectError Argument of type 'number[]' is not assignable to parameter of type 'string[]'.
// $ExpectType Applicative<string>
prepend('foo')([1, 2]);
6 changes: 3 additions & 3 deletions test/typescript/reduce.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Just, Nothing, add, concat, reduce} from '../..';
import {Just, Nothing, add, reduce} from '../..';

// $ExpectType number
reduce(add)(0)([1, 2, 3]);
Expand All @@ -7,9 +7,9 @@ reduce(add)(0)([1, 2, 3]);
reduce((s1: string) => s2 => s1 + s2)('a')(['b', 'c']);

// $ExpectType string[]
reduce((a1: string[]) => a2 => concat(a1)(a2))([])([['a'], [], ['b', 'c']]);
reduce((a1: Array<string>) => a2 => a1.concat(a2))([])([['a'], [], ['b', 'c']]);

// $ExpectError Argument of type '(string | number)[]' is not assignable to parameter of type 'number[] | Maybe<number> | Foldable<number> | StrMap<number> | Either<{}, number>'.
// $ExpectType number
reduce(add)(0)([1, 'foo', 3]);

// TODO: Placeholder tests, foldable tests, curried version tests, type error tests
Expand Down
3 changes: 0 additions & 3 deletions test/typescript/reverse.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {reverse} from '../..';

// $ExpectType string
reverse('foo');

// $ExpectType number[]
reverse([1, 2, 3]);

Expand Down
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"space-before-function-paren": false,
"typedef-whitespace": false,
"unified-signatures": false,
"variable-name": false,
"whitespace": false
}
}

0 comments on commit e732768

Please sign in to comment.