Skip to content

Commit 08d87df

Browse files
marickLiamGoodacre
authored andcommitted
More documentation in the module header (#30)
* More documentation in the module header * Implement suggestions of @LiamGoodacre on previous pull request.
1 parent 0036bf9 commit 08d87df

File tree

1 file changed

+52
-16
lines changed

1 file changed

+52
-16
lines changed

src/Data/Tuple/Nested.purs

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,62 @@
1-
-- | Utilities for n-tuples: sequences longer than two components built from
2-
-- | nested pairs.
1+
-- | Tuples that are not restricted to two elements.
32
-- |
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:
84
-- |
5+
-- |
96
-- | ```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+
-- | ```
1424
-- |
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`.
1727
-- |
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+
-- | ```
2041
-- |
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
2347
-- | ```
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+
-- |
2460
module Data.Tuple.Nested where
2561

2662
import Prelude

0 commit comments

Comments
 (0)