Skip to content

Commit

Permalink
Add A.filterMap (alias A.keepMap)
Browse files Browse the repository at this point in the history
  • Loading branch information
mobily committed Dec 22, 2021
1 parent 0516b9e commit 3e1458b
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
45 changes: 45 additions & 0 deletions __tests__/Array/filterMap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { expectType } from 'ts-expect'

import { A, O, pipe } from '../..'

describe('filterMap', () => {
it('provides correct types', () => {
expectType<ReadonlyArray<string>>(
A.filterMap(['', 'hello', 'world'], value => {
return value.length ? O.Some(value) : O.None
}),
)
})

it('*', () => {
expect(
A.filterMap(['', 'hello', 'world', ''], value => {
return value.length > 0 ? O.Some(value.length) : O.None
}),
).toEqual([5, 5])
})
})

describe('filterMap (pipe)', () => {
it('provides correct types', () => {
expectType<ReadonlyArray<number>>(
pipe(
[1, 2, 3, 4, 5],
A.filterMap(value => {
return value % 2 === 0 ? O.Some(value * 2) : O.None
}),
),
)
})

it('*', () => {
expect(
pipe(
[1, 2, 3, 4, 5],
A.filterMap(value => {
return value % 2 === 0 ? O.Some(value * 2) : O.None
}),
),
).toEqual([4, 8])
})
})
53 changes: 53 additions & 0 deletions docs/api/generated/_array.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,29 @@ pipe(
) // → ['wo', 'rld']
```

### filterMap

Returns a new array that keep all elements that return `Some(value)` applied within `predicateFn`.


```ts
function filterMap<A, B>(xs: ReadonlyArray<A>, predicateFn: (_1: A) => Option<B>): ReadonlyArray<B>
function filterMap<A, B>(predicateFn: (_1: A) => Option<B>): (xs: ReadonlyArray<A>) => ReadonlyArray<B>
```

```ts
A.filterMap(['', 'hello', 'world', ''], value => {
return value.length > 0 ? O.Some(value.length) : O.None
}) // → [5, 5]
pipe(
[1, 2, 3, 4, 5],
A.filterMap(value => {
return value % 2 === 0 ? O.Some(value * 2) : O.None
}),
) // → [4, 8]
```

### filterWithIndex

Returns a new array that keep all elements satisfy the given predicate (which take two arguments: the element for the array and its index).
Expand Down Expand Up @@ -364,6 +387,36 @@ function join<A>(xs: ReadonlyArray<A>, delimiter: string): string
function join<A>(delimiter: string): (xs: ReadonlyArray<A>) => string
```

### keep

Alias for `filter`.


```ts
function keep<A>(xs: ReadonlyArray<A>, predicateFn: (_1: A) => boolean): ReadonlyArray<A>
function keep<A>(predicateFn: (_1: A) => boolean): (xs: ReadonlyArray<A>) => ReadonlyArray<A>
```

### keepMap

Alias for `filterMap`.


```ts
function keepMap<A, B>(xs: ReadonlyArray<A>, predicateFn: (_1: A) => Option<B>): ReadonlyArray<B>
function keepMap<A, B>(predicateFn: (_1: A) => Option<B>): (xs: ReadonlyArray<A>) => ReadonlyArray<B>
```

### keepWithIndex

Alias for `filterWithIndex`.


```ts
function keepWithIndex<A>(xs: ReadonlyArray<A>, predicateFn: (_1: number, _2: A) => boolean): ReadonlyArray<A>
function keepWithIndex<A>(predicateFn: (_1: number, _2: A) => boolean): (xs: ReadonlyArray<A>) => ReadonlyArray<A>
```

### last

Returns the last element (`Some(value)`) in the array, or `None` if the given array is empty.
Expand Down
1 change: 1 addition & 0 deletions docs/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ title: Changelog
- ✨ added `D.isEmpty`
- ✨ added `D.isNotEmpty`
- ✨ added `A.reduceReverse`
- ✨ added `A.filterMap` (alias: `A.keepMap`)

