Skip to content

Commit

Permalink
Implement at method for tuples (#2976)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim <hello@timsmart.co>
  • Loading branch information
2 people authored and gcanti committed Jun 14, 2024
1 parent 2bdd713 commit 83632b3
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
11 changes: 11 additions & 0 deletions .changeset/violet-students-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"effect": minor
---

Add `Tuple.at` api, to retrieve an element at a specified index from a tuple.

```ts
import { Tuple } from "effect"

assert.deepStrictEqual(Tuple.at([1, 'hello', true], 1), 'hello')
```
45 changes: 44 additions & 1 deletion packages/effect/dtslint/Tuple.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pipe } from "effect/Function"
import { hole, pipe } from "effect/Function"
import * as T from "effect/Tuple"

// -------------------------------------------------------------------------------------
Expand All @@ -17,3 +17,46 @@ pipe(T.make("a", 1), T.appendElement(true))

// $ExpectType [string, number, boolean]
T.appendElement(T.make("a", 1), true)

// -------------------------------------------------------------------------------------
// at
// -------------------------------------------------------------------------------------

// $ExpectType <A extends ReadonlyArray<unknown>>(self: A) => A[0]
const at0 = T.at(0)

// $ExpectType undefined
pipe(hole<[]>(), at0)

// $ExpectType undefined
pipe(hole<readonly []>(), at0)

// $ExpectType string
pipe(hole<[string, number]>(), at0)

// $ExpectType string
pipe(hole<readonly [string, number]>(), at0)

// $ExpectType number
pipe(hole<[string, number]>(), T.at(1))

// $ExpectType number
pipe(hole<readonly [string, number]>(), T.at(1))

// $ExpectType undefined
pipe(hole<[string, number]>(), T.at(2))

// $ExpectType undefined
pipe(hole<readonly [string, number]>(), T.at(2))

// $ExpectType string | number
pipe(hole<[string, number]>(), T.at(-1))

// $ExpectType string | number
pipe(hole<readonly [string, number]>(), T.at(-1))

// $ExpectType number
pipe(hole<Array<number>>(), T.at(1))

// $ExpectType number
pipe(hole<Array<number>>(), T.at(-1))
26 changes: 18 additions & 8 deletions packages/effect/src/Tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,24 @@ export const appendElement: {
<A extends ReadonlyArray<unknown>, B>(self: A, that: B): [...A, B]
} = dual(2, <A extends ReadonlyArray<unknown>, B>(self: A, that: B): [...A, B] => [...self, that])

/*
TODO:
- at
- swap
*/
/**
* 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 { Tuple } from "effect"
*
* assert.deepStrictEqual(Tuple.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])

export {
/**
Expand Down
4 changes: 4 additions & 0 deletions packages/effect/test/Tuple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ describe("Tuple", () => {
it("swap", () => {
expect(T.swap(T.make("a", 1))).toEqual([1, "a"])
})

it("at", () => {
expect(T.at([1, "hello", true], 1)).toEqual("hello")
})
})

0 comments on commit 83632b3

Please sign in to comment.