Skip to content

Commit 8b767db

Browse files
committed
Merge pull request #52 from hdgarrood/improve-docs
Improve docs
2 parents 57f4c37 + 5ac412b commit 8b767db

File tree

5 files changed

+69
-24
lines changed

5 files changed

+69
-24
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
language: node_js
22
sudo: false
33
node_js:
4-
- 0.10
4+
- 4
5+
- 5
56
env:
67
- PATH=$HOME/purescript:$PATH
78
install:

docs/Data/Array.md

+41-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,36 @@ use cases. This module is useful when integrating with JavaScript libraries
88
which use arrays, but immutable arrays are not a practical data structure
99
for many use cases due to their poor asymptotics.
1010

11+
In addition to the functions in this module, Arrays have a number of
12+
useful instances:
13+
14+
* `Functor`, which provides `map :: forall a b. (a -> b) -> Array a ->
15+
Array b`
16+
* `Apply`, which provides `(<*>) :: forall a b. Array (a -> b) -> Array a
17+
-> Array b`. This function works a bit like a Cartesian product; the
18+
result array is constructed by applying each function in the first
19+
array to each value in the second, so that the result array ends up with
20+
a length equal to the product of the two arguments' lengths.
21+
* `Bind`, which provides `(>>=) :: forall a b. (a -> Array b) -> Array a
22+
-> Array b` (this is the same as `concatMap`).
23+
* `Semigroup`, which provides `(<>) :: forall a. Array a -> Array a ->
24+
Array a`, for concatenating arrays.
25+
* `Foldable`, which provides a slew of functions for *folding* (also known
26+
as *reducing*) arrays down to one value. For example,
27+
`Data.Foldable.any` tests whether an array of `Boolean` values contains
28+
at least one `true`.
29+
* `Traversable`, which provides the PureScript version of a for-loop,
30+
allowing you to iterate over an array and accumulate effects.
31+
32+
1133
#### `singleton`
1234

1335
``` purescript
1436
singleton :: forall a. a -> Array a
1537
```
1638

39+
Create an array of one element
40+
1741
#### `range`
1842

1943
``` purescript
@@ -453,7 +477,7 @@ specified equivalence relation to detemine equality.
453477
nub :: forall a. (Eq a) => Array a -> Array a
454478
```
455479

456-
Special case of `nubBy`: `nubBy eq`
480+
Remove the duplicates from an array, creating a new array.
457481

458482
#### `nubBy`
459483

@@ -462,8 +486,7 @@ nubBy :: forall a. (a -> a -> Boolean) -> Array a -> Array a
462486
```
463487

464488
Remove the duplicates from an array, where element equality is determined
465-
by the specified equivalence relation, creating a new array. The first
466-
occurence of an element is always the one that is kept.
489+
by the specified equivalence relation, creating a new array.
467490

468491
#### `union`
469492

@@ -537,6 +560,17 @@ relation to compare elements, creating a new array.
537560
zipWith :: forall a b c. (a -> b -> c) -> Array a -> Array b -> Array c
538561
```
539562

563+
Apply a function to pairs of elements at the same index in two arrays,
564+
collecting the results in a new array.
565+
566+
If one array is longer, elements will be discarded from the longer array.
567+
568+
For example
569+
570+
```purescript
571+
zipWith (*) [1, 2, 3] [4, 5, 6, 7] == [4, 10, 18]
572+
```
573+
540574
#### `zipWithA`
541575

542576
``` purescript
@@ -569,3 +603,7 @@ second components.
569603
``` purescript
570604
foldM :: forall m a b. (Monad m) => (a -> b -> m a) -> a -> Array b -> m a
571605
```
606+
607+
Perform a fold using a monadic step function.
608+
609+

docs/Data/Array/ST.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ A reference to a mutable array.
1515
The first type parameter represents the memory region which the array belongs to.
1616
The second type parameter defines the type of elements of the mutable array.
1717

18-
The runtime representation of a value of type `STArray h a` is the same as that of `[a]`,
18+
The runtime representation of a value of type `STArray h a` is the same as that of `Array a`,
1919
except that mutation is allowed.
2020

2121
#### `Assoc`
@@ -24,7 +24,7 @@ except that mutation is allowed.
2424
type Assoc a = { value :: a, index :: Int }
2525
```
2626

27-
An element and its index
27+
An element and its index.
2828

2929
#### `runSTArray`
3030

src/Data/Array.purs

