How do I get the flat tuple type of a tuple?


Pedro A

Consider I have a tuple of tuples:

type Example = [[3,5,7], [4,9], [0,1,10,9]];

I want to create a utility Flatten<T>that Flatten<Example>results in:

type FlatExample = Flatten<Example>;
// type FlatExample = [3,5,7,4,9,0,1,10,9];

For my use case, you can assume that tuples are only nested one level. Tuples can have any size.

How can I do this?

Titian Chernikova Dragomir

For this you will need recursive conditional types. This will be fully supported in 4.1:

type Example = [[3,5,7], [4,9], [0,1,10,9]];
type Flatten<T extends any[]> = 
    T extends [infer U, ...infer R] ? U extends any[] ? [...U, ... Flatten<R>]: []: []
type FlatExample = Flatten<Example>;

playground link

edit

A simpler version, courtesy of jcalz:

type Example = [[3,5,7], [4,9], [0,1,10,9]];
type Flatten<T extends any[]> = 
    T extends [any, ...infer R] ? [...T[0], ... Flatten<R>]:  []
type FlatExample = Flatten<Example>;

playground link

/edit

You can even hack a version today (requires extra indirection to trick the compiler into allowing recursive conditional types, which is conceptually equivalent to the simpler version above):

type Example = [[3, 5, 7], [4, 9], [0, 1, 10, 9]];
type Flatten<T extends any[]> = T extends [infer U, ...infer R] ? {
    1: U extends any[] ? [...U, ...Flatten<R>] : [],
    2: []
}[U extends any[] ? 1 : 2] : [];

type FlatExample = Flatten<Example>;

playground link

Just for fun, it's the 4.1 generalized flattened version.

type Example = [[3,5,7], [4,9, [10, 12, [10, 12]]], [0,1,10,9, [10, 12]]];
type Flatten<T extends any[]> = 
    T extends [infer U, ...infer R] ? 
        U extends any[] ? 
        [...Flatten<U>, ... Flatten<R>]: [U, ... Flatten<R>]: []
type FlatExample = Flatten<Example>;

playground link

Note : While recursive types are more supported in 4.1, you can still run into compiler-hardcoded limitations such as type instantiation depth and total type instances (as such recursive types generate many type instantiations). Therefore, use it with caution.

Related


How do I get the flat tuple type of a tuple?

Pedro A Consider I have a tuple of tuples: type Example = [[3,5,7], [4,9], [0,1,10,9]]; I want to create a utility Flatten<T>that Flatten<Example>results in: type FlatExample = Flatten<Example>; // type FlatExample = [3,5,7,4,9,0,1,10,9]; For my use case, yo

How do I get the flat tuple type of a tuple?

Pedro A Consider I have a tuple of tuples: type Example = [[3,5,7], [4,9], [0,1,10,9]]; I want to create a utility Flatten<T>that Flatten<Example>results in: type FlatExample = Flatten<Example>; // type FlatExample = [3,5,7,4,9,0,1,10,9]; For my use case, yo

How do I get the flat tuple type of a tuple?

Pedro A Consider I have a tuple of tuples: type Example = [[3,5,7], [4,9], [0,1,10,9]]; I want to create a utility Flatten<T>that Flatten<Example>results in: type FlatExample = Flatten<Example>; // type FlatExample = [3,5,7,4,9,0,1,10,9]; For my use case, yo

How do I get the flat tuple type of a tuple?

Pedro A Consider I have a tuple of tuples: type Example = [[3,5,7], [4,9], [0,1,10,9]]; I want to create a utility Flatten<T>that Flatten<Example>results in: type FlatExample = Flatten<Example>; // type FlatExample = [3,5,7,4,9,0,1,10,9]; For my use case, yo

Get flat tuple type from arbitrarily nested tuple type

Zheng Qu I want a type trait that creates a flattened tuple type flatten_tuple_tfrom arbitrarily nested tuple types . The following code snippet illustrates flatten_tuple_t. template <typename T> struct flatten_tuple { using type = T; }; // The real implemen

How do I get the min and max of a tuple

Jean Prosin How to get codes that return 2 (minimum) and 29 (maximum) intervals = [ (2, 18), (2, 15), (5, 28), (10, 14), (11, 29), (6, 17), (3, 7), (8, 22) ] z = max(intervals) print (z) Olivier Melançon When it comes time to

