项目作者: nythrox

项目描述 :
Pointfree type-safe functional programming library for TypeScript - with do notation, HKTs, generic lifts and more
高级语言: TypeScript
项目地址: git://github.com/nythrox/purifree.git
创建时间: 2020-09-15T13:07:43Z
项目社区:https://github.com/nythrox/purifree

开源协议:ISC License

下载



Purify logo
</h3



Build Status
Built with Typescript

Purifree

Purifree is a fork from Purify that allows you to program in a point-free style, and adds a few new capabilities.

What is Purify?

Purify is a library for functional programming in TypeScript.
Its purpose is to allow developers to use popular patterns and abstractions that are available in most functional languages.
Learn more about Purify here

How to start?

Purifree is available as a package on npm. You can install it with a package manager of your choice:

  1. $ npm install purifree-ts

Purifree compatability

Purifree is 100% compatible with purify, and can be used interchangeably.

Point-free style

Point-free functions can be used with any ADTs (without needing module-specific imports), and can also be used together with the chainable (purify) API.

  1. // pointfree: Maybe<string>
  2. const pointfree = pipe(
  3. Just('name'),
  4. map((name) => name.toUpperCase()),
  5. filter((name) => name.length > 5),
  6. chain((name) => (Math.random() > 0.5 ? Just(name + ' lucky :)') : Nothing))
  7. )
  8. // matchTest: string
  9. const matchTest = pipe(
  10. Right<number, string>(100),
  11. chain((num) => (num > 50 ? Right(num) : Left(`bad number: ${num}`))),
  12. match({
  13. Right: (e) => 'Great number!' + e,
  14. Left: (e) => `OK number. | msg: (${e})`
  15. })
  16. )

Do* notation

This fork features the generator do* notation for all data structures except for arrays.
The do notation lets you easily chain operations without having to nest your code.

  1. // result: Either<Error, { name: string, surname: string, favoriteColor: string }>
  2. const result = Do(function* () {
  3. // name: string
  4. const name = yield* Right("name")
  5. // surname: string
  6. const surname = yield* Right("surname")
  7. // favoriteColor: string
  8. const favoriteColor = yield* Left<Error, string>(Error("DB error!"))
  9. return {
  10. name,
  11. surname,
  12. favoriteColor
  13. }
  14. })

Chain version equivalent:

  1. // result: Either<Error, { name: string, surname: string, favoriteColor: string }>
  2. const result = Right<string, Error>('name').chain((name) =>
  3. Right<string, Error>('surname').chain((surname) =>
  4. Left<Error, string>(Error('DB error!')).map((favoriteColor) => ({
  5. name,
  6. surname,
  7. favoriteColor
  8. }))
  9. )
  10. )

Traverse, Sequence, SequenceS, SequenceT

  1. // Gets an Either<never, number>, maps it to an Either<never, NonEmptyList<number>>, and inverts it into a NonEmptyList<Either<never, number>>
  2. // traverseTest: NonEmptyList<Either<never, number>>
  3. const traverseTest = pipe(
  4. Right(1),
  5. traverse(NonEmptyList, (num) => NonEmptyList(num))
  6. )
  7. // Gets an Either<never, NonEmptyList<number>> and inverts it into a NonEmptyList<Either<never, number>>
  8. // sequenceTest: NonEmptyList<Either<never, number>>
  9. const sequenceTest = pipe(
  10. Right(NonEmptyList(1)),
  11. sequence(NonEmptyList)
  12. )
  13. // sequenceTTest: Either<never, [number, string, boolean]>
  14. const sequenceTTest = sequenceT(Either.of)(Right(2), Right('name'), Right(true))
  15. // sequenceStrutureTest: Either<string, { name: string, age: number }>
  16. const sequenceStrutureTest = sequenceS(Either.of)({
  17. name: Right<string, string>('name'),
  18. age: Right<number, string>(100)
  19. })

Kleisli

The function pipeK can be used as an easy way to combine functions that return monads without using chain.
If you need to use a long list of chains, you can use the pipeK function to compose the functions instead of passing each one into chain.
Instead of:

  1. const getNameTest = pipe(
  2. chain((name?: string) => name ? Just(name) : Nothing),
  3. chain((name) => Just(name.toUpperCase())),
  4. chain((uppercasedName) => uppercasedName.length > 3 ? Just(uppercasedName) : Nothing)
  5. )

Use:

  1. // getNameTest: ( name?: string ) => Maybe<string>
  2. const getNameTest = kleisli(
  3. (name?: string) => name ? Just(name) : Nothing,
  4. (name) => Just(name.toUpperCase()),
  5. (uppercasedName) => uppercasedName.length > 3 ? Just(uppercasedName) : Nothing
  6. )
  7. // result: Maybe<string>
  8. const result = getNameTest('name')

Lifting

You can use the liftN family of functions to lift a function that takes normal values into a function that takes and returns elevated values.
WARNING: If you try lifting a function that uses generics, it will probably loose its type due to typescript’s limitations.

  1. // add takes normal values
  2. const add = (num1: number, num2: number) => num1 + num2
  3. // addL takes elevated values, and returns an elevated value
  4. // addL: Lifted<(a: Ap<number>, b: Ap<number>) => Ap<number>>
  5. const addL = lift2(add)
  6. // add5Option (b: Either<never, number>) => Either<never, number>
  7. const add5Option = addL(Right(5))
  8. // result: Either<never, number> = Right(15)
  9. const result = add5Option(Right(10))

Node for Codec

The Codec module’s function map is re-exported as Codec.map

Codesandbox

You can try it out in the browser.