+22-16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,28 @@
55
-- | use cases. This module is useful when integrating with JavaScript libraries
66
-- | which use arrays, but immutable arrays are not a practical data structure
77
-- | for many use cases due to their poor asymptotics.
8+
-- |
9+
-- | In addition to the functions in this module, Arrays have a number of
10+
-- | useful instances:
11+
-- |
12+
-- | * `Functor`, which provides `map :: forall a b. (a -> b) -> Array a ->
13+
-- | Array b`
14+
-- | * `Apply`, which provides `(<*>) :: forall a b. Array (a -> b) -> Array a
15+
-- | -> Array b`. This function works a bit like a Cartesian product; the
16+
-- | result array is constructed by applying each function in the first
17+
-- | array to each value in the second, so that the result array ends up with
18+
-- | a length equal to the product of the two arguments' lengths.
19+
-- | * `Bind`, which provides `(>>=) :: forall a b. (a -> Array b) -> Array a
20+
-- | -> Array b` (this is the same as `concatMap`).
21+
-- | * `Semigroup`, which provides `(<>) :: forall a. Array a -> Array a ->
22+
-- | Array a`, for concatenating arrays.
23+
-- | * `Foldable`, which provides a slew of functions for *folding* (also known
24+
-- | as *reducing*) arrays down to one value. For example,
25+
-- | `Data.Foldable.any` tests whether an array of `Boolean` values contains
26+
-- | at least one `true`.
27+
-- | * `Traversable`, which provides the PureScript version of a for-loop,
28+
-- | allowing you to iterate over an array and accumulate effects.
29+
-- |
830
module Data.Array
931
( singleton
1032
, (..), range
@@ -94,10 +116,6 @@ import Data.Traversable (sequence)
94116
import Data.Tuple (Tuple(..))
95117
import qualified Data.Maybe.Unsafe as U
96118

97-
--------------------------------------------------------------------------------
98-
-- Array creation --------------------------------------------------------------
99-
--------------------------------------------------------------------------------
100-
101119
-- | Create an array of one element
102120
singleton :: forall a. a -> Array a
103121
singleton a = [a]
@@ -464,10 +482,6 @@ groupBy op = go []
464482
in go ((o.head : sp.init) : acc) sp.rest
465483
Nothing -> reverse acc
466484

467-
--------------------------------------------------------------------------------
468-
-- Set-like operations ---------------------------------------------------------
469-
--------------------------------------------------------------------------------
470-
471485
-- | Remove the duplicates from an array, creating a new array.
472486
nub :: forall a. (Eq a) => Array a -> Array a
473487
nub = nubBy eq
@@ -519,10 +533,6 @@ intersect = intersectBy eq
519533
intersectBy :: forall a. (a -> a -> Boolean) -> Array a -> Array a -> Array a
520534
intersectBy eq xs ys = filter (\x -> isJust (findIndex (eq x) ys)) xs
521535

522-
--------------------------------------------------------------------------------
523-
-- Zipping ---------------------------------------------------------------------
524-
--------------------------------------------------------------------------------
525-
526536
-- | Apply a function to pairs of elements at the same index in two arrays,
527537
-- | collecting the results in a new array.
528538
-- |
@@ -551,10 +561,6 @@ unzip :: forall a b. Array (Tuple a b) -> Tuple (Array a) (Array b)
551561
unzip = uncons' (\_ -> Tuple [] []) \(Tuple a b) ts -> case unzip ts of
552562
Tuple as bs -> Tuple (a : as) (b : bs)
553563

554-
--------------------------------------------------------------------------------
555-
-- Folding ---------------------------------------------------------------------
556-
--------------------------------------------------------------------------------
557-
558564
-- | Perform a fold using a monadic step function.
559565
foldM :: forall m a b. (Monad m) => (a -> b -> m a) -> a -> Array b -> m a
560566
foldM f a = uncons' (\_ -> return a) (\b bs -> f a b >>= \a' -> foldM f a' bs)

src/Data/Array/ST.purs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import Data.Maybe (Maybe(..))
2828
-- | The first type parameter represents the memory region which the array belongs to.
2929
-- | The second type parameter defines the type of elements of the mutable array.
3030
-- |
31-
-- | The runtime representation of a value of type `STArray h a` is the same as that of `[a]`,
31+
-- | The runtime representation of a value of type `STArray h a` is the same as that of `Array a`,
3232
-- | except that mutation is allowed.
3333
foreign import data STArray :: * -> * -> *
3434

35-
-- | An element and its index
35+
-- | An element and its index.
3636
type Assoc a = { value :: a, index :: Int }
3737

3838
-- | Freeze a mutable array, creating an immutable array. Use this function as you would use

0 commit comments

Comments
 (0)