gcanti / fp-ts

Functional programming in TypeScript
https://gcanti.github.io/fp-ts/
MIT License
10.66k stars 503 forks source link

Support for primitive types? #1137

Open ryota-ka opened 4 years ago

ryota-ka commented 4 years ago

🚀 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 -
filipekiss commented 4 years ago

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.

raveclassic commented 4 years ago

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

christianbradley commented 4 years ago

@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> => ...
filipekiss commented 4 years ago

@christianbradley that would be an amazing improvement!

waynevanson commented 4 years ago

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.

samhh commented 3 years ago

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: