Skip to content

Commit

Permalink
make Array.separate, Array.getRights, Array.getLefts, `Array.ge…
Browse files Browse the repository at this point in the history
…tSomes` heterogeneous (#2744)

Co-authored-by: maksim.khramtsov <maksim.khramtsov@btsdigital.kz>
  • Loading branch information
KhraksMamtsov and maksim.khramtsov authored May 23, 2024
1 parent 1262e7b commit a44e532
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-drinks-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

make `Array.separate`, `Array.getRights`, `Array.getLefts`, `Array.getSomes` heterogeneous
48 changes: 48 additions & 0 deletions packages/effect/dtslint/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,12 @@ pipe(
// $ExpectType [unknown[], unknown[]]
Array.separate([])

// $ExpectType [never[], number[]]
Array.separate([Either.right(1)])

// $ExpectType [string[], never[]]
Array.separate([Either.left("a")])

// $ExpectType [string[], number[]]
Array.separate([Either.left("a"), Either.right(1)])

Expand All @@ -1239,13 +1245,25 @@ Array.separate(hole<Array<Either.Either<number, string>>>())
// $ExpectType [string[], number[]]
Array.separate(hole<Iterable<Either.Either<number, string>>>())

// $ExpectType [(string | Date)[], (number | boolean)[]]
Array.separate(hole<Iterable<Either.Either<number, string> | Either.Either<boolean, Date>>>())

// $ExpectType [(string | Date)[], (number | boolean)[]]
Array.separate(hole<Iterable<Either.Either<number, string>> | Iterable<Either.Either<boolean, Date>>>())

// -------------------------------------------------------------------------------------
// getRights
// -------------------------------------------------------------------------------------

// $ExpectType unknown[]
Array.getRights([])

// $ExpectType never[]
Array.getRights([Either.left("a")])

// $ExpectType number[]
Array.getRights([Either.right(1)])

// $ExpectType number[]
Array.getRights([Either.left("a"), Either.right(1)])

Expand All @@ -1255,13 +1273,25 @@ Array.getRights(hole<Array<Either.Either<number, string>>>())
// $ExpectType number[]
Array.getRights(hole<Iterable<Either.Either<number, string>>>())

// $ExpectType (number | boolean)[]
Array.getRights(hole<Iterable<Either.Either<number, string> | Either.Either<boolean, Date>>>())

// $ExpectType (number | boolean)[]
Array.getRights(hole<Iterable<Either.Either<number, string>> | Iterable<Either.Either<boolean, Date>>>())

// -------------------------------------------------------------------------------------
// getLefts
// -------------------------------------------------------------------------------------

// $ExpectType unknown[]
Array.getLefts([])

// $ExpectType string[]
Array.getLefts([Either.left("a")])

// $ExpectType never[]
Array.getLefts([Either.right(1)])

// $ExpectType string[]
Array.getLefts([Either.left("a"), Either.right(1)])

Expand All @@ -1271,13 +1301,25 @@ Array.getLefts(hole<Array<Either.Either<number, string>>>())
// $ExpectType string[]
Array.getLefts(hole<Iterable<Either.Either<number, string>>>())

// $ExpectType (string | Date)[]
Array.getLefts(hole<Iterable<Either.Either<number, string> | Either.Either<boolean, Date>>>())

// $ExpectType (string | Date)[]
Array.getLefts(hole<Iterable<Either.Either<number, string>> | Iterable<Either.Either<boolean, Date>>>())

// -------------------------------------------------------------------------------------
// getSomes
// -------------------------------------------------------------------------------------

// $ExpectType unknown[]
Array.getSomes([])

// $ExpectType never[]
Array.getSomes([Option.none()])

// $ExpectType number[]
Array.getSomes([Option.some(1)])

// $ExpectType number[]
Array.getSomes([Option.none(), Option.some(1)])

Expand All @@ -1286,3 +1328,9 @@ Array.getSomes(hole<Array<Option.Option<number>>>())

// $ExpectType number[]
Array.getSomes(hole<Iterable<Option.Option<number>>>())

// $ExpectType (string | number)[]
Array.getSomes(hole<Iterable<Option.Option<number> | Option.Option<string>>>())

// $ExpectType (string | number)[]
Array.getSomes(hole<Iterable<Option.Option<number>> | Iterable<Option.Option<string>>>())
19 changes: 13 additions & 6 deletions packages/effect/src/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2254,7 +2254,10 @@ export const partitionMap: {
* @category filtering
* @since 2.0.0
*/
export const getSomes: <A>(self: Iterable<Option<A>>) => Array<A> = filterMap(identity)

export const getSomes: <T extends Iterable<Option<X>>, X = any>(
self: T
) => Array<Option.Value<ReadonlyArray.Infer<T>>> = filterMap(identity as any)

/**
* Retrieves the `Left` values from an `Iterable` of `Either`s, collecting them into an array.
Expand All @@ -2270,8 +2273,8 @@ export const getSomes: <A>(self: Iterable<Option<A>>) => Array<A> = filterMap(id
* @category filtering
* @since 2.0.0
*/
export const getLefts = <R, L>(self: Iterable<Either<R, L>>): Array<L> => {
const out: Array<L> = []
export const getLefts = <T extends Iterable<Either<any, any>>>(self: T): Array<Either.Left<ReadonlyArray.Infer<T>>> => {
const out: Array<any> = []
for (const a of self) {
if (E.isLeft(a)) {
out.push(a.left)
Expand All @@ -2295,8 +2298,10 @@ export const getLefts = <R, L>(self: Iterable<Either<R, L>>): Array<L> => {
* @category filtering
* @since 2.0.0
*/
export const getRights = <R, L>(self: Iterable<Either<R, L>>): Array<R> => {
const out: Array<R> = []
export const getRights = <T extends Iterable<Either<any, any>>>(
self: T
): Array<Either.Right<ReadonlyArray.Infer<T>>> => {
const out: Array<any> = []
for (const a of self) {
if (E.isRight(a)) {
out.push(a.right)
Expand Down Expand Up @@ -2377,7 +2382,9 @@ export const partition: {
* @category filtering
* @since 2.0.0
*/
export const separate: <R, L>(self: Iterable<Either<R, L>>) => [Array<L>, Array<R>] = partitionMap(
export const separate: <T extends Iterable<Either<any, any>>>(
self: T
) => [Array<Either.Left<ReadonlyArray.Infer<T>>>, Array<Either.Right<ReadonlyArray.Infer<T>>>] = partitionMap(
identity
)

Expand Down
4 changes: 2 additions & 2 deletions packages/effect/test/Array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -797,8 +797,8 @@ describe("ReadonlyArray", () => {

it("separate", () => {
expect(RA.separate([])).toEqual([[], []])
expect(RA.separate([E.right(1), E.left("e"), E.right(2)])).toEqual([
["e"],
expect(RA.separate([E.right(1), E.left("e"), E.left(2), E.right(2)])).toEqual([
["e", 2],
[1, 2]
])
})
Expand Down

0 comments on commit a44e532

Please sign in to comment.