musement / types-generator

3 stars 1 forks source link

refactor: remove deprecated functions #16

Closed zi closed 2 years ago

zi commented 3 years ago

With latest fp-ts updates some functions has been deprecated. Goal of this PR is to remove the deprecated functions. Some info: https://stackoverflow.com/a/67875549/657582

dinoiam commented 3 years ago

@zi Why did you replace pipe with flow? For the rest, it seems fine to me.

zi commented 3 years ago

@zi Why did you replace pipe with flow? For the rest, it seems fine to me.

I've been misled by this comment

/**
 * Use [`pipe`](https://gcanti.github.io/fp-ts/modules/function.ts.html#flow) from `function` module instead.
 *
 * @since 2.0.0
 * @deprecated
 */

but I see now that the function module exports also the pipe function

enricopolanski commented 2 years ago

Flow allows you to "curry" better.

E.g.

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

const inc = (x: number) => x + 1;

const double = (x: number) => x * 2;

const doublePlusOne = flow(inc, double); // this is okay, I composed the two functions. I can now use it doublePlusOne(3)

const doublePlusOneAlt = pipe(inc, double) // error

/*
  Argument of type '(x: number) => number' is not assignable to parameter of type 'number'.
*/

In our case actually flow allows us to remove the parameter passing.

Using pipe I would be forced to to write it as:

const doublePlusOne = (x: number) => pipe(x, inc, double)

Which is imho useful only if the passed parameter is used more than in one place (e.g. it makes sense for promptForMissingOptions as it uses the options twice).

But, e.g.,cli offers a nice opportunity for flow:

const cli = flow(parseArgumentsIntoOptions, promptForMissingOptions)

I think the "flow" ( no pun intended :D ) is much more readable left to right compared to the function version.

As for functions like this it's probably better to revert to pipe:

function promptForMissingOptions(
  options: Partial<CliConfig>
): TE.TaskEither<Error, CliConfig> {
  return pipe(
    options,
    getQuestions,
    getAnswers,
    T.map((answers) => ({ ...options, ...answers })),
    T.map(checkOptions)
  );
}