Skip to content
This repository has been archived by the owner on May 3, 2021. It is now read-only.

Commit

Permalink
feat: minor either and reader_either changes
Browse files Browse the repository at this point in the history
* renamed either getMonoid and getSemigroup to getRightMonoid
  and getRightSemigroup
* implemented getLeftSemigroup in either.ts
* reverse compose in fns.ts to make it pipeable
* implemented compose for reader_either.ts
* added Guard type to types.ts
  • Loading branch information
baetheus committed Dec 27, 2020
1 parent 8a59239 commit af0850d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
13 changes: 10 additions & 3 deletions either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,24 @@ export const getOrd = <E, A>(
},
});

export const getSemigroup = <E, A>(
export const getLeftSemigroup = <E, A>(
SE: TC.Semigroup<E>,
): TC.Semigroup<Either<E, A>> => ({
concat: (x, y) =>
isRight(x) ? x : isRight(y) ? y : left(SE.concat(x.left, y.left)),
});

export const getRightSemigroup = <E, A>(
SA: TC.Semigroup<A>,
): TC.Semigroup<Either<E, A>> => ({
concat: (x, y) =>
isLeft(x) ? x : isLeft(y) ? y : right(SA.concat(x.right, y.right)),
});

export const getMonoid = <E, A>(
export const getRightMonoid = <E, A>(
MA: TC.Monoid<A>,
): TC.Monoid<Either<E, A>> => ({
...getSemigroup(MA),
...getRightSemigroup(MA),
empty: () => right(MA.empty()),
});

Expand Down
4 changes: 2 additions & 2 deletions fns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export const flip = <A, B, C>(f: Fn<[A], Fn<[B], C>>): Fn<[B], Fn<[A], C>> =>
export const swap = <A, B, C>(fn: Fn<[A, B], C>): Fn<[B, A], C> =>
(b, a) => fn(a, b);

export const compose = <A, B>(fab: Fn<[A], B>) =>
<C>(fbc: Fn<[B], C>): Fn<[A], C> => (a) => fbc(fab(a));
export const compose = <B, C>(fbc: Fn<[B], C>) =>
<A>(fab: Fn<[A], B>): Fn<[A], C> => (a) => fbc(fab(a));

export const constant = <A>(a: A): Lazy<A> => () => a;

Expand Down
6 changes: 5 additions & 1 deletion reader_either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const getRightMonad = <F>(
return {
of: right,
ap: (tfab) => (ta) => (r) => M.ap(tfab(r))(ta(r)),
map: (fab) => (ta) => (r) => M.map(fab)(ta(r)),
map: (fab) => (ta) => flow(ta, M.map(fab)),
join: (tta) =>
(r) =>
pipe(
Expand All @@ -127,6 +127,10 @@ export const { of, ap, map, join, chain } = Monad;

export const { bimap, mapLeft } = Bifunctor;

export const compose = <E, B, C>(rbc: ReaderEither<B, E, C>) =>
<A>(rab: ReaderEither<A, E, B>): ReaderEither<A, E, C> =>
flow(rab, E.chain(rbc));

/***************************************************************************************************
* @section Sequence
**************************************************************************************************/
Expand Down
4 changes: 4 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export interface Predicate<A> {
(a: A): boolean;
}

export interface Guard<A> {
(a: unknown): a is A;
}

export interface Refinement<A, B extends A> {
(a: A): a is B;
}
Expand Down

0 comments on commit af0850d

Please sign in to comment.