Skip to content

Commit

Permalink
Zipper: FunctorWithIndex1
Browse files Browse the repository at this point in the history
  • Loading branch information
giogonzo authored and gcanti committed Jun 23, 2020
1 parent 849eb07 commit a508774
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
17 changes: 16 additions & 1 deletion docs/modules/Zipper.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Added in v0.1.6
- [length](#length)
- [make](#make)
- [map](#map)
- [mapWithIndex](#mapwithindex)
- [modify](#modify)
- [move](#move)
- [of](#of)
Expand Down Expand Up @@ -320,6 +321,16 @@ Added in v0.1.6

Added in v0.1.6

# mapWithIndex

**Signature**

```ts
<A, B>(f: (i: number, a: A) => B) => (fa: Zipper<A>) => Zipper<B>
```

Added in v0.1.17

# modify

Applies `f` to the focus and update with the result.
Expand Down Expand Up @@ -425,7 +436,11 @@ Added in v0.1.6
**Signature**

```ts
export const zipper: Applicative1<URI> & Foldable1<URI> & Traversable1<URI> & Comonad1<URI> = ...
export const zipper: Applicative1<URI> &
Foldable1<URI> &
Traversable1<URI> &
Comonad1<URI> &
FunctorWithIndex1<URI, number> = ...
```

Added in v0.1.6
25 changes: 21 additions & 4 deletions src/Zipper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Show } from 'fp-ts/lib/Show'
import { pipe, pipeable } from 'fp-ts/lib/pipeable'
import { Foldable1 } from 'fp-ts/lib/Foldable'
import { Traversable1 } from 'fp-ts/lib/Traversable'
import { FunctorWithIndex1 } from 'fp-ts/lib/FunctorWithIndex'

declare module 'fp-ts/lib/HKT' {
interface URItoKind<A> {
Expand Down Expand Up @@ -268,7 +269,11 @@ export function getMonoid<A>(M: Monoid<A>): Monoid<Zipper<A>> {
/**
* @since 0.1.6
*/
export const zipper: Applicative1<URI> & Foldable1<URI> & Traversable1<URI> & Comonad1<URI> = {
export const zipper: Applicative1<URI> &
Foldable1<URI> &
Traversable1<URI> &
Comonad1<URI> &
FunctorWithIndex1<URI, number> = {
URI,
map: (z, f) => make(z.lefts.map(f), f(z.focus), z.rights.map(f)),
of,
Expand All @@ -295,10 +300,18 @@ export const zipper: Applicative1<URI> & Foldable1<URI> & Traversable1<URI> & Co
return M.concat(M.concat(lefts, f(fa.focus)), rights)
},
traverse,
sequence
sequence,
mapWithIndex: (fa, f) => {
const l = fa.lefts.length
return make(
fa.lefts.map((a, i) => f(i, a)),
f(l, fa.focus),
fa.rights.map((a, i) => f(l + 1 + i, a))
)
}
}

const { ap, apFirst, apSecond, duplicate, extend, foldMap, map, reduce, reduceRight } = pipeable(zipper)
const { ap, apFirst, apSecond, duplicate, extend, foldMap, map, reduce, reduceRight, mapWithIndex } = pipeable(zipper)

export {
/**
Expand Down Expand Up @@ -336,5 +349,9 @@ export {
/**
* @since 0.1.6
*/
reduceRight
reduceRight,
/**
* @since 0.1.17
*/
mapWithIndex
}
8 changes: 8 additions & 0 deletions test/Zipper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,12 @@ describe('Zipper', () => {
)
)
})

it('mapWithIndex', () => {
const fa = Z.make(['a', 'b'], 'c', ['d'])
assert.deepStrictEqual(
Z.zipper.mapWithIndex(fa, (i, a) => a + i),
Z.make(['a0', 'b1'], 'c2', ['d3'])
)
})
})

0 comments on commit a508774

Please sign in to comment.