-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
Implement at
method for tuples
#2976
Implement at
method for tuples
#2976
Conversation
🦋 Changeset detectedLatest commit: 7d9686e The changes in this PR will be included in the next version bump. This PR includes changesets to release 25 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
We could probably do better by using |
Ready! |
export const at: {
<A extends ReadonlyArray<unknown>, N extends number>(index: N): (self: A) => A[N]
<A extends ReadonlyArray<unknown>, N extends number>(self: A, index: N): A[N]
} wondering if export const at: {
<N extends number>(index: N): <A extends ReadonlyArray<unknown>>(self: A) => A[N]
<A extends ReadonlyArray<unknown>, N extends number>(self: A, index: N): A[N]
} So that it allows |
b6ce3f9
to
3572b58
Compare
16dfb57
to
283e95e
Compare
I would mirror what we did in
export const get =
<I extends number>(index: I) => <T extends ReadonlyArray<any>>(t: T): [] extends T ? T[I] | undefined : T[I] =>
t[index]
import { hole, pipe } from "effect/Function"
// -------------------------------------------------------------------------------------
// get
// -------------------------------------------------------------------------------------
// $ExpectType <T extends readonly any[]>(t: T) => [] extends T ? T[0] | undefined : T[0]
const get0 = get(0)
// $ExpectType undefined
pipe(hole<[]>(), get0)
// $ExpectType undefined
pipe(hole<readonly []>(), get0)
// $ExpectType string
pipe(hole<[string, number]>(), get0)
// $ExpectType string
pipe(hole<readonly [string, number]>(), get0)
// $ExpectType number
pipe(hole<[string, number]>(), get(1))
// $ExpectType number
pipe(hole<readonly [string, number]>(), get(1))
// $ExpectType undefined
pipe(hole<[string, number]>(), get(2))
// $ExpectType undefined
pipe(hole<readonly [string, number]>(), get(2))
// $ExpectType string | number
pipe(hole<[string, number]>(), get(-1))
// $ExpectType string | number
pipe(hole<readonly [string, number]>(), get(-1))
// $ExpectType number | undefined
pipe(hole<Array<number>>(), get(1))
// $ExpectType number | undefined
pipe(hole<Array<number>>(), get(-1)) |
thank you @gcanti! what do you think, @mikearnaldi? |
It would be something as: /**
* Retrieves the element at a specified index from a tuple.
*
* @param self - A tuple from which to retrieve the element.
* @param index - The index of the element to retrieve.
*
* @example
* import { at } from "effect/Tuple"
*
* assert.deepStrictEqual(at([1, 'hello', true], 1), 'hello')
*
* @category getters
* @since 3.4.0
*/
export const at: {
<N extends number>(index: N): <A extends ReadonlyArray<unknown>>(self: A) => A[N]
<A extends ReadonlyArray<unknown>, N extends number>(self: A, index: N): A[N]
} = dual(2, <A extends ReadonlyArray<unknown>, N extends number>(self: A, index: N): A[N] => self[index]) |
I think I am ok with it, it's in line with schema |
I will send a PR to add it |
cc @mikearnaldi 👍🏻 |
3572b58
to
b9ea946
Compare
57465f3
to
1e768a0
Compare
5a5e747
to
5fd0054
Compare
5fd0054
to
57465f3
Compare
c2327e7
to
f68d673
Compare
I would be good to add the dtslint tests @gcanti quoted above. |
f51ab48
to
31a5555
Compare
f68d673
to
176ec9f
Compare
176ec9f
to
c2327e7
Compare
31a5555
to
5ec3fa4
Compare
c2327e7
to
440b9f5
Compare
Closes #2975