Skip to content

Commit

Permalink
issue-#57 - Progress on idiomatic curry - Converted methods up to './…
Browse files Browse the repository at this point in the history
…src/data/'.
  • Loading branch information
elycruz committed Aug 9, 2022
1 parent 550641d commit 085793b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
6 changes: 3 additions & 3 deletions packages/fjl/src/data/either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {isset} from '../object/isset';
import {id} from '../function/id';
import {MonadBase} from './monad';
import {FunctorMapFn} from "../types";
import {curry3, CurryOf3} from "../function";

export type Either<A, B> = Right<A> | Right<B>;

Expand Down Expand Up @@ -96,5 +95,6 @@ export type EitherFnParams = Parameters<EitherFn>;
/**
* Curried version `either`.
*/
export const $either =
curry3(either) as CurryOf3<EitherFnParams[0], EitherFnParams[1], EitherFnParams[2], ReturnType<EitherFn>>;
export const $either = <A, B, C>(leftCallback: FunctorMapFn<C>) =>
(rightCallback: FunctorMapFn<C>) =>
(_either_: Left<A> | Right<B>): C => either(leftCallback, rightCallback, _either_);
9 changes: 6 additions & 3 deletions packages/fjl/src/data/maybe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {Monad, MonadBase, MonadConstructor} from './monad';
import {Unary, UnaryPred} from "../types";
import {FunctorMapFn} from "../types";
import {$instanceOf} from "../platform/object";
import {curry2, curry3, CurryOf2, CurryOf3} from "../function";

let NothingSingleton: Nothing;

Expand Down Expand Up @@ -139,7 +138,10 @@ export const
/**
* Curried version of `maybe`.
*/
$maybe = curry3(maybe) as CurryOf3,
$maybe = <A, B>(replacement: B) =>
(fn: Unary<A, B>) =>
(maybeInst: Maybe<A> | A | null | undefined): B =>
maybe(replacement, fn, maybeInst),

/**
* Equality operator for maybes.
Expand All @@ -149,7 +151,8 @@ export const
/**
* Curried version of `maybeEqual`.
*/
$maybeEqual = curry2(maybeEqual) as CurryOf2,
$maybeEqual = <A, B>(a: Maybe<A>) =>
(b: Maybe<B>): boolean => maybeEqual(a, b),

/**
* Checks for maybe.
Expand Down
16 changes: 11 additions & 5 deletions packages/fjl/src/data/monad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
import {isset} from "../object/isset";
import {$instanceOf} from '../platform/object';
import {toFunction, curry2, CurryOf2} from "../function";
import {toFunction} from "../function";
import {
Applicative, ApplicativeConstructor, Functor,
FunctorConstructor, FunctorMapFn, Apply, ApplyConstructor, TypeRef, Unary, Binary
Expand Down Expand Up @@ -124,7 +124,9 @@ export const
/**
* Curried version of `fmap`.
*/
$fmap = curry2(fmap) as CurryOf2,
$fmap = <T, RetT>(fn: FunctorMapFn<RetT>) =>
(x: Functor<T>): Functor<RetT> | Functor =>
fmap(fn, x),

/**
* Applies function contained by applicative to contents of given functor.
Expand All @@ -136,7 +138,8 @@ export const
/**
* Curried version of `ap`.
*/
$ap = curry2(ap) as CurryOf2,
$ap = <A, B, RetT>(app: Applicative<A>) =>
(functor: Functor<B>): Functor<RetT> => app.ap(functor),

/**
* Flat maps a function over given monad's contained value.
Expand All @@ -146,7 +149,8 @@ export const
/**
* Curried version of `flatMap`.
*/
$flatMap = curry2(flatMap) as CurryOf2,
$flatMap = <T, RetT>(fn: FunctorMapFn<RetT>) =>
(monad: Monad<T>): Monad<RetT> => monad.flatMap(fn),

/**
* Unwraps monad by type.
Expand All @@ -168,4 +172,6 @@ export const
/**
* Curried version of `unwrapMonadByType`.
*/
$unwrapMonadByType = curry2(unwrapMonadByType) as CurryOf2;
$unwrapMonadByType = <T>(Type: TypeRef) =>
(monad: Monad<T> | T): any =>
unwrapMonadByType(Type, monad);

0 comments on commit 085793b

Please sign in to comment.