Fp-ts: Support for primitive types?

Created on 14 Feb 2020  路  6Comments  路  Source: gcanti/fp-ts

馃殌 Feature request

I wonder if it's nice if there were support for primitive types, namely something like fp-ts/lib/number or fp-ts/lib/string.
Or is it simply out of scope of this project?

Current Behavior

boolean.ts exists, only fold function is defined.
Array.ts and Date.ts can also be regarded as JavaScript built-in types.

Desired Behavior

Having number.ts and/or string.ts

Suggested Solution

Adding number.ts and/or string.ts with accompanying functions.
Enriching boolean.ts.

Who does this impact? Who is this for?

All users.

Describe alternatives you've considered

Mark them as out of scope.

Additional context

Nowadays it's believed that it's a bad manners to extend prototypes.
Providing compositional (pipeable) utility functions from this package can be a great benefit for all FPers.

Your environment

| Software | Version(s) |
| ---------- | ---------- |
| fp-ts | 2.5.0 |
| TypeScript | - |

Needs Proposal

Most helpful comment

I've created this repo: https://github.com/samhh/fp-ts-std

The idea is that it'll be something in-between Ramda for fp-ts and a wrapper around unsafe JS APIs.

Contributions welcome! :slightly_smiling_face:

All 6 comments

I second this. I've worked a little bit with Ramda before but the TypeScript support is not ideal. I moved to fp-ts and I'm missing a few functions, for example, dropLeft<string>(n: number)(s: string) to remove the first n chars from string s.

I'll keep an eye here so I can help with pull-requests, since I've written a few of these modules for my personal use already.

Looks like there's a strong consecutive demand from the community for some standard lib. AFAIK a standard place for the helpers is fp-ts-contrib but it doesn't follow any structure.
At the moment I'm definitely against of including such stdlib into fp-ts core (I may be wrong though) because it blurs the boundaries and bloats API.
@gcanti Looks like we need a centralised and consistent solution.

related #1138 #1136

@filipekiss - I see your second and raise you a third.

A number of stdlib methods either throw errors, perform i/o, or have "invalid" type states (think InvalidDate, NaN etc).

I suggest we look at global types, and define fp-ts typeclasses like Ord, Eq, etc for them, then add well typed, functional methods based on their prototype (ie: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) for each.

// String.ts
export type Character = string & { length: 1 }
export const ordString = ...
export const eqString = ...
export const string = { ... }
export const charAt = (x: number): Option<Character> => ...

@christianbradley that would be an amazing improvement!

If a verdict is reached as to where a module like this would go, be in fp-ts or somewhere else, I'll be happy to do some of this.

I've created this repo: https://github.com/samhh/fp-ts-std

The idea is that it'll be something in-between Ramda for fp-ts and a wrapper around unsafe JS APIs.

Contributions welcome! :slightly_smiling_face:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Crashthatch picture Crashthatch  路  4Comments

gcanti picture gcanti  路  3Comments

steida picture steida  路  4Comments

mmkal picture mmkal  路  3Comments

bobaaaaa picture bobaaaaa  路  4Comments