Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filterCollect to Record, Map and variants #1604

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/modules/Map.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Added in v2.0.0
- [collect](#collect)
- [difference](#difference)
- [elem](#elem)
- [filterCollect](#filtercollect)
- [foldMap](#foldmap)
- [foldMapWithIndex](#foldmapwithindex)
- [intersection](#intersection)
Expand Down Expand Up @@ -537,6 +538,16 @@ export declare const elem: <A>(E: Eq<A>) => { (a: A): <K>(m: Map<K, A>) => boole

Added in v2.0.0

## filterCollect

**Signature**

```ts
export declare const filterCollect: <K>(O: Ord<K>) => <A, B>(f: (k: K, a: A) => O.Option<B>) => (m: Map<K, A>) => B[]
```

Added in v2.12.0

## foldMap

**Signature**
Expand Down
13 changes: 13 additions & 0 deletions docs/modules/ReadonlyMap.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Added in v2.5.0
- [difference](#difference)
- [elem](#elem)
- [empty](#empty)
- [filterCollect](#filtercollect)
- [foldMap](#foldmap)
- [foldMapWithIndex](#foldmapwithindex)
- [intersection](#intersection)
Expand Down Expand Up @@ -655,6 +656,18 @@ export declare const empty: ReadonlyMap<never, never>

Added in v2.5.0

## filterCollect

**Signature**

```ts
export declare function filterCollect<K>(
O: Ord<K>
): <A, B>(f: (k: K, a: A) => Option<B>) => (m: ReadonlyMap<K, A>) => ReadonlyArray<B>
```

Added in v2.12.0

## foldMap

**Signature**
Expand Down
35 changes: 35 additions & 0 deletions docs/modules/ReadonlyRecord.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Added in v2.5.0
- [elem](#elem)
- [empty](#empty)
- [every](#every)
- [filterCollect](#filtercollect)
- [filterWithIndex](#filterwithindex)
- [foldMapWithIndex](#foldmapwithindex)
- [fromFoldable](#fromfoldable)
Expand Down Expand Up @@ -792,6 +793,40 @@ export declare function every<A>(predicate: Predicate<A>): (r: ReadonlyRecord<st

Added in v2.5.0

## filterCollect

Map a `ReadonlyRecord` into a `ReadonlyArray` passing a key/value pair to the
iterating function and filtering out undesired results. The keys in the
resulting record are sorted according to the passed instance of
`Ord<string>`.

**Signature**

```ts
export declare function filterCollect(
O: Ord<string>
): <K extends string, A, B>(f: (k: K, a: A) => Option<B>) => (r: ReadonlyRecord<K, A>) => ReadonlyArray<B>
```

**Example**

```ts
import { none, some } from 'fp-ts/Option'
import { filterCollect } from 'fp-ts/ReadonlyRecord'
import { Ord } from 'fp-ts/string'

const x: { readonly a: string; readonly b: boolean; readonly c: number } = { a: 'c', b: false, c: 123 }
assert.deepStrictEqual(
filterCollect(Ord)((key, value) => (typeof value === 'boolean' ? none : some({ key, value })))(x),
[
{ key: 'a', value: 'c' },
{ key: 'c', value: 123 },
]
)
```

Added in v2.12.0

## filterWithIndex

**Signature**
Expand Down
34 changes: 34 additions & 0 deletions docs/modules/Record.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Added in v2.0.0
- [deleteAt](#deleteat)
- [elem](#elem)
- [every](#every)
- [filterCollect](#filtercollect)
- [filterMapWithIndex](#filtermapwithindex)
- [filterWithIndex](#filterwithindex)
- [foldMapWithIndex](#foldmapwithindex)
Expand Down Expand Up @@ -641,6 +642,39 @@ export declare const every: <A>(predicate: Predicate<A>) => (r: Record<string, A

Added in v2.0.0

## filterCollect

Map a `Record` into an `Array` passing a key/value pair to the iterating
function and filtering out undesired results. The keys in the resulting
record are sorted according to the passed instance of `Ord<string>`.

**Signature**

```ts
export declare const filterCollect: (
O: Ord<string>
) => <K extends string, A, B>(f: (k: K, a: A) => Option<B>) => (r: Record<K, A>) => B[]
```

**Example**

```ts
import { none, some } from 'fp-ts/Option'
import { filterCollect } from 'fp-ts/Record'
import { Ord } from 'fp-ts/string'

const x: { readonly a: string; readonly b: boolean; readonly c: number } = { a: 'c', b: false, c: 123 }
assert.deepStrictEqual(
filterCollect(Ord)((key, value) => (typeof value === 'boolean' ? none : some({ key, value })))(x),
[
{ key: 'a', value: 'c' },
{ key: 'c', value: 123 },
]
)
```

Added in v2.12.0

## filterMapWithIndex

**Signature**
Expand Down
7 changes: 7 additions & 0 deletions src/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ export function collect<K>(O: Ord<K>): <A, B>(f: (k: K, a: A) => B) => (m: Map<K
}
}

/**
* @since 2.12.0
*/
export const filterCollect: <K>(
O: Ord<K>
) => <A, B>(f: (k: K, a: A) => Option<B>) => (m: Map<K, A>) => Array<B> = RM.filterCollect as any
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if doing as any is a good idea. But it avoids duplication implementing the same function with a different signature.


/**
* Get a sorted `Array` of the key/value pairs contained in a `Map`.
*
Expand Down
19 changes: 19 additions & 0 deletions src/ReadonlyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,25 @@ export function collect<K>(O: Ord<K>): <A, B>(f: (k: K, a: A) => B) => (m: Reado
}
}

/**
* @since 2.12.0
*/
export function filterCollect<K>(
O: Ord<K>
): <A, B>(f: (k: K, a: A) => Option<B>) => (m: ReadonlyMap<K, A>) => ReadonlyArray<B> {
const keysO = keys(O)
return <A, B>(f: (k: K, a: A) => Option<B>) => (m: ReadonlyMap<K, A>): ReadonlyArray<B> => {
const out: Array<B> = []
for (const key of keysO(m)) {
const x = f(key, m.get(key)!)
if (_.isSome(x)) {
out.push(x.value)
}
}
return out
}
}

/**
* Get a sorted `ReadonlyArray` of the key/value pairs contained in a `ReadonlyMap`.
*
Expand Down
35 changes: 35 additions & 0 deletions src/ReadonlyRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,41 @@ export function collect<A, B>(
}
}

/**
* Map a `ReadonlyRecord` into a `ReadonlyArray` passing a key/value pair to the
* iterating function and filtering out undesired results. The keys in the
* resulting record are sorted according to the passed instance of
* `Ord<string>`.
*
* @example
* import { none, some } from 'fp-ts/Option'
* import { filterCollect } from 'fp-ts/ReadonlyRecord'
* import { Ord } from 'fp-ts/string'
*
* const x: { readonly a: string, readonly b: boolean, readonly c: number } = { a: 'c', b: false, c: 123 }
* assert.deepStrictEqual(
* filterCollect(Ord)((key, value) => typeof value === 'boolean' ? none : some({ key, value }))(x),
* [{ key: 'a', value: 'c' }, { key: 'c', value: 123 }]
* )
*
* @since 2.12.0
*/
export function filterCollect(
O: Ord<string>
): <K extends string, A, B>(f: (k: K, a: A) => Option<B>) => (r: ReadonlyRecord<K, A>) => ReadonlyArray<B> {
const keysO = keys_(O)
return <K extends string, A, B>(f: (k: K, a: A) => Option<B>) => (r: ReadonlyRecord<K, A>): ReadonlyArray<B> => {
const out: Array<B> = []
for (const key of keysO(r)) {
const x = f(key, r[key])
if (_.isSome(x)) {
out.push(x.value)
}
}
return out
}
}

/**
* Get a sorted `ReadonlyArray` of the key/value pairs contained in a `ReadonlyRecord`.
*
Expand Down
22 changes: 22 additions & 0 deletions src/Record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ export function collect<A, B>(
}
}

/**
* Map a `Record` into an `Array` passing a key/value pair to the iterating
* function and filtering out undesired results. The keys in the resulting
* record are sorted according to the passed instance of `Ord<string>`.
*
* @example
* import { none, some } from 'fp-ts/Option'
* import { filterCollect } from 'fp-ts/Record'
* import { Ord } from 'fp-ts/string'
*
* const x: { readonly a: string, readonly b: boolean, readonly c: number } = { a: 'c', b: false, c: 123 }
* assert.deepStrictEqual(
* filterCollect(Ord)((key, value) => typeof value === 'boolean' ? none : some({ key, value }))(x),
* [{ key: 'a', value: 'c' }, { key: 'c', value: 123 }]
* )
*
* @since 2.12.0
*/
export const filterCollect: (
O: Ord<string>
) => <K extends string, A, B>(f: (k: K, a: A) => Option<B>) => (r: Record<K, A>) => Array<B> = RR.filterCollect as any

/**
* Get a sorted `Array` of the key/value pairs contained in a `Record`.
*
Expand Down
55 changes: 55 additions & 0 deletions test/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,61 @@ describe('Map', () => {
)
})

it('filterCollect', () => {
const collectO = _.filterCollect(ordUser)
const f = (_k: User, a: number): O.Option<number> => (a % 2 === 0 ? O.none : O.some(a + 1))
U.deepStrictEqual(
collectO(f)(
new Map<User, number>([
[{ id: 'a' }, 1],
[{ id: 'b' }, 2],
[{ id: 'c' }, 3]
])
),
[2, 4]
)
U.deepStrictEqual(
collectO(f)(
new Map<User, number>([
[{ id: 'b' }, 2],
[{ id: 'a' }, 1],
[{ id: 'c' }, 3]
])
),
[2, 4]
)

const collect = _.filterCollect(ordKey)
const g = (k: Key, a: Value): O.Option<readonly [number, number]> =>
k.id % 2 === 0 ? O.some([k.id, a.value]) : O.none
U.deepStrictEqual(
collect(g)(
new Map([
[{ id: 1 }, { value: 1 }],
[{ id: 2 }, { value: 2 }],
[{ id: 4 }, { value: 4 }]
])
),
[
[4, 4],
[2, 2]
]
)
U.deepStrictEqual(
collect(g)(
new Map([
[{ id: 2 }, { value: 2 }],
[{ id: 1 }, { value: 1 }],
[{ id: 4 }, { value: 4 }]
])
),
[
[4, 4],
[2, 2]
]
)
})

it('toArray', () => {
const m1 = new Map<User, number>([
[{ id: 'a' }, 1],
Expand Down
55 changes: 55 additions & 0 deletions test/ReadonlyMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,61 @@ describe('ReadonlyMap', () => {
)
})

it('filterCollect', () => {
const collectO = _.filterCollect(ordUser)
const f = (_k: User, a: number): O.Option<number> => (a % 2 === 0 ? O.none : O.some(a + 1))
U.deepStrictEqual(
collectO(f)(
new Map<User, number>([
[{ id: 'a' }, 1],
[{ id: 'b' }, 2],
[{ id: 'c' }, 3]
])
),
[2, 4]
)
U.deepStrictEqual(
collectO(f)(
new Map<User, number>([
[{ id: 'b' }, 2],
[{ id: 'a' }, 1],
[{ id: 'c' }, 3]
])
),
[2, 4]
)

const collect = _.filterCollect(ordKey)
const g = (k: Key, a: Value): O.Option<readonly [number, number]> =>
k.id % 2 === 0 ? O.some([k.id, a.value]) : O.none
U.deepStrictEqual(
collect(g)(
new Map([
[{ id: 1 }, { value: 1 }],
[{ id: 2 }, { value: 2 }],
[{ id: 4 }, { value: 4 }]
])
),
[
[4, 4],
[2, 2]
]
)
U.deepStrictEqual(
collect(g)(
new Map([
[{ id: 2 }, { value: 2 }],
[{ id: 1 }, { value: 1 }],
[{ id: 4 }, { value: 4 }]
])
),
[
[4, 4],
[2, 2]
]
)
})

it('toReadonlyArray', () => {
const m1 = new Map<User, number>([
[{ id: 'a' }, 1],
Expand Down