Skip to content

Commit

Permalink
Add branch awareness and tests for the type of 'ap'
Browse files Browse the repository at this point in the history
  • Loading branch information
Avaq committed Jul 19, 2020
1 parent ba6e210 commit 7dccce7
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
27 changes: 25 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,33 @@ export function alt<R>(second: ConcurrentResolved<R>): {
export function alt<L, R>(second: ConcurrentUncertain<L, R>): (first: ConcurrentUncertain<L, R>) => ConcurrentUncertain<L, R>

/** Apply the function in the right Future to the value in the left Future. See https://github.com/fluture-js/Fluture#ap */
export function ap<L, RA>(value: FutureInstance<L, RA>): <RB>(apply: FutureInstance<L, (value: RA) => RB>) => FutureInstance<L, RB>
export function ap<L>(second: Rejected<L>): {
<T>(first: Rejected<T>): Rejected<T>
(first: Resolved<(value: any) => any>): Rejected<L>
(first: Uncertain<L, (value: any) => any>): Rejected<L>
}

/** Apply the function in the right Future to the value in the left Future. See https://github.com/fluture-js/Fluture#ap */
export function ap<L, RA>(second: Uncertain<L, RA>): {
<T>(first: Rejected<T>): Rejected<T>
<RB>(first: Resolved<(value: RA) => RB>): Uncertain<L, RB>
<RB>(first: Uncertain<L, (value: RA) => RB>): Uncertain<L, RB>
}

/** Apply the function in the right ConcurrentFuture to the value in the left ConcurrentFuture. See https://github.com/fluture-js/Fluture#ap */
export function ap(value: ConcurrentNever): {
<L>(func: ConcurrentRejected<L>): ConcurrentRejected<L>
<L>(func: ConcurrentUncertain<L, (value: any) => any>): ConcurrentNever
}

/** Apply the function in the right ConcurrentFuture to the value in the left ConcurrentFuture. See https://github.com/fluture-js/Fluture#ap */
export function ap<L>(value: ConcurrentRejected<L>): (func: ConcurrentUncertain<L, (value: any) => any>) => ConcurrentRejected<L>

/** Apply the function in the right ConcurrentFuture to the value in the left ConcurrentFuture. See https://github.com/fluture-js/Fluture#ap */
export function ap<RA>(value: ConcurrentResolved<RA>): <L, RB>(func: ConcurrentUncertain<L, (value: RA) => RB>) => ConcurrentUncertain<L, RB>

/** Apply the function in the right ConcurrentFuture to the value in the left ConcurrentFuture. See https://github.com/fluture-js/Fluture#ap */
export function ap<L, RA>(value: ConcurrentFutureInstance<L, RA>): <RB>(apply: ConcurrentFutureInstance<L, (value: RA) => RB>) => ConcurrentFutureInstance<L, RB>
export function ap<L, RA>(value: ConcurrentUncertain<L, RA>): <RB>(func: ConcurrentUncertain<L, (value: RA) => RB>) => ConcurrentUncertain<L, RB>

/** Apply the function in the right Future to the value in the left Future in parallel. See https://github.com/fluture-js/Fluture#pap */
export function pap<L, RA>(value: FutureInstance<L, RA>): <RB>(apply: FutureInstance<L, (value: RA) => RB>) => FutureInstance<L, RB>
Expand Down
55 changes: 55 additions & 0 deletions test/types/ap.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {expectType, expectError} from 'tsd';

import * as fl from '../../index.js';

const mx = fl.resolve (42);
const mf = fl.resolve ((x: number) => x + 1);

const msx: fl.Uncertain<string, number> = fl.resolve (42);
const mnx: fl.Uncertain<number, number> = fl.resolve (42);
const msf: fl.Uncertain<string, (x: number) => number> = fl.resolve ((x: number) => x + 1);

expectType<fl.Resolved<number>> (fl.ap (mx) (mf));
expectType<fl.Uncertain<string, number>> (fl.ap (msx) (msf));

expectType<fl.Resolved<never>> (fl.ap (fl.never) (fl.never));
expectType<fl.Resolved<never>> (fl.ap (mx) (fl.never));
expectType<fl.Resolved<never>> (fl.ap (fl.never) (mf));

expectType<fl.Resolved<never>> (fl.ap (fl.reject ('a')) (fl.never));
expectType<fl.Rejected<string>> (fl.ap (fl.never) (fl.reject ('a')));

expectType<fl.Rejected<string>> (fl.ap (fl.reject ('a')) (mf));
expectType<fl.Rejected<string>> (fl.ap (mx) (fl.reject ('a')));
expectType<fl.Rejected<number>> (fl.ap (fl.reject (42)) (fl.reject (42)));
expectType<fl.Rejected<number>> (fl.ap (fl.reject ('a')) (fl.reject (42)));

expectError (fl.ap (mnx) (msf));
expectError (fl.ap (mx) (mx));
expectError (fl.ap (mf) (mf));

const cmx = fl.Par (fl.resolve (42));
const cmf = fl.Par (fl.resolve ((x: number) => x + 1));

const cmsx: fl.ConcurrentUncertain<string, number> = fl.Par (fl.resolve (42));
const cmnx: fl.ConcurrentUncertain<number, number> = fl.Par (fl.resolve (42));
const cmsf: fl.ConcurrentUncertain<string, (x: number) => number> = fl.Par (fl.resolve ((x: number) => x + 1));

expectType<fl.ConcurrentResolved<number>> (fl.ap (cmx) (cmf));
expectType<fl.ConcurrentUncertain<string, number>> (fl.ap (cmsx) (cmsf));

expectType<fl.ConcurrentNever> (fl.ap (fl.Par (fl.never)) (fl.Par (fl.never)));
expectType<fl.ConcurrentNever> (fl.ap (cmx) (fl.Par (fl.never)));
expectType<fl.ConcurrentNever> (fl.ap (fl.Par (fl.never)) (cmf));

expectType<fl.ConcurrentRejected<string>> (fl.ap (fl.Par (fl.reject ('a'))) (fl.Par (fl.never)));
expectType<fl.ConcurrentRejected<string>> (fl.ap (fl.Par (fl.never)) (fl.Par (fl.reject ('a'))));

expectType<fl.ConcurrentRejected<string>> (fl.ap (fl.Par (fl.reject ('a'))) (cmf));
expectType<fl.ConcurrentRejected<string>> (fl.ap (cmx) (fl.Par (fl.reject ('a'))));
expectType<fl.ConcurrentRejected<number>> (fl.ap (fl.Par (fl.reject (42))) (fl.Par (fl.reject (42))));

expectError (fl.ap (cmnx) (cmsf));
expectError (fl.ap (fl.Par (fl.reject ('a'))) (fl.Par (fl.reject (42))));
expectError (fl.ap (cmx) (cmx));
expectError (fl.ap (cmf) (cmf));

0 comments on commit 7dccce7

Please sign in to comment.