Skip to content

Commit

Permalink
Move instances into the IO type.
Browse files Browse the repository at this point in the history
  • Loading branch information
rzeigler committed Apr 23, 2019
1 parent 961c596 commit 597bb41
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 98 deletions.
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ export { IO } from "./io";
export { Fiber } from "./fiber";
export { Ref } from "./ref";
export { Deferred } from "./deferred";
export { AsyncQueue, unboundedQueue, boundedQueue, OverflowStrategy } from "./queue";
export * from "./result";
export * from "./console";
export * from "./instances";
96 changes: 0 additions & 96 deletions src/instances.ts

This file was deleted.

90 changes: 90 additions & 0 deletions src/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,97 @@ import { Runtime } from "./internal/runtime";
import { Ref } from "./ref";
import { Abort, Attempt, Cause, FiberResult, Raise, Result, Value } from "./result";

import { Applicative2 } from "fp-ts/lib/Applicative";
import { Monad2 } from "fp-ts/lib/Monad";
import { Monoid } from "fp-ts/lib/Monoid";
import { Semigroup } from "fp-ts/lib/Semigroup";

export const URI = "IO";
export type URI = typeof URI;

declare module "fp-ts/lib/HKT" {
interface URI2HKT2<L, A> {
IO: IO<L, A>;
}
}

const map = <L, A, B>(fa: IO<L, A>, f: (a: A) => B): IO<L, B> => fa.map(f);
const of = <L, A>(a: A): IO<L, A> => IO.pure(a);

export class IO<E, A> {
/**
* Get the Monad instance for an IO<E, A>
*/
public static monad: Monad2<URI> = {
URI,
map,
of,
ap: <L, A, B>(fab: IO<L, (a: A) => B>, fa: IO<L, A>): IO<L, B> => fab.ap_(fa),
chain: <L, A, B>(fa: IO<L, A>, f: (a: A) => IO<L, B>): IO<L, B> => fa.chain(f)
};

/**
* Get a parallel applicative instance for IO<E, A>
*/
public static parallelApplicative: Applicative2<URI> = {
URI,
map,
of,
ap: <L, A, B>(fab: IO<L, (a: A) => B>, fa: IO<L, A>): IO<L, B> => fab.parAp_(fa)
}

/**
* Get a monoid for IO<E, A> that combines actions by racing them.
*/
public static getRaceMonoid<L, A>(): Monoid<IO<L, A>> {
return {
empty: IO.never_() as unknown as IO<L, A>,
concat: (l: IO<L, A>, r: IO<L, A>): IO<L, A> => l.race(r)
};
}

/**
* Get a semigroup for IO<E, A> given a semigroup for A.
* @param S
*/
public static getSemigroup<L, A>(S: Semigroup<A>): Semigroup<IO<L, A>> {
return {
concat: (l: IO<L, A>, r: IO<L, A>): IO<L, A> => l.map2(r, S.concat)
};
}

/**
* Get a semigroup for IO<E, A> given a semigroup for A that runs in parallel
* @param S
*/
public static getParallelSemigroup<L, A>(S: Semigroup<A>): Semigroup<IO<L, A>> {
return {
concat: (l: IO<L, A>, r: IO<L, A>): IO<L, A> => l.parMap2(r, S.concat)
};
}

/**
* Get a monoid for IO<E, A> given a monoid for A that runs in sequence
* @param M
*/
public static getMonoid<L, A>(M: Monoid<A>): Monoid<IO<L, A>> {
return {
...IO.getSemigroup(M),
empty: IO.pure(M.empty)
};
}

/**
* Get a monoid for IO<E, A> given a monoid for A that runs in sequence
* @param M
*/
public static getParallelMonoid<L, A>(M: Monoid<A>): Monoid<IO<L, A>> {
return {
...IO.getParallelSemigroup(M),
empty: IO.pure(M.empty)
};
}

/**
* Construct an IO from a pure value.
* @param a
Expand Down

0 comments on commit 597bb41

Please sign in to comment.