Skip to content

Commit

Permalink
Cover other modify functions
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilkybarkid authored and gcanti committed Apr 5, 2022
1 parent 4d39acb commit 57d857e
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/modules/Iso.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ Added in v2.3.8
**Signature**

```ts
export declare const modify: <A>(f: (a: A) => A) => <S>(sa: Iso<S, A>) => (s: S) => S
export declare const modify: <A, B extends A = A>(f: (a: A) => B) => <S>(sa: Iso<S, A>) => (s: S) => S
```

Added in v2.3.0
Expand Down
6 changes: 4 additions & 2 deletions docs/modules/Optional.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ Added in v2.3.0
**Signature**

```ts
export declare const modify: <A>(f: (a: A) => A) => <S>(optional: Optional<S, A>) => (s: S) => S
export declare const modify: <A, B extends A = A>(f: (a: A) => B) => <S>(optional: Optional<S, A>) => (s: S) => S
```

Added in v2.3.0
Expand Down Expand Up @@ -252,7 +252,9 @@ Added in v2.3.5
**Signature**

```ts
export declare const modifyOption: <A>(f: (a: A) => A) => <S>(optional: Optional<S, A>) => (s: S) => O.Option<S>
export declare const modifyOption: <A, B extends A = A>(
f: (a: A) => B
) => <S>(optional: Optional<S, A>) => (s: S) => O.Option<S>
```

Added in v2.3.0
Expand Down
4 changes: 2 additions & 2 deletions docs/modules/Prism.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ Added in v2.3.0
**Signature**

```ts
export declare const modify: <A>(f: (a: A) => A) => <S>(sa: Prism<S, A>) => (s: S) => S
export declare const modify: <A, B extends A = A>(f: (a: A) => B) => <S>(sa: Prism<S, A>) => (s: S) => S
```

Added in v2.3.0
Expand Down Expand Up @@ -249,7 +249,7 @@ Added in v2.3.5
**Signature**

```ts
export declare const modifyOption: <A>(f: (a: A) => A) => <S>(sa: Prism<S, A>) => (s: S) => O.Option<S>
export declare const modifyOption: <A, B extends A = A>(f: (a: A) => B) => <S>(sa: Prism<S, A>) => (s: S) => O.Option<S>
```

Added in v2.3.0
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/Traversal.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ Added in v2.3.0
**Signature**

```ts
export declare const modify: <A>(f: (a: A) => A) => <S>(sa: Traversal<S, A>) => (s: S) => S
export declare const modify: <A, B extends A = A>(f: (a: A) => B) => <S>(sa: Traversal<S, A>) => (s: S) => S
```

Added in v2.3.0
Expand Down
25 changes: 25 additions & 0 deletions dtslint/ts3.5/Iso.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as _ from '../../src/Iso'
import { pipe } from 'fp-ts/lib/pipeable'

interface A {
a: string
b: number
c: string | boolean
}

declare const isoC: _.Iso<A, A['c']>

//
// modify
//

// $ExpectType (s: A) => A
pipe(isoC, _.modify((
a // $ExpectType string | boolean
) => a))

// $ExpectType (s: A) => A
pipe(isoC, _.modify<string | boolean>(() => 'foo'))

// $ExpectType (s: A) => A
pipe(isoC, _.modify(() => 'foo'))
8 changes: 5 additions & 3 deletions dtslint/ts3.5/Lens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@ interface A {
c: string | boolean
}

declare const lensC: L.Lens<A, A['c']>

//
// modify
//

