Skip to content

Commit d8d278b

Browse files
gcantimikearnaldi
authored andcommittedFeb 21, 2024
swap GroupBy type parameters from `GroupBy<out R, out E, out K, out… (#2169)
1 parent c05ab66 commit d8d278b

21 files changed

+101
-75
lines changed
 

‎.changeset/cyan-kangaroos-fix.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": minor
3+
---
4+
5+
swap `GroupBy` type parameters from `GroupBy<out R, out E, out K, out V>` to `GroupBy<out K, out V, out E = never, out R = never>`

‎packages/effect/mod.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import type k from "ast-types/gen/kinds.js"
22
import type cs from "jscodeshift"
33

44
const enabled = {
5-
swapChannelStateParams: true,
5+
swapGroupByParams: false,
6+
swapChannelStateParams: false,
67
swapScheduleParams: false,
78
swapEitherParams: false,
89
swapLayerParams: false,
@@ -113,6 +114,23 @@ const swapTExitParams = swapParamsEA("TExit")
113114
const swapEitherParams = swapParamsEA("Either")
114115
const swapChannelStateParams = swapParamsEA("ChannelState")
115116

117+
// from: GroupBy<out R, out E, out K, out V>
118+
// to: GroupBy<out K, out V, out E = never, out R = never>
119+
const swapGroupByParams = (ast: cs.ASTPath<cs.TSTypeReference>) => {
120+
const is = filter(ast, "GroupBy")
121+
if (
122+
is(ast.value.typeName) &&
123+
ast.value.typeParameters &&
124+
ast.value.typeParameters.params.length === 4
125+
) {
126+
const params = ast.value.typeParameters.params
127+
const newParams = [params[2], params[3], params[1], params[0]]
128+
popNever(newParams)
129+
popNever(newParams)
130+
ast.value.typeParameters.params = newParams
131+
}
132+
}
133+
116134
// from: Channel<out Env, in InErr, in InElem, in InDone, out OutErr, out OutElem, out OutDone>
117135
// to: Channel<OutElem, InElem = unknown, OutErr = never, InErr = unknown, OutDone = void, InDone = unknown, Env = never>
118136
const swapChannelParams = (ast: cs.ASTPath<cs.TSTypeReference>) => {
@@ -199,6 +217,9 @@ export default function transformer(file: cs.FileInfo, api: cs.API) {
199217
}
200218

201219
forEveryTypeReference(root, (ast) => {
220+
if (enabled.swapGroupByParams) {
221+
swapGroupByParams(ast)
222+
}
202223
if (enabled.swapChannelStateParams) {
203224
swapChannelStateParams(ast)
204225
}

‎packages/effect/src/Effect.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -780,11 +780,11 @@ export const reduceRight: {
780780
* @category collecting & elements
781781
*/
782782
export const reduceWhile: {
783-
<A, R, E, Z>(
783+
<Z, A, E, R>(
784784
zero: Z,
785785
options: { readonly while: Predicate<Z>; readonly body: (s: Z, a: A, i: number) => Effect<Z, E, R> }
786786
): (elements: Iterable<A>) => Effect<Z, E, R>
787-
<A, R, E, Z>(
787+
<A, Z, E, R>(
788788
elements: Iterable<A>,
789789
zero: Z,
790790
options: { readonly while: Predicate<Z>; readonly body: (s: Z, a: A, i: number) => Effect<Z, E, R> }
@@ -3574,13 +3574,13 @@ export const andThen: {
35743574
) => [X] extends [Effect<infer A1, infer E1, infer R1>] ? Effect<A1, E | E1, R | R1>
35753575
: [X] extends [Promise<infer A1>] ? Effect<A1, E | Cause.UnknownException, R>
35763576
: Effect<X, E, R>
3577-
<A, R, E, X>(
3577+
<A, E, R, X>(
35783578
self: Effect<A, E, R>,
35793579
f: (a: NoInfer<A>) => X
35803580
): [X] extends [Effect<infer A1, infer E1, infer R1>] ? Effect<A1, E | E1, R | R1>
35813581
: [X] extends [Promise<infer A1>] ? Effect<A1, E | Cause.UnknownException, R>
35823582
: Effect<X, E, R>
3583-
<A, R, E, X>(
3583+
<A, E, R, X>(
35843584
self: Effect<A, E, R>,
35853585
f: X
35863586
): [X] extends [Effect<infer A1, infer E1, infer R1>] ? Effect<A1, E | E1, R | R1>

‎packages/effect/src/GroupBy.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export type GroupByTypeId = typeof GroupByTypeId
2929
* @since 2.0.0
3030
* @category models
3131
*/
32-
export interface GroupBy<out R, out E, out K, out V> extends GroupBy.Variance<R, E, K, V>, Pipeable {
32+
export interface GroupBy<out K, out V, out E = never, out R = never> extends GroupBy.Variance<K, V, E, R>, Pipeable {
3333
readonly grouped: Stream.Stream<readonly [K, Queue.Dequeue<Take.Take<V, E>>], E, R>
3434
}
3535

@@ -41,12 +41,12 @@ export declare namespace GroupBy {
4141
* @since 2.0.0
4242
* @category models
4343
*/
44-
export interface Variance<out R, out E, out K, out V> {
44+
export interface Variance<out K, out V, out E, out R> {
4545
readonly [GroupByTypeId]: {
46-
readonly _R: Covariant<R>
47-
readonly _E: Covariant<E>
4846
readonly _K: Covariant<K>
4947
readonly _V: Covariant<V>
48+
readonly _E: Covariant<E>
49+
readonly _R: Covariant<R>
5050
}
5151
}
5252
}
@@ -62,9 +62,9 @@ export const evaluate: {
6262
<K, V, E, A, E2, R2>(
6363
f: (key: K, stream: Stream.Stream<V, E, never>) => Stream.Stream<A, E2, R2>,
6464
options?: { readonly bufferSize?: number | undefined } | undefined
65-
): <R>(self: GroupBy<R, E, K, V>) => Stream.Stream<A, E | E2, R2 | R>
66-
<R, E, K, V, A, E2, R2>(
67-
self: GroupBy<R, E, K, V>,
65+
): <R>(self: GroupBy<K, V, E, R>) => Stream.Stream<A, E | E2, R2 | R>
66+
<K, V, E, R, A, E2, R2>(
67+
self: GroupBy<K, V, E, R>,
6868
f: (key: K, stream: Stream.Stream<V, E, never>) => Stream.Stream<A, E2, R2>,
6969
options?: { readonly bufferSize?: number | undefined } | undefined
7070
): Stream.Stream<A, E | E2, R | R2>
@@ -77,8 +77,8 @@ export const evaluate: {
7777
* @category utils
7878
*/
7979
export const filter: {
80-
<K>(predicate: Predicate<NoInfer<K>>): <R, E, V>(self: GroupBy<R, E, K, V>) => GroupBy<R, E, K, V>
81-
<R, E, V, K>(self: GroupBy<R, E, K, V>, predicate: Predicate<K>): GroupBy<R, E, K, V>
80+
<K>(predicate: Predicate<NoInfer<K>>): <V, E, R>(self: GroupBy<K, V, E, R>) => GroupBy<K, V, E, R>
81+
<K, V, E, R>(self: GroupBy<K, V, E, R>, predicate: Predicate<K>): GroupBy<K, V, E, R>
8282
} = internal.filter
8383

8484
/**
@@ -88,8 +88,8 @@ export const filter: {
8888
* @category utils
8989
*/
9090
export const first: {
91-
(n: number): <R, E, K, V>(self: GroupBy<R, E, K, V>) => GroupBy<R, E, K, V>
92-
<R, E, K, V>(self: GroupBy<R, E, K, V>, n: number): GroupBy<R, E, K, V>
91+
(n: number): <K, V, E, R>(self: GroupBy<K, V, E, R>) => GroupBy<K, V, E, R>
92+
<K, V, E, R>(self: GroupBy<K, V, E, R>, n: number): GroupBy<K, V, E, R>
9393
} = internal.first
9494

9595
/**
@@ -98,6 +98,6 @@ export const first: {
9898
* @since 2.0.0
9999
* @category constructors
100100
*/
101-
export const make: <R, E, K, V>(
101+
export const make: <K, V, E, R>(
102102
grouped: Stream.Stream<readonly [K, Queue.Dequeue<Take.Take<V, E>>], E, R>
103-
) => GroupBy<R, E, K, V> = internal.make
103+
) => GroupBy<K, V, E, R> = internal.make

‎packages/effect/src/MergeDecision.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const Await: <R, E0, Z0, E, Z>(
6161
* @since 2.0.0
6262
* @category constructors
6363
*/
64-
export const AwaitConst: <R, E, Z>(effect: Effect.Effect<Z, E, R>) => MergeDecision<R, unknown, unknown, E, Z> =
64+
export const AwaitConst: <Z, E, R>(effect: Effect.Effect<Z, E, R>) => MergeDecision<R, unknown, unknown, E, Z> =
6565
internal.AwaitConst
6666

6767
/**

‎packages/effect/src/Stream.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,7 @@ export const flattenEffect: {
13521352
* @since 2.0.0
13531353
* @category sequencing
13541354
*/
1355-
export const flattenExitOption: <R, E, E2, A>(
1355+
export const flattenExitOption: <A, E2, E, R>(
13561356
self: Stream<Exit.Exit<A, Option.Option<E2>>, E, R>
13571357
) => Stream<A, E | E2, R> = internal.flattenExitOption
13581358

@@ -1620,12 +1620,12 @@ export const groupBy: {
16201620
<A, K, V, E2, R2>(
16211621
f: (a: A) => Effect.Effect<readonly [K, V], E2, R2>,
16221622
options?: { readonly bufferSize?: number | undefined } | undefined
1623-
): <E, R>(self: Stream<A, E, R>) => GroupBy.GroupBy<R2 | R, E2 | E, K, V>
1623+
): <E, R>(self: Stream<A, E, R>) => GroupBy.GroupBy<K, V, E2 | E, R2 | R>
16241624
<A, E, R, K, V, E2, R2>(
16251625
self: Stream<A, E, R>,
16261626
f: (a: A) => Effect.Effect<readonly [K, V], E2, R2>,
16271627
options?: { readonly bufferSize?: number | undefined } | undefined
1628-
): GroupBy.GroupBy<R | R2, E | E2, K, V>
1628+
): GroupBy.GroupBy<K, V, E | E2, R | R2>
16291629
} = _groupBy.groupBy
16301630

16311631
/**
@@ -1671,14 +1671,14 @@ export const groupByKey: {
16711671
options?: {
16721672
readonly bufferSize?: number | undefined
16731673
}
1674-
): <E, R>(self: Stream<A, E, R>) => GroupBy.GroupBy<R, E, K, A>
1674+
): <E, R>(self: Stream<A, E, R>) => GroupBy.GroupBy<K, A, E, R>
16751675
<A, E, R, K>(
16761676
self: Stream<A, E, R>,
16771677
f: (a: A) => K,
16781678
options?: {
16791679
readonly bufferSize?: number | undefined
16801680
}
1681-
): GroupBy.GroupBy<R, E, K, A>
1681+
): GroupBy.GroupBy<K, A, E, R>
16821682
} = _groupBy.groupByKey
16831683

16841684
/**

‎packages/effect/src/SubscriptionRef.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ export const modify: {
139139
* @category utils
140140
*/
141141
export const modifyEffect: {
142-
<A, R, E, B>(f: (a: A) => Effect.Effect<readonly [B, A], E, R>): (self: SubscriptionRef<A>) => Effect.Effect<B, E, R>
143-
<A, R, E, B>(self: SubscriptionRef<A>, f: (a: A) => Effect.Effect<readonly [B, A], E, R>): Effect.Effect<B, E, R>
142+
<B, A, E, R>(f: (a: A) => Effect.Effect<readonly [B, A], E, R>): (self: SubscriptionRef<A>) => Effect.Effect<B, E, R>
143+
<A, B, E, R>(self: SubscriptionRef<A>, f: (a: A) => Effect.Effect<readonly [B, A], E, R>): Effect.Effect<B, E, R>
144144
} = internal.modifyEffect
145145

146146
/**

‎packages/effect/src/TArray.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ export const collectFirst: {
7070
* @category elements
7171
*/
7272
export const collectFirstSTM: {
73-
<A, R, E, B>(pf: (a: A) => Option.Option<STM.STM<B, E, R>>): (self: TArray<A>) => STM.STM<Option.Option<B>, E, R>
74-
<A, R, E, B>(self: TArray<A>, pf: (a: A) => Option.Option<STM.STM<B, E, R>>): STM.STM<Option.Option<B>, E, R>
73+
<A, B, E, R>(pf: (a: A) => Option.Option<STM.STM<B, E, R>>): (self: TArray<A>) => STM.STM<Option.Option<B>, E, R>
74+
<A, B, E, R>(self: TArray<A>, pf: (a: A) => Option.Option<STM.STM<B, E, R>>): STM.STM<Option.Option<B>, E, R>
7575
} = internal.collectFirstSTM
7676

7777
/**

‎packages/effect/src/TMap.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ export const findAllSTM: {
128128
* @category elements
129129
*/
130130
export const forEach: {
131-
<K, V, R, E, X>(f: (key: K, value: V) => STM.STM<X, E, R>): (self: TMap<K, V>) => STM.STM<void, E, R>
132-
<K, V, R, E, X>(self: TMap<K, V>, f: (key: K, value: V) => STM.STM<X, E, R>): STM.STM<void, E, R>
131+
<K, V, X, E, R>(f: (key: K, value: V) => STM.STM<X, E, R>): (self: TMap<K, V>) => STM.STM<void, E, R>
132+
<K, V, X, E, R>(self: TMap<K, V>, f: (key: K, value: V) => STM.STM<X, E, R>): STM.STM<void, E, R>
133133
} = internal.forEach
134134

135135
/**

‎packages/effect/src/TSet.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ export const takeFirst: {
272272
* @category mutations
273273
*/
274274
export const takeFirstSTM: {
275-
<A, R, E, B>(pf: (a: A) => STM.STM<B, Option.Option<E>, R>): (self: TSet<A>) => STM.STM<B, E, R>
276-
<A, R, E, B>(self: TSet<A>, pf: (a: A) => STM.STM<B, Option.Option<E>, R>): STM.STM<B, E, R>
275+
<A, B, E, R>(pf: (a: A) => STM.STM<B, Option.Option<E>, R>): (self: TSet<A>) => STM.STM<B, E, R>
276+
<A, B, E, R>(self: TSet<A>, pf: (a: A) => STM.STM<B, Option.Option<E>, R>): STM.STM<B, E, R>
277277
} = internal.takeFirstSTM
278278

279279
/**
@@ -294,8 +294,8 @@ export const takeSome: {
294294
* @category mutations
295295
*/
296296
export const takeSomeSTM: {
297-
<A, R, E, B>(pf: (a: A) => STM.STM<B, Option.Option<E>, R>): (self: TSet<A>) => STM.STM<[B, ...Array<B>], E, R>
298-
<A, R, E, B>(self: TSet<A>, pf: (a: A) => STM.STM<B, Option.Option<E>, R>): STM.STM<[B, ...Array<B>], E, R>
297+
<A, B, E, R>(pf: (a: A) => STM.STM<B, Option.Option<E>, R>): (self: TSet<A>) => STM.STM<[B, ...Array<B>], E, R>
298+
<A, B, E, R>(self: TSet<A>, pf: (a: A) => STM.STM<B, Option.Option<E>, R>): STM.STM<[B, ...Array<B>], E, R>
299299
} = internal.takeSomeSTM
300300

301301
/**

‎packages/effect/src/internal/channel/mergeDecision.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export const Await = <R, E0, Z0, E, Z>(
7272
}
7373

7474
/** @internal */
75-
export const AwaitConst = <R, E, Z>(
75+
export const AwaitConst = <Z, E, R>(
7676
effect: Effect.Effect<Z, E, R>
7777
): MergeDecision.MergeDecision<R, unknown, unknown, E, Z> => Await(() => effect)
7878

‎packages/effect/src/internal/core-effect.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1282,22 +1282,22 @@ export const reduceRight = dual<
12821282

12831283
/* @internal */
12841284
export const reduceWhile = dual<
1285-
<A, R, E, Z>(
1285+
<Z, A, E, R>(
12861286
zero: Z,
12871287
options: {
12881288
readonly while: Predicate.Predicate<Z>
12891289
readonly body: (s: Z, a: A, i: number) => Effect.Effect<Z, E, R>
12901290
}
12911291
) => (elements: Iterable<A>) => Effect.Effect<Z, E, R>,
1292-
<A, R, E, Z>(
1292+
<A, Z, E, R>(
12931293
elements: Iterable<A>,
12941294
zero: Z,
12951295
options: {
12961296
readonly while: Predicate.Predicate<Z>
12971297
readonly body: (s: Z, a: A, i: number) => Effect.Effect<Z, E, R>
12981298
}
12991299
) => Effect.Effect<Z, E, R>
1300-
>(3, <A, R, E, Z>(
1300+
>(3, <A, Z, E, R>(
13011301
elements: Iterable<A>,
13021302
zero: Z,
13031303
options: {

‎packages/effect/src/internal/core.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -703,13 +703,13 @@ export const andThen: {
703703
) => [X] extends [Effect.Effect<infer A1, infer E1, infer R1>] ? Effect.Effect<A1, E | E1, R | R1>
704704
: [X] extends [Promise<infer A1>] ? Effect.Effect<A1, E | Cause.UnknownException, R>
705705
: Effect.Effect<X, E, R>
706-
<A, R, E, X>(
706+
<A, E, R, X>(
707707
self: Effect.Effect<A, E, R>,
708708
f: (a: NoInfer<A>) => X
709709
): [X] extends [Effect.Effect<infer A1, infer E1, infer R1>] ? Effect.Effect<A1, E | E1, R | R1>
710710
: [X] extends [Promise<infer A1>] ? Effect.Effect<A1, E | Cause.UnknownException, R>
711711
: Effect.Effect<X, E, R>
712-
<A, R, E, X>(
712+
<A, E, R, X>(
713713
self: Effect.Effect<A, E, R>,
714714
f: X
715715
): [X] extends [Effect.Effect<infer A1, infer E1, infer R1>] ? Effect.Effect<A1, E | E1, R | R1>

0 commit comments

Comments
 (0)
Please sign in to comment.