### `v3.4.1`

Expand Down
18 changes: 18 additions & 0 deletions src/Array/Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ let filter = (xs, predicateFn) => {
arr
}

%comment("Alias for `filter`.")
@gentype
let keep = (xs, predicateFn) => filter(xs, predicateFn)

%comment(
"Returns a new array that keep all elements satisfy the given predicate (which take two arguments: the element for the array and its index)."
)
Expand All @@ -263,6 +267,10 @@ let filterWithIndex = (xs, predicateFn) => {
arr
}

%comment("Alias for `filterWithIndex`.")
@gentype
let keepWithIndex = (xs, predicateFn) => filterWithIndex(xs, predicateFn)

%comment(
"Returns a new array of elements from the provided array which do not satisfy the given predicate."
)
Expand Down Expand Up @@ -578,3 +586,13 @@ let flip = xs => {
let (fst, snd) = xs
(snd, fst)
}

%comment(
"Returns a new array that keep all elements that return `Some(value)` applied within `predicateFn`."
)
@gentype
let filterMap = (xs, predicateFn) => Belt.Array.keepMapU(xs, predicateFn)

%comment("Alias for `filterMap`.")
@gentype
let keepMap = (xs, predicateFn) => filterMap(xs, predicateFn)
44 changes: 44 additions & 0 deletions src/Array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,17 @@ export declare function filter<A>(
predicateFn: (_1: A) => boolean,
): (xs: ReadonlyArray<A>) => ReadonlyArray<A>

/** Alias for `filter`. */

export declare function keep<A>(
xs: ReadonlyArray<A>,
predicateFn: (_1: A) => boolean,
): ReadonlyArray<A>

export declare function keep<A>(
predicateFn: (_1: A) => boolean,
): (xs: ReadonlyArray<A>) => ReadonlyArray<A>

/** Returns a new array that keep all elements satisfy the given predicate (which take two arguments: the element for the array and its index). */

export declare function filterWithIndex<A>(
Expand All @@ -283,6 +294,17 @@ export declare function filterWithIndex<A>(
predicateFn: (_1: number, _2: A) => boolean,
): (xs: ReadonlyArray<A>) => ReadonlyArray<A>

/** Alias for `filterWithIndex`. */

export declare function keepWithIndex<A>(
xs: ReadonlyArray<A>,
predicateFn: (_1: number, _2: A) => boolean,
): ReadonlyArray<A>

export declare function keepWithIndex<A>(
predicateFn: (_1: number, _2: A) => boolean,
): (xs: ReadonlyArray<A>) => ReadonlyArray<A>

/** Returns a new array of elements from the provided array which do not satisfy the given predicate. */

export declare function reject<A>(
Expand Down Expand Up @@ -707,3 +729,25 @@ export declare function tap<A>(
/** Returns a new tuple with the reverse order of the elements. */

export declare function flip<A, B>(xs: readonly [A, B]): readonly [B, A]

/** Returns a new array that keep all elements that return `Some(value)` applied within `predicateFn`. */

export declare function filterMap<A, B>(
xs: ReadonlyArray<A>,
predicateFn: (_1: A) => Option<B>,
): ReadonlyArray<B>

export declare function filterMap<A, B>(
predicateFn: (_1: A) => Option<B>,
): (xs: ReadonlyArray<A>) => ReadonlyArray<B>

/** Alias for `filterMap`. */

export declare function keepMap<A, B>(
xs: ReadonlyArray<A>,
predicateFn: (_1: A) => Option<B>,
): ReadonlyArray<B>

export declare function keepMap<A, B>(
predicateFn: (_1: A) => Option<B>,
): (xs: ReadonlyArray<A>) => ReadonlyArray<B>

0 comments on commit 3e1458b

Please sign in to comment.