// $ExpectType (s: A) => A
pipe(L.id<A>(), L.prop('c'), L.modify((
pipe(lensC, L.modify((
a // $ExpectType string | boolean
) => a))

// $ExpectType (s: A) => A
pipe(L.id<A>(), L.prop('c'), L.modify<string | boolean>(() => 'foo'))
pipe(lensC, L.modify<string | boolean>(() => 'foo'))

// $ExpectType (s: A) => A
pipe(L.id<A>(), L.prop('c'), L.modify(() => 'foo'))
pipe(lensC, L.modify(() => 'foo'))

//
// prop
Expand Down
34 changes: 33 additions & 1 deletion dtslint/ts3.5/Optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,41 @@ import { pipe } from 'fp-ts/lib/pipeable'
interface A {
a: string
b: number
c: boolean
c: string | boolean
}

declare const optionalC: O.Optional<A, A['c']>

//
// modifyOption
//

// $ExpectType (s: A) => Option<A>
pipe(optionalC, O.modifyOption((
a // $ExpectType string | boolean
) => a))

// $ExpectType (s: A) => Option<A>
pipe(optionalC, O.modifyOption<string | boolean>(() => 'foo'))

// $ExpectType (s: A) => Option<A>
pipe(optionalC, O.modifyOption(() => 'foo'))

//
// modify
//

// $ExpectType (s: A) => A
pipe(optionalC, O.modify((
a // $ExpectType string | boolean
) => a))

// $ExpectType (s: A) => A
pipe(optionalC, O.modify<string | boolean>(() => 'foo'))

// $ExpectType (s: A) => A
pipe(optionalC, O.modify(() => 'foo'))

// $ExpectError
pipe(O.id<A>(), O.props())
// $ExpectError
Expand Down
38 changes: 37 additions & 1 deletion dtslint/ts3.5/Prism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,45 @@ import { pipe } from 'fp-ts/lib/pipeable'
interface A {
a: string
b: number
c: boolean
c: string | boolean
}

declare const prismC: P.Prism<A, A['c']>

//
// modifyOption
//

// $ExpectType (s: A) => Option<A>
pipe(prismC, P.modifyOption((
a // $ExpectType string | boolean
) => a))

// $ExpectType (s: A) => Option<A>
pipe(prismC, P.modifyOption<string | boolean>(() => 'foo'))

// $ExpectType (s: A) => Option<A>
pipe(prismC, P.modifyOption(() => 'foo'))

//
// modify
//

// $ExpectType (s: A) => A
pipe(prismC, P.modify((
a // $ExpectType string | boolean
) => a))

// $ExpectType (s: A) => A
pipe(prismC, P.modify<string | boolean>(() => 'foo'))

// $ExpectType (s: A) => A
pipe(prismC, P.modify(() => 'foo'))

//
// props
//

// $ExpectError
pipe(P.id<A>(), P.props())
// $ExpectError
Expand Down
19 changes: 18 additions & 1 deletion dtslint/ts3.5/Traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,26 @@ import { pipe } from 'fp-ts/lib/pipeable'
interface A {
a: string
b: number
c: boolean
c: string | boolean
}

declare const traversalC: T.Traversal<A, A['c']>

//
// modify
//

// $ExpectType (s: A) => A
pipe(traversalC, T.modify((
a // $ExpectType string | boolean
) => a))

// $ExpectType (s: A) => A
pipe(traversalC, T.modify<string | boolean>(() => 'foo'))

// $ExpectType (s: A) => A
pipe(traversalC, T.modify(() => 'foo'))

// $ExpectError
pipe(T.id<A>(), T.props())
// $ExpectError
Expand Down
3 changes: 2 additions & 1 deletion src/Iso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ export const reverse = <S, A>(sa: Iso<S, A>): Iso<A, S> => iso(sa.reverseGet, sa
* @category combinators
* @since 2.3.0
*/
export const modify = <A>(f: (a: A) => A) => <S>(sa: Iso<S, A>) => (s: S): S => sa.reverseGet(f(sa.get(s)))
export const modify = <A, B extends A = A>(f: (a: A) => B) => <S>(sa: Iso<S, A>) => (s: S): S =>
sa.reverseGet(f(sa.get(s)))

/**
* @category combinators
Expand Down
8 changes: 5 additions & 3 deletions src/Optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ export const composeTraversal = <A, B>(ab: Traversal<A, B>): (<S>(sa: Optional<S
* @category combinators
* @since 2.3.0
*/
export const modifyOption: <A>(f: (a: A) => A) => <S>(optional: Optional<S, A>) => (s: S) => Option<S> =
_.optionalModifyOption
export const modifyOption: <A, B extends A = A>(
f: (a: A) => B
) => <S>(optional: Optional<S, A>) => (s: S) => Option<S> = _.optionalModifyOption

/**
* @category combinators
Expand All @@ -162,7 +163,8 @@ export const setOption = <A>(a: A): (<S>(optional: Optional<S, A>) => (s: S) =>
* @category combinators
* @since 2.3.0
*/
export const modify: <A>(f: (a: A) => A) => <S>(optional: Optional<S, A>) => (s: S) => S = _.optionalModify
export const modify: <A, B extends A = A>(f: (a: A) => B) => <S>(optional: Optional<S, A>) => (s: S) => S =
_.optionalModify

/**
* @category combinators
Expand Down
5 changes: 3 additions & 2 deletions src/Prism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,14 @@ export const set: <A>(a: A) => <S>(sa: Prism<S, A>) => (s: S) => S = _.prismSet
* @category combinators
* @since 2.3.0
*/
export const modifyOption: <A>(f: (a: A) => A) => <S>(sa: Prism<S, A>) => (s: S) => Option<S> = _.prismModifyOption
export const modifyOption: <A, B extends A = A>(f: (a: A) => B) => <S>(sa: Prism<S, A>) => (s: S) => Option<S> =
_.prismModifyOption

/**
* @category combinators
* @since 2.3.0
*/
export const modify: <A>(f: (a: A) => A) => <S>(sa: Prism<S, A>) => (s: S) => S = _.prismModify
export const modify: <A, B extends A = A>(f: (a: A) => B) => <S>(sa: Prism<S, A>) => (s: S) => S = _.prismModify

/**
* @category combinators
Expand Down
2 changes: 1 addition & 1 deletion src/Traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export const composeOptional: <A, B>(ab: Optional<A, B>) => <S>(sa: Traversal<S,
* @category combinators
* @since 2.3.0
*/
export const modify = <A>(f: (a: A) => A) => <S>(sa: Traversal<S, A>): ((s: S) => S) =>
export const modify = <A, B extends A = A>(f: (a: A) => B) => <S>(sa: Traversal<S, A>): ((s: S) => S) =>
sa.modifyF(_.ApplicativeIdentity)(f)

/**
Expand Down
10 changes: 6 additions & 4 deletions src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export const prismAsTraversal = <S, A>(sa: Prism<S, A>): Traversal<S, A> =>
)

/** @internal */
export const prismModifyOption = <A>(f: (a: A) => A) => <S>(sa: Prism<S, A>) => (s: S): O.Option<S> =>
export const prismModifyOption = <A, B extends A>(f: (a: A) => B) => <S>(sa: Prism<S, A>) => (s: S): O.Option<S> =>
pipe(
sa.getOption(s),
O.map((o) => {
Expand All @@ -179,7 +179,7 @@ export const prismModifyOption = <A>(f: (a: A) => A) => <S>(sa: Prism<S, A>) =>
)

/** @internal */
export const prismModify = <A>(f: (a: A) => A) => <S>(sa: Prism<S, A>): ((s: S) => S) => {
export const prismModify = <A, B extends A>(f: (a: A) => B) => <S>(sa: Prism<S, A>): ((s: S) => S) => {
const g = prismModifyOption(f)(sa)
return (s) =>
pipe(
Expand Down Expand Up @@ -238,7 +238,9 @@ export const optionalAsTraversal = <S, A>(sa: Optional<S, A>): Traversal<S, A> =
)

/** @internal */
export const optionalModifyOption = <A>(f: (a: A) => A) => <S>(optional: Optional<S, A>) => (s: S): O.Option<S> =>
export const optionalModifyOption = <A, B extends A>(f: (a: A) => B) => <S>(optional: Optional<S, A>) => (
s: S
): O.Option<S> =>
pipe(
optional.getOption(s),
O.map((a) => {
Expand All @@ -248,7 +250,7 @@ export const optionalModifyOption = <A>(f: (a: A) => A) => <S>(optional: Optiona
)

/** @internal */
export const optionalModify = <A>(f: (a: A) => A) => <S>(optional: Optional<S, A>): ((s: S) => S) => {
export const optionalModify = <A, B extends A>(f: (a: A) => B) => <S>(optional: Optional<S, A>): ((s: S) => S) => {
const g = optionalModifyOption(f)(optional)
return (s) =>
pipe(
Expand Down

0 comments on commit 57d857e

Please sign in to comment.