|
5 | 5 | -- | use cases. This module is useful when integrating with JavaScript libraries
|
6 | 6 | -- | which use arrays, but immutable arrays are not a practical data structure
|
7 | 7 | -- | 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 | +-- | |
8 | 30 | module Data.Array
|
9 | 31 | ( singleton
|
10 | 32 | , (..), range
|
@@ -94,10 +116,6 @@ import Data.Traversable (sequence)
|
94 | 116 | import Data.Tuple (Tuple(..))
|
95 | 117 | import qualified Data.Maybe.Unsafe as U
|
96 | 118 |
|
97 |
| --------------------------------------------------------------------------------- |
98 |
| --- Array creation -------------------------------------------------------------- |
99 |
| --------------------------------------------------------------------------------- |
100 |
| - |
101 | 119 | -- | Create an array of one element
|
102 | 120 | singleton :: forall a. a -> Array a
|
103 | 121 | singleton a = [a]
|
@@ -464,10 +482,6 @@ groupBy op = go []
|
464 | 482 | in go ((o.head : sp.init) : acc) sp.rest
|
465 | 483 | Nothing -> reverse acc
|
466 | 484 |
|
467 |
| --------------------------------------------------------------------------------- |
468 |
| --- Set-like operations --------------------------------------------------------- |
469 |
| --------------------------------------------------------------------------------- |
470 |
| - |
471 | 485 | -- | Remove the duplicates from an array, creating a new array.
|
472 | 486 | nub :: forall a. (Eq a) => Array a -> Array a
|
473 | 487 | nub = nubBy eq
|
@@ -519,10 +533,6 @@ intersect = intersectBy eq
|
519 | 533 | intersectBy :: forall a. (a -> a -> Boolean) -> Array a -> Array a -> Array a
|
520 | 534 | intersectBy eq xs ys = filter (\x -> isJust (findIndex (eq x) ys)) xs
|
521 | 535 |
|
522 |
| --------------------------------------------------------------------------------- |
523 |
| --- Zipping --------------------------------------------------------------------- |
524 |
| --------------------------------------------------------------------------------- |
525 |
| - |
526 | 536 | -- | Apply a function to pairs of elements at the same index in two arrays,
|
527 | 537 | -- | collecting the results in a new array.
|
528 | 538 | -- |
|
@@ -551,10 +561,6 @@ unzip :: forall a b. Array (Tuple a b) -> Tuple (Array a) (Array b)
|
551 | 561 | unzip = uncons' (\_ -> Tuple [] []) \(Tuple a b) ts -> case unzip ts of
|
552 | 562 | Tuple as bs -> Tuple (a : as) (b : bs)
|
553 | 563 |
|
554 |
| --------------------------------------------------------------------------------- |
555 |
| --- Folding --------------------------------------------------------------------- |
556 |
| --------------------------------------------------------------------------------- |
557 |
| - |
558 | 564 | -- | Perform a fold using a monadic step function.
|
559 | 565 | foldM :: forall m a b. (Monad m) => (a -> b -> m a) -> a -> Array b -> m a
|
560 | 566 | foldM f a = uncons' (\_ -> return a) (\b bs -> f a b >>= \a' -> foldM f a' bs)
|
0 commit comments