File tree Expand file tree Collapse file tree 3 files changed +33
-0
lines changed
packages/stdlib/_src/prelude Expand file tree Collapse file tree 3 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ " @tsplus/stdlib " : patch
3+ ---
4+
5+ Add Ord.tuple
Original file line number Diff line number Diff line change @@ -5,4 +5,5 @@ export * from "@tsplus/stdlib/prelude/Ord/definition"
55export * from "@tsplus/stdlib/prelude/Ord/getAssociative"
66export * from "@tsplus/stdlib/prelude/Ord/getAssociativeIdentity"
77export * from "@tsplus/stdlib/prelude/Ord/operations"
8+ export * from "@tsplus/stdlib/prelude/Ord/tuple"
89// codegen:end
Original file line number Diff line number Diff line change 1+ export type NonEmptyArrayOrd = Array < Ord < any > > & { readonly 0 : Ord < any > }
2+
3+ export type TupleOrd < T extends NonEmptyArrayOrd > = {
4+ [ K in keyof T ] : [ T [ K ] ] extends [ Ord < infer A > ] ? A : never
5+ }
6+
7+ /**
8+ * Derives an Ord instance for a tuple
9+ *
10+ * @tsplus static Ord/Ops tuple
11+ */
12+ export function tuple < T extends NonEmptyArrayOrd > (
13+ ...ords : T & {
14+ readonly 0 : Ord < any >
15+ }
16+ ) : Ord < ForcedTuple < TupleOrd < T > > > {
17+ return Ord ( ( first , second ) => {
18+ let i = 0
19+ for ( ; i < ords . length - 1 ; i ++ ) {
20+ const r = ords [ i ] ! . compare ( first [ i ] , second [ i ] )
21+ if ( r !== 0 ) {
22+ return r
23+ }
24+ }
25+ return ords [ i ] ! . compare ( first [ i ] , second [ i ] )
26+ } )
27+ }
You can’t perform that action at this time.
0 commit comments