gcanti / logging-ts

Composable loggers for TypeScript
https://gcanti.github.io/logging-ts/
MIT License
98 stars 8 forks source link

withEitherLogger #23

Open waynevanson opened 4 years ago

waynevanson commented 4 years ago

I've been thinking about the question in #20 for Either. The following implementation is good for eithers, but could it be made applicable to more structures?

I think having something like this would be good for this library to.

@matthewpflueger Here is something more up your alley for your question in #20

import { either as E, task as T, taskEither as TE, console as C } from "fp-ts";
import { Kind, URIS } from "fp-ts/HKT";
import { MonadIO1 } from "fp-ts/lib/MonadIO";
import { LoggerIO } from "logging-ts/lib/IO";
import { pipe } from "fp-ts/lib/function";

export const withBiLogger = <M extends URIS>(M: MonadIO1<M>) => <G, B>(
  logger: LoggerIO<E.Either<G, B>>
) => <E, A>(f: (e: E) => G, g: (a: A) => B) => (mea: Kind<M, E.Either<E, A>>) =>
  M.chain(mea, (ea) =>
    M.map(M.fromIO(logger(E.Bifunctor.bimap(ea, f, g))), () => ea)
  );

const logger: LoggerIO<E.Either<string, string>> = E.fold(C.error, C.log);

const taskEitherLogger = withBiLogger(T.task)(logger);

const program = pipe(
  TE.of(2),
  taskEitherLogger(
    (e) => `Error, something happened: "${e}'`,
    (a) => `Success, you do you!`
  ),
  TE.map((n) => n + 2)
);

program();
matthewpflueger commented 4 years ago

This is really great! I appreciate you taking the time to do this! My skills are not at the level that I would have come up with this...

On Sun, Aug 23, 2020 at 18:04 Wayne Van Son notifications@github.com wrote:

I've been thinking about the question in #20 https://github.com/gcanti/logging-ts/issues/20 for Either. The following implementation is good for eithers, but could it be made applicable to more structures?

I think having something like this would be good for this library to.

@matthewpflueger https://github.com/matthewpflueger

Here is something more up your alley for your question in #20 https://github.com/gcanti/logging-ts/issues/20

import { either as E, task as T, taskEither as TE, console as C } from "fp-ts";

import { Kind, URIS } from "fp-ts/HKT";

import { MonadIO1 } from "fp-ts/lib/MonadIO";

import { LoggerIO } from "logging-ts/lib/IO";

import { pipe } from "fp-ts/lib/function";

export const withBiLogger = (M: MonadIO1) => <G, B>(

logger: LoggerIO<E.Either<G, B>>

) => <E, A>(f: (e: E) => G, g: (a: A) => B) => (mea: Kind<M, E.Either<E, A>>) =>

M.chain(mea, (ea) =>

M.map(M.fromIO(logger(E.Bifunctor.bimap(ea, f, g))), () => ea)

);

const logger: LoggerIO<E.Either<string, string>> = E.fold(C.error, C.log);

const taskEitherLogger = withBiLogger(T.task)(logger);

const program = pipe(

TE.of(2),

taskEitherLogger(

(e) => `Error, something happened: "${e}'`,

(a) => `Success, you do you!`

),

TE.map((n) => n + 2)

);

program();

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/gcanti/logging-ts/issues/23, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA5KLJ4OU57R2AOSJ2FMQLSCE42DANCNFSM4QIXOCHQ .

twzhangyang commented 3 years ago

Thanks guys, this taskEitherLogger is very useful for me