|
1 |
| --- | Utilities for n-tuples: sequences longer than two components built from |
2 |
| --- | nested pairs. |
| 1 | +-- | Tuples that are not restricted to two elements. |
3 | 2 | -- |
|
4 |
| --- | Nested tuples arise naturally in product combinators. You shouldn't |
5 |
| --- | represent data using nested tuples, but if combinators you're working with |
6 |
| --- | create them, utilities in this module will allow to to more easily work |
7 |
| --- | with them, including translating to and from more traditional product types. |
| 3 | +-- | Here is an example of a 3-tuple: |
8 | 4 | -- |
|
| 5 | +-- | |
9 | 6 | -- | ```purescript
|
10 |
| --- | data Address = Address String City (Maybe Province) Country |
11 |
| --- | |
12 |
| --- | exampleAddress1 = makeAddress "221B Baker Street" London Nothing UK |
13 |
| --- | exampleAddress2 = makeAddressT $ "221B Baker Street" /\ London /\ Nothing /\ UK |
| 7 | +-- | > tuple = tuple3 1 "2" 3.0 |
| 8 | +-- | > tuple |
| 9 | +-- | (Tuple 1 (Tuple "2" (Tuple 3.0 unit))) |
| 10 | +-- | ``` |
| 11 | +-- | |
| 12 | +-- | Notice that a tuple is a nested structure not unlike a list. The type of `tuple` is this: |
| 13 | +-- | |
| 14 | +-- | ```purescript |
| 15 | +-- | > :t tuple |
| 16 | +-- | Tuple Int (Tuple String (Tuple Number Unit)) |
| 17 | +-- | ``` |
| 18 | +-- | |
| 19 | +-- | That, however, can be abbreviated with the `Tuple3` type: |
| 20 | +-- | |
| 21 | +-- | ```purescript |
| 22 | +-- | Tuple3 Int String Number |
| 23 | +-- | ``` |
14 | 24 | -- |
|
15 |
| --- | makeAddressT :: Tuple4 String City (Maybe Province) Country -> Address |
16 |
| --- | makeAddressT = uncurry4 Address |
| 25 | +-- | All tuple functions are numbered from 1 to 10. That is, there's |
| 26 | +-- | a `get1` and a `get10`. |
17 | 27 | -- |
|
18 |
| --- | makeAddress :: String -> City -> (Maybe Province) -> Country -> Address |
19 |
| --- | makeAddress = curry4 unit makeAddressT |
| 28 | +-- | The `getN` functions accept tuples of length N or greater: |
| 29 | +-- | |
| 30 | +-- | ```purescript |
| 31 | +-- | get1 tuple = 1 |
| 32 | +-- | get3 tuple = 3 |
| 33 | +-- | get4 tuple -- type error. `get4` requires a longer tuple. |
| 34 | +-- | ``` |
| 35 | +-- | |
| 36 | +-- | The same is true of the `overN` functions: |
| 37 | +-- | |
| 38 | +-- | ```purescript |
| 39 | +-- | over2 negate (tuple3 1 2 3) = tuple3 1 (-2) 3 |
| 40 | +-- | ``` |
20 | 41 | -- |
|
21 |
| --- | tupleAddress :: Address -> Tuple4 String City (Maybe Province) Country |
22 |
| --- | tupleAddress (Address a b c d) = tuple4 a b c d |
| 42 | + |
| 43 | +-- | `uncurryN` can be used to convert a function that takes `N` arguments to one that takes an N-tuple: |
| 44 | +-- | |
| 45 | +-- | ```purescript |
| 46 | +-- | uncurry2 (+) (tuple2 1 2) = 3 |
23 | 47 | -- | ```
|
| 48 | +-- | |
| 49 | +-- | The reverse `curryN` function converts functions that take |
| 50 | +-- | N-tuples (which are rare) to functions that take `N` arguments. |
| 51 | +-- | |
| 52 | +-- | --------------- |
| 53 | +-- | In addition to types like `Tuple3`, there are also types like |
| 54 | +-- | `T3`. Whereas `Tuple3` describes a tuple with exactly three |
| 55 | +-- | elements, `T3` describes a tuple of length *two or longer*. More |
| 56 | +-- | specifically, `T3` requires two element plus a "tail" that may be |
| 57 | +-- | `unit` or more tuple elements. Use types like `T3` when you want to |
| 58 | +-- | create a set of functions for arbitrary tuples. See the source for how that's done. |
| 59 | +-- | |
24 | 60 | module Data.Tuple.Nested where
|
25 | 61 |
|
26 | 62 | import Prelude
|
|
0 commit comments