Skip to content

Commit

Permalink
#42 - Added missing changes from previous commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
elycruz committed Jun 13, 2021
1 parent 7aeba75 commit af5e51d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 52 deletions.
4 changes: 2 additions & 2 deletions packages/fjl/src/data/monad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export class MonadBase<T> implements Monad<T> {
}

static liftA2<A, B, RetT>(fn, appA: Applicative<A>, appB: Applicative<B>): Applicative<RetT> {
return (this.constructor as ApplicativeConstructor<RetT>).of(
fn(appA.valueOf(), appB.valueOf)
return (appA.constructor as ApplicativeConstructor<RetT>).of(
fn(appA.valueOf(), appB.valueOf())
);
}

Expand Down
100 changes: 50 additions & 50 deletions packages/fjl/tests/data/monad_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,66 @@ import {Applicative, FunctorMapFn} from "../../src/types";
import {id} from "../../src/function";

describe('#valueOf()', () => {
(<[Monad<any>, any][]>[
[just(20), 20],
[just(null), null],
[right(20), 20],
[left('Oh no ... an error occurred.'), 'Oh no ... an error occurred.'],
])
.forEach(([monad, expectedValue]) => {
it(`valueOf(${monad}) === ${expectedValue}`, () => {
expect(valueOf(monad)).toEqual(expectedValue);
});
})
(<[Monad<any>, any][]>[
[just(20), 20],
[just(null), null],
[right(20), 20],
[left('Oh no ... an error occurred.'), 'Oh no ... an error occurred.'],
])
.forEach(([monad, expectedValue]) => {
it(`valueOf(${monad}) === ${expectedValue}`, () => {
expect(valueOf(monad)).toEqual(expectedValue);
});
})
});

describe('#join()', () => {
it(`join === valueOf`, () => {
expect(join).toEqual(valueOf);
});
it(`join === valueOf`, () => {
expect(join).toEqual(valueOf);
});
});

describe('#fmap()', () => {
(<[Monad<any>, FunctorMapFn<any>, Monad<any>][]>[
[nothing(), id, nothing()],
[just(null), id, just(null)],
[just(2), x => x * 2, just(4)],
[[1, 2, 3], x => x * 2, [2, 4, 6]]
])
.forEach(([monad, mapper, monad2]) => {
it(`fmap(${mapper}, ${monad}).valueOf() == ${monad2}.valueOf()`, function () {
const rslt = fmap(mapper, monad) as Monad<any>;
expect(rslt.valueOf()).toEqual(monad2.valueOf());
});
})
(<[Monad<any>, FunctorMapFn<any>, Monad<any>][]>[
[nothing(), id, nothing()],
[just(null), id, just(null)],
[just(2), x => x * 2, just(4)],
[[1, 2, 3], x => x * 2, [2, 4, 6]]
])
.forEach(([monad, mapper, monad2]) => {
it(`fmap(${mapper}, ${monad}).valueOf() == ${monad2}.valueOf()`, function () {
const rslt = fmap(mapper, monad) as Monad<any>;
expect(rslt.valueOf()).toEqual(monad2.valueOf());
});
})
});

describe('#ap', () => {
(<[Applicative<<T>(x: T) => T>, Monad<any>, Monad<any>][]>[
[just((x: number) => x * 2), just(4), just(8)],
[just((x: number) => x * 3), just(2), just(6)],
[just(x => x), nothing(), nothing()],
[just(x => x), just(null), just(null)]
])
.forEach(([applicative, functor, expected]) => {
it(`${applicative}`, function () {
const rslt = ap(applicative, functor) as Monad<any>;
expect(rslt.join()).toEqual(expected.join());
});
});
(<[Applicative<<T>(x: T) => T>, Monad<any>, Monad<any>][]>[
[just((x: number) => x * 2), just(4), just(8)],
[just((x: number) => x * 3), just(2), just(6)],
[just(x => x), nothing(), nothing()],
[just(x => x), just(null), just(null)]
])
.forEach(([applicative, functor, expected]) => {
it(`${applicative}`, function () {
const rslt = ap(applicative, functor) as Monad<any>;
expect(rslt.join()).toEqual(expected.join());
});
});
});

describe('#flatMap', () => {
(<[Monad<any>, FunctorMapFn<any>, Monad<any>][]>[
[nothing(), id, nothing()],
[just(null), id, just(null)],
[just(2), x => x * 2, just(4)],
[[1, 2, 3], x => x * 2, [2, 4, 6]]
])
.forEach(([monad, mapper, monad2]) => {
it(`flatMap(${mapper}, ${monad}).valueOf() == ${monad2}.valueOf()`, function () {
const rslt = flatMap(mapper, monad) as Monad<any>;
expect(rslt.valueOf()).toEqual(monad2.valueOf());
});
})
(<[Monad<any>, FunctorMapFn<any>, Monad<any>][]>[
[nothing(), id, nothing()],
[just(null), id, just(null)],
[just(2), x => x * 2, just(4)],
[[1, 2, 3], x => x * 2, [2, 4, 6]]
])
.forEach(([monad, mapper, monad2]) => {
it(`flatMap(${mapper}, ${monad}).valueOf() == ${monad2}.valueOf()`, function () {
const rslt = flatMap(mapper, monad) as Monad<any>;
expect(rslt.valueOf()).toEqual(monad2.valueOf());
});
})
});

0 comments on commit af5e51d

Please sign in to comment.