Skip to content

Commit

Permalink
chore: add Ord.tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
mikearnaldi committed Aug 25, 2022
1 parent 7d8d9b9 commit 2c38b88
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-steaks-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tsplus/stdlib": patch
---

Add Ord.tuple
1 change: 1 addition & 0 deletions packages/stdlib/_src/prelude/Ord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export * from "@tsplus/stdlib/prelude/Ord/definition"
export * from "@tsplus/stdlib/prelude/Ord/getAssociative"
export * from "@tsplus/stdlib/prelude/Ord/getAssociativeIdentity"
export * from "@tsplus/stdlib/prelude/Ord/operations"
export * from "@tsplus/stdlib/prelude/Ord/tuple"
// codegen:end
27 changes: 27 additions & 0 deletions packages/stdlib/_src/prelude/Ord/tuple.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export type NonEmptyArrayOrd = Array<Ord<any>> & { readonly 0: Ord<any> }

export type TupleOrd<T extends NonEmptyArrayOrd> = {
[K in keyof T]: [T[K]] extends [Ord<infer A>] ? A : never
}

/**
* Derives an Ord instance for a tuple
*
* @tsplus static Ord/Ops tuple
*/
export function tuple<T extends NonEmptyArrayOrd>(
...ords: T & {
readonly 0: Ord<any>
}
): Ord<ForcedTuple<TupleOrd<T>>> {
return Ord((first, second) => {
let i = 0
for (; i < ords.length - 1; i++) {
const r = ords[i]!.compare(first[i], second[i])
if (r !== 0) {
return r
}
}
return ords[i]!.compare(first[i], second[i])
})
}

0 comments on commit 2c38b88

Please # to comment.