How do I get the min and max of a tuple

Jean Prosin How to get codes that return 2 (minimum) and 29 (maximum) intervals = [ (2, 18), (2, 15), (5, 28), (10, 14), (11, 29), (6, 17), (3, 7), (8, 22) ] z = max(intervals) print (z) Olivier Melançon When it comes time to

How do I get the min and max of a tuple

Jean Prosin How to get codes that return 2 (minimum) and 29 (maximum) intervals = [ (2, 18), (2, 15), (5, 28), (10, 14), (11, 29), (6, 17), (3, 7), (8, 22) ] z = max(intervals) print (z) Olivier Melançon When it comes time to

How do I get the min and max of a tuple

Jean Prosin How to get codes that return 2 (minimum) and 29 (maximum) intervals = [ (2, 18), (2, 15), (5, 28), (10, 14), (11, 29), (6, 17), (3, 7), (8, 22) ] z = max(intervals) print (z) Olivier Melançon When it comes time to

Why do I get an empty tuple if I type / in iPython?

Edgar Klerks Open iPython and enter the following: / Press Enter and want to know the result: () You can't assign it, I guess it has something to do with shell capabilities. edit: You can assign it to: p = Out[xx] but not directly: p = / will give: Synt

Python Why do I get a tuple and how to avoid it

Daniel Eagle I don't know why, but when I calculate AIQhum I get tuple, in this case hum > 42, I get this tuple (0, 190669.42) Don't know why I get 0 in the first place. How can I avoid it forcing to not just get the tuple of the second value? Thank you. de

How do I convert a list to a tuple within a tuple

Moore Cole enter: (('Alfred', ['gaming', 'shopping', 'sport', 'travel']), ('Carmen', ['cooking', 'pets', 'photography', 'shopping', 'sport',])) How can I convert this list to a tuple? Expected output: (('Alfred', ('gaming', 'shopping', 'sport', 'travel')),

how do i convert tuple to string

Narendra Petkar Using DataBaseLibrary for Robot Framework, I get a list of tuples @{recordList} Query select * from employee How to convert it to string? I want to write this to a file. When I try FOR ${ELEMENT} in @{recordList} str{$ELEMENT} If the va

how do i convert tuple to string

Narendra Petkar Using DataBaseLibrary for Robot Framework, I get a list of tuples @{recordList} Query select * from employee How to convert it to string? I want to write this to a file. When I try FOR ${ELEMENT} in @{recordList} str{$ELEMENT} If the va

How to flat nest tuple arguments in a function?

counter2015 I have a function that gtakes arguments (Int, (Int, Int)) => Intand a flat functionf0 (Int, Int, Int) => Int I want to construct a function ftthat flattens gthe argument to to f0. Here is an example: val f0: ((Int, Int, Int)) => Int = (x: (Int, Int

How to determine the tuple type?

toplel32 Apparently ITupleinternal, thus disabling solutions such as typeof(ITuple).IsAssignableFrom(type). Or, what is the most efficient way to determine Tuple<>farming Tuple<,,,,,,,>? A solution without type name comparison is preferable. rtf_leg try this:

How to determine the tuple type?

toplel32 Apparently ITupleinternal, thus disabling solutions such as typeof(ITuple).IsAssignableFrom(type). Or, what is the most efficient way to determine Tuple<>farming Tuple<,,,,,,,>? A solution without type name comparison is preferable. rtf_leg Try this:

How to determine the tuple type?

toplel32 Apparently ITupleinternal, thus disabling solutions such as typeof(ITuple).IsAssignableFrom(type). Or, what is the most efficient way to determine Tuple<>farming Tuple<,,,,,,,>? A solution without type name comparison is preferable. rtf_leg try this:

How to determine the tuple type?

toplel32 Apparently ITupleinternal, thus disabling solutions such as typeof(ITuple).IsAssignableFrom(type). Or, what is the most efficient way to determine Tuple<>farming Tuple<,,,,,,,>? A solution without type name comparison is preferable. rtf_leg Try this:

How to determine the tuple type?

toplel32 Apparently ITupleinternal, thus disabling solutions such as typeof(ITuple).IsAssignableFrom(type). Or, what is the most efficient way to determine Tuple<>farming Tuple<,,,,,,,>? A solution without type name comparison is preferable. rtf_leg try this: