Fp-ts: Prelude file

Created on 13 Sep 2019  路  2Comments  路  Source: gcanti/fp-ts

馃殌 Feature request

Hi guys, the project is awesome, but for newcomers from other FP languages would be much easier to start coding if there was some Prelude file with all basic types and combinators, like Array, Option, map, flatMap, filter, find, etc. This would drammatically decrease effort necessary to adapt to new syntax, would remove necessity to grep the repo to find how to apply, for instance, Filterable type class. This is all basic stuff which usually works out-of-the-box in Scala or Haskell without any imports.

Current Behavior

Necessary to import a lot of files to write something like this (in plain TS):

let xs: number[] = [1,2,3,4,5,6,7];

let result = xs
  .filter(x => x % 2 === 0)
  .map(x => x ** 2)
  .find(x => x % 3 === 0);

console.log(result);

Desired Behavior

Would be awesome if the snippet above would be implementable without more than 1-2 imports and several method renamings.

Who does this impact? Who is this for?

For newcomers from other FP languages like Scala or Haskell.

Describe alternatives you've considered

To add docs page how to import all the core stuff.

out of scope

Most helpful comment

Here's something I'm using in a prelude.ts file, it would be interesting to hear from others, too. I don't know whether this would be generalizable, as there's a conflict between wanting to keep the names as short as possible, but not interfering with other people's code.

import * as Ar from "fp-ts/lib/Array";
import * as E from "fp-ts/lib/Either";
import * as Eq from "fp-ts/lib/Eq";
import * as Ma from "fp-ts/lib/Map";
import * as An from "fp-ts/lib/NonEmptyArray";
import * as O from "fp-ts/lib/Option";
import * as Ord from "fp-ts/lib/Ord";
import * as R from "fp-ts/lib/Record";
import * as Te from "fp-ts/lib/TaskEither";
import * as io from "io-ts";
import * as nt from "newtype-ts";

export * from "fp-ts/lib/function";
export * from "fp-ts/lib/pipeable";
export { An, Ar, E, Eq, Ma, O, Ord, R, Te, io, nt };

Also, there's fp-ts-ramda, which @giogonzo is experimenting with, that uses fp-ts under the hood but provides the convenient functions from Ramda.

All 2 comments

Here's something I'm using in a prelude.ts file, it would be interesting to hear from others, too. I don't know whether this would be generalizable, as there's a conflict between wanting to keep the names as short as possible, but not interfering with other people's code.

import * as Ar from "fp-ts/lib/Array";
import * as E from "fp-ts/lib/Either";
import * as Eq from "fp-ts/lib/Eq";
import * as Ma from "fp-ts/lib/Map";
import * as An from "fp-ts/lib/NonEmptyArray";
import * as O from "fp-ts/lib/Option";
import * as Ord from "fp-ts/lib/Ord";
import * as R from "fp-ts/lib/Record";
import * as Te from "fp-ts/lib/TaskEither";
import * as io from "io-ts";
import * as nt from "newtype-ts";

export * from "fp-ts/lib/function";
export * from "fp-ts/lib/pipeable";
export { An, Ar, E, Eq, Ma, O, Ord, R, Te, io, nt };

Also, there's fp-ts-ramda, which @giogonzo is experimenting with, that uses fp-ts under the hood but provides the convenient functions from Ramda.

I wrote my own prelude package for my current projects. It doesn't contain all of fp-ts and io-ts. Just the main stuff I'm using. Below is an example of how I use the imports in a project.

import * as P from 'maasglobal-prelude-ts';
P.Either    // static type
P.Either    // io-ts codec
P.Either_   // fp-ts utils
P.Either__  // fp-ts type class implementations
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jollytoad picture jollytoad  路  4Comments

gcanti picture gcanti  路  3Comments

vicrac picture vicrac  路  4Comments

mattgrande picture mattgrande  路  3Comments

mohsensaremi picture mohsensaremi  路  3Comments