Skip to content

Commit

Permalink
docs: add example to TaskEither.getApplicativeTaskValidation
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Sep 8, 2022
1 parent 049312b commit ed2b205
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 23 deletions.
4 changes: 2 additions & 2 deletions docs/modules/Either.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ Added in v2.0.0
## getAltValidation
The default [`Alt`](#alt) instance returns the last error, if you want to
get all errors you need to provide an way to concatenate them via a `Semigroup`.
get all errors you need to provide a way to concatenate them via a `Semigroup`.
**Signature**
Expand Down Expand Up @@ -1239,7 +1239,7 @@ Added in v2.7.0
## getApplicativeValidation
The default [`Applicative`](#applicative) instance returns the first error, if you want to
get all errors you need to provide an way to concatenate them via a `Semigroup`.
get all errors you need to provide a way to concatenate them via a `Semigroup`.
**Signature**
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/IOEither.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ Added in v2.0.0
## getAltIOValidation
The default [`Alt`](#alt) instance returns the last error, if you want to
get all errors you need to provide an way to concatenate them via a `Semigroup`.
get all errors you need to provide a way to concatenate them via a `Semigroup`.
See [`getAltValidation`](./Either.ts.html#getaltvalidation).
Expand All @@ -1005,7 +1005,7 @@ Added in v2.7.0
## getApplicativeIOValidation

The default [`ApplicativePar`](#applicativepar) instance returns the first error, if you want to
get all errors you need to provide an way to concatenate them via a `Semigroup`.
get all errors you need to provide a way to concatenate them via a `Semigroup`.

See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).

Expand Down
4 changes: 2 additions & 2 deletions docs/modules/ReaderEither.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ Added in v2.0.0
## getAltReaderValidation
The default [`Alt`](#alt) instance returns the last error, if you want to
get all errors you need to provide an way to concatenate them via a `Semigroup`.
get all errors you need to provide a way to concatenate them via a `Semigroup`.
See [`getAltValidation`](./Either.ts.html#getaltvalidation).
Expand All @@ -1104,7 +1104,7 @@ Added in v2.7.0
## getApplicativeReaderValidation

The default [`Applicative`](#applicative) instance returns the first error, if you want to
get all errors you need to provide an way to concatenate them via a `Semigroup`.
get all errors you need to provide a way to concatenate them via a `Semigroup`.

See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).

Expand Down
4 changes: 2 additions & 2 deletions docs/modules/ReaderTaskEither.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,7 @@ Added in v2.0.0
## getAltReaderTaskValidation
The default [`Alt`](#alt) instance returns the last error, if you want to
get all errors you need to provide an way to concatenate them via a `Semigroup`.
get all errors you need to provide a way to concatenate them via a `Semigroup`.
See [`getAltValidation`](./Either.ts.html#getaltvalidation).
Expand All @@ -1615,7 +1615,7 @@ Added in v2.7.0
## getApplicativeReaderTaskValidation

The default [`ApplicativePar`](#applicativepar) instance returns the first error, if you want to
get all errors you need to provide an way to concatenate them via a `Semigroup`.
get all errors you need to provide a way to concatenate them via a `Semigroup`.

See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).

Expand Down
52 changes: 48 additions & 4 deletions docs/modules/TaskEither.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ Added in v2.0.0
## getAltTaskValidation
The default [`Alt`](#alt) instance returns the last error, if you want to
get all errors you need to provide an way to concatenate them via a `Semigroup`.
get all errors you need to provide a way to concatenate them via a `Semigroup`.
See [`getAltValidation`](./Either.ts.html#getaltvalidation).
Expand All @@ -1268,16 +1268,60 @@ Added in v2.7.0
## getApplicativeTaskValidation

The default [`ApplicativePar`](#applicativepar) instance returns the first error, if you want to
get all errors you need to provide an way to concatenate them via a `Semigroup`.

See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).
get all errors you need to provide a way to concatenate them via a `Semigroup`.

**Signature**

```ts
export declare function getApplicativeTaskValidation<E>(A: Apply1<T.URI>, S: Semigroup<E>): Applicative2C<URI, E>
```

**Example**

```ts
import * as E from 'fp-ts/Either'
import { pipe } from 'fp-ts/function'
import * as RA from 'fp-ts/ReadonlyArray'
import * as S from 'fp-ts/Semigroup'
import * as string from 'fp-ts/string'
import * as T from 'fp-ts/Task'
import * as TE from 'fp-ts/TaskEither'
interface User {
readonly id: string
readonly name: string
}
const remoteDatabase: ReadonlyArray<User> = [
{ id: 'id1', name: 'John' },
{ id: 'id2', name: 'Mary' },
{ id: 'id3', name: 'Joey' },
]
const fetchUser = (id: string): TE.TaskEither<string, User> =>
pipe(
remoteDatabase,
RA.findFirst((user) => user.id === id),
TE.fromOption(() => `${id} not found`)
)
async function test() {
assert.deepStrictEqual(
await pipe(['id4', 'id5'], RA.traverse(TE.ApplicativePar)(fetchUser))(),
E.left('id4 not found') // <= first error
)
const Applicative = TE.getApplicativeTaskValidation(T.ApplyPar, pipe(string.Semigroup, S.intercalate(', ')))
assert.deepStrictEqual(
await pipe(['id4', 'id5'], RA.traverse(Applicative)(fetchUser))(),
E.left('id4 not found, id5 not found') // <= all errors
)
}
test()
```

Added in v2.7.0

## getCompactable
Expand Down
4 changes: 2 additions & 2 deletions src/Either.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ export const getWitherable = <E>(M: Monoid<E>): Witherable2C<URI, E> => {

/**
* The default [`Applicative`](#applicative) instance returns the first error, if you want to
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
* get all errors you need to provide a way to concatenate them via a `Semigroup`.
*
* @example
* import * as A from 'fp-ts/Apply'
Expand Down Expand Up @@ -415,7 +415,7 @@ export const getApplicativeValidation = <E>(SE: Semigroup<E>): Applicative2C<URI

/**
* The default [`Alt`](#alt) instance returns the last error, if you want to
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
* get all errors you need to provide a way to concatenate them via a `Semigroup`.
*
* @example
* import * as E from 'fp-ts/Either'
Expand Down
4 changes: 2 additions & 2 deletions src/IOEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ declare module './HKT' {

/**
* The default [`ApplicativePar`](#applicativepar) instance returns the first error, if you want to
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
* get all errors you need to provide a way to concatenate them via a `Semigroup`.
*
* See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).
*
Expand All @@ -491,7 +491,7 @@ export function getApplicativeIOValidation<E>(S: Semigroup<E>): Applicative2C<UR

/**
* The default [`Alt`](#alt) instance returns the last error, if you want to
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
* get all errors you need to provide a way to concatenate them via a `Semigroup`.
*
* See [`getAltValidation`](./Either.ts.html#getaltvalidation).
*
Expand Down
4 changes: 2 additions & 2 deletions src/ReaderEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ export function getFilterable<E>(M: Monoid<E>): Filterable3C<URI, E> {

/**
* The default [`Applicative`](#applicative) instance returns the first error, if you want to
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
* get all errors you need to provide a way to concatenate them via a `Semigroup`.
*
* See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).
*
Expand All @@ -536,7 +536,7 @@ export function getApplicativeReaderValidation<E>(S: Semigroup<E>): Applicative3

/**
* The default [`Alt`](#alt) instance returns the last error, if you want to
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
* get all errors you need to provide a way to concatenate them via a `Semigroup`.
*
* See [`getAltValidation`](./Either.ts.html#getaltvalidation).
*
Expand Down
4 changes: 2 additions & 2 deletions src/ReaderTaskEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ export function getFilterable<E>(M: Monoid<E>): Filterable3C<URI, E> {

/**
* The default [`ApplicativePar`](#applicativepar) instance returns the first error, if you want to
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
* get all errors you need to provide a way to concatenate them via a `Semigroup`.
*
* See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).
*
Expand All @@ -804,7 +804,7 @@ export function getApplicativeReaderTaskValidation<E>(A: Apply1<T.URI>, S: Semig

/**
* The default [`Alt`](#alt) instance returns the last error, if you want to
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
* get all errors you need to provide a way to concatenate them via a `Semigroup`.
*
* See [`getAltValidation`](./Either.ts.html#getaltvalidation).
*
Expand Down
50 changes: 47 additions & 3 deletions src/TaskEither.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,9 +682,53 @@ declare module './HKT' {

/**
* The default [`ApplicativePar`](#applicativepar) instance returns the first error, if you want to
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
* get all errors you need to provide a way to concatenate them via a `Semigroup`.
*
* See [`getApplicativeValidation`](./Either.ts.html#getapplicativevalidation).
* @example
* import * as E from 'fp-ts/Either'
* import { pipe } from 'fp-ts/function'
* import * as RA from 'fp-ts/ReadonlyArray'
* import * as S from 'fp-ts/Semigroup'
* import * as string from 'fp-ts/string'
* import * as T from 'fp-ts/Task'
* import * as TE from 'fp-ts/TaskEither'
*
* interface User {
* readonly id: string
* readonly name: string
* }
*
* const remoteDatabase: ReadonlyArray<User> = [
* { id: 'id1', name: 'John' },
* { id: 'id2', name: 'Mary' },
* { id: 'id3', name: 'Joey' }
* ]
*
* const fetchUser = (id: string): TE.TaskEither<string, User> =>
* pipe(
* remoteDatabase,
* RA.findFirst((user) => user.id === id),
* TE.fromOption(() => `${id} not found`)
* )
*
* async function test() {
* assert.deepStrictEqual(
* await pipe(['id4', 'id5'], RA.traverse(TE.ApplicativePar)(fetchUser))(),
* E.left('id4 not found') // <= first error
* )
*
* const Applicative = TE.getApplicativeTaskValidation(
* T.ApplyPar,
* pipe(string.Semigroup, S.intercalate(', '))
* )
*
* assert.deepStrictEqual(
* await pipe(['id4', 'id5'], RA.traverse(Applicative)(fetchUser))(),
* E.left('id4 not found, id5 not found') // <= all errors
* )
* }
*
* test()
*
* @category instances
* @since 2.7.0
Expand All @@ -702,7 +746,7 @@ export function getApplicativeTaskValidation<E>(A: Apply1<T.URI>, S: Semigroup<E

/**
* The default [`Alt`](#alt) instance returns the last error, if you want to
* get all errors you need to provide an way to concatenate them via a `Semigroup`.
* get all errors you need to provide a way to concatenate them via a `Semigroup`.
*
* See [`getAltValidation`](./Either.ts.html#getaltvalidation).
*
Expand Down

0 comments on commit ed2b205

Please sign in to comment.