Skip to content

Commit 07ec880

Browse files
committed
Merge pull request #31 from hdgarrood/fix-docs
Update complexities in documentation
2 parents ccb9af9 + 3589518 commit 07ec880

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

README.md

+18-8
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ head :: forall a. [a] -> Maybe a
4848

4949
Get the first element in an array, or `Nothing` if the array is empty
5050

51+
Running time: `O(1)`.
52+
5153
#### `last`
5254

5355
``` purescript
@@ -56,7 +58,7 @@ last :: forall a. [a] -> Maybe a
5658

5759
Get the last element in an array, or `Nothing` if the array is empty
5860

59-
Running time: `O(n)` where `n` is the length of the array
61+
Running time: `O(1)`.
6062

6163
#### `tail`
6264

@@ -226,7 +228,7 @@ creating a new array.
226228
(\\) :: forall a. (Eq a) => [a] -> [a] -> [a]
227229
```
228230

229-
Delete the first occurrence of each element in the second array from the first array,
231+
Delete the first occurrence of each element in the second array from the first array,
230232
creating a new array.
231233

232234
#### `intersectBy`
@@ -252,7 +254,7 @@ Calculate the intersection of two arrays, creating a new array.
252254
concatMap :: forall a b. (a -> [b]) -> [a] -> [b]
253255
```
254256

255-
Apply a function to each element in an array, and flatten the results
257+
Apply a function to each element in an array, and flatten the results
256258
into a single, new array.
257259

258260
#### `map`
@@ -287,7 +289,7 @@ a value, creating a new array.
287289
filter :: forall a. (a -> Boolean) -> [a] -> [a]
288290
```
289291

290-
Filter an array, keeping the elements which satisfy a predicate function,
292+
Filter an array, keeping the elements which satisfy a predicate function,
291293
creating a new array.
292294

293295
#### `range`
@@ -312,7 +314,7 @@ An infix synonym for `range`.
312314
zipWith :: forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
313315
```
314316

315-
Apply a function to pairs of elements at the same index in two arrays,
317+
Apply a function to pairs of elements at the same index in two arrays,
316318
collecting the results in a new array.
317319

318320
If one array is longer, elements will be discarded from the longer array.
@@ -337,7 +339,7 @@ Remove the duplicates from an array, creating a new array.
337339
nubBy :: forall a. (a -> a -> Boolean) -> [a] -> [a]
338340
```
339341

340-
Remove the duplicates from an array, where element equality is determined by the
342+
Remove the duplicates from an array, where element equality is determined by the
341343
specified equivalence relation, creating a new array.
342344

343345
#### `sort`
@@ -400,7 +402,7 @@ equivalence relation to detemine equality.
400402
span :: forall a. (a -> Boolean) -> [a] -> { rest :: [a], init :: [a] }
401403
```
402404

403-
Split an array into two parts:
405+
Split an array into two parts:
404406

405407
1. the longest initial subarray for which all element satisfy the specified predicate
406408
2. the remaining elements
@@ -630,6 +632,8 @@ head :: forall a. [a] -> a
630632

631633
Get the first element of a non-empty array.
632634

635+
Running time: `O(1)`.
636+
633637
#### `tail`
634638

635639
``` purescript
@@ -638,6 +642,8 @@ tail :: forall a. [a] -> [a]
638642

639643
Get all but the first element of a non-empty array.
640644

645+
Running time: `O(n)`, where `n` is the length of the array.
646+
641647
#### `last`
642648

643649
``` purescript
@@ -646,10 +652,14 @@ last :: forall a. [a] -> a
646652

647653
Get the last element of a non-empty array.
648654

655+
Running time: `O(1)`.
656+
649657
#### `init`
650658

651659
``` purescript
652660
init :: forall a. [a] -> [a]
653661
```
654662

655-
Get all but the last element of a non-empty array.
663+
Get all but the last element of a non-empty array.
664+
665+
Running time: `O(n)`, where `n` is the length of the array.

src/Data/Array.purs

+18-16
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,14 @@ singleton :: forall a. a -> [a]
9191
singleton a = [a]
9292

9393
-- | Get the first element in an array, or `Nothing` if the array is empty
94+
-- |
95+
-- | Running time: `O(1)`.
9496
head :: forall a. [a] -> Maybe a
9597
head xs = xs !! 0
9698

9799
-- | Get the last element in an array, or `Nothing` if the array is empty
98100
-- |
99-
-- | Running time: `O(n)` where `n` is the length of the array
101+
-- | Running time: `O(1)`.
100102
last :: forall a. [a] -> Maybe a
101103
last xs = xs !! (length xs - 1)
102104

@@ -268,7 +270,7 @@ delete = deleteBy (==)
268270

269271
infix 5 \\
270272

271-
-- | Delete the first occurrence of each element in the second array from the first array,
273+
-- | Delete the first occurrence of each element in the second array from the first array,
272274
-- | creating a new array.
273275
(\\) :: forall a. (Eq a) => [a] -> [a] -> [a]
274276
(\\) xs ys = go xs ys
@@ -290,7 +292,7 @@ intersectBy eq xs ys = filter el xs
290292
intersect :: forall a. (Eq a) => [a] -> [a] -> [a]
291293
intersect = intersectBy (==)
292294

293-
-- | Apply a function to each element in an array, and flatten the results
295+
-- | Apply a function to each element in an array, and flatten the results
294296
-- | into a single, new array.
295297
foreign import concatMap
296298
"function concatMap (f) {\
@@ -326,7 +328,7 @@ mapMaybe f = concatMap (maybe [] singleton <<< f)
326328
catMaybes :: forall a. [Maybe a] -> [a]
327329
catMaybes = concatMap (maybe [] singleton)
328330

329-
-- | Filter an array, keeping the elements which satisfy a predicate function,
331+
-- | Filter an array, keeping the elements which satisfy a predicate function,
330332
-- | creating a new array.
331333
foreign import filter
332334
"function filter (f) {\
@@ -363,7 +365,7 @@ infix 8 ..
363365
(..) :: Number -> Number -> [Number]
364366
(..) = range
365367

366-
-- | Apply a function to pairs of elements at the same index in two arrays,
368+
-- | Apply a function to pairs of elements at the same index in two arrays,
367369
-- | collecting the results in a new array.
368370
-- |
369371
-- | If one array is longer, elements will be discarded from the longer array.
@@ -391,7 +393,7 @@ foreign import zipWith
391393
nub :: forall a. (Eq a) => [a] -> [a]
392394
nub = nubBy (==)
393395

394-
-- | Remove the duplicates from an array, where element equality is determined by the
396+
-- | Remove the duplicates from an array, where element equality is determined by the
395397
-- | specified equivalence relation, creating a new array.
396398
nubBy :: forall a. (a -> a -> Boolean) -> [a] -> [a]
397399
nubBy _ [] = []
@@ -421,19 +423,19 @@ foreign import sortJS
421423
\}" :: forall a. (a -> a -> Number) -> [a] -> [a]
422424

423425
-- | Group equal, consecutive elements of an array into arrays.
424-
-- |
426+
-- |
425427
-- | For example,
426-
-- |
428+
-- |
427429
-- | ```purescript
428430
-- | group [1,1,2,2,1] == [[1,1],[2,2],[1]]
429431
-- | ```
430432
group :: forall a. (Eq a) => [a] -> [[a]]
431433
group xs = groupBy (==) xs
432434

433435
-- | Sort and group the elements of an array into arrays.
434-
-- |
436+
-- |
435437
-- | For example,
436-
-- |
438+
-- |
437439
-- | ```purescript
438440
-- | group [1,1,2,2,1] == [[1,1,1],[2,2]]
439441
-- | ```
@@ -450,13 +452,13 @@ groupBy = go []
450452
go acc op (x:xs) = let sp = span (op x) xs in
451453
go ((x:sp.init):acc) op sp.rest
452454

453-
-- | Split an array into two parts:
454-
-- |
455+
-- | Split an array into two parts:
456+
-- |
455457
-- | 1. the longest initial subarray for which all element satisfy the specified predicate
456458
-- | 2. the remaining elements
457-
-- |
459+
-- |
458460
-- | For example,
459-
-- |
461+
-- |
460462
-- | ```purescript
461463
-- | span (\n -> n % 2 == 1) [1,3,2,4,5] == { init: [1,3], rest: [2,4,5] }
462464
-- | ```
@@ -496,10 +498,10 @@ instance semigroupArray :: Semigroup [a] where
496498

497499
instance altArray :: Alt [] where
498500
(<|>) = append
499-
501+
500502
instance plusArray :: Plus [] where
501503
empty = []
502-
504+
503505
instance alternativeArray :: Alternative []
504506

505507
instance monadPlusArray :: MonadPlus []

src/Data/Array/Unsafe.purs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- | Unsafe helper functions for working with immutable arrays.
2-
-- |
2+
-- |
33
-- | _Note_: these functions should be used with care, and may result in unspecified
44
-- | behavior, including runtime exceptions.
55

@@ -10,17 +10,25 @@ import Data.Maybe.Unsafe
1010
import qualified Data.Array as A
1111

1212
-- | Get the first element of a non-empty array.
13+
-- |
14+
-- | Running time: `O(1)`.
1315
head :: forall a. [a] -> a
1416
head xs = unsafeIndex xs 0
1517

1618
-- | Get all but the first element of a non-empty array.
19+
-- |
20+
-- | Running time: `O(n)`, where `n` is the length of the array.
1721
tail :: forall a. [a] -> [a]
1822
tail (_ : xs) = xs
1923

2024
-- | Get the last element of a non-empty array.
25+
-- |
26+
-- | Running time: `O(1)`.
2127
last :: forall a. [a] -> a
2228
last xs = unsafeIndex xs (A.length xs - 1)
2329

2430
-- | Get all but the last element of a non-empty array.
31+
-- |
32+
-- | Running time: `O(n)`, where `n` is the length of the array.
2533
init :: forall a. [a] -> [a]
2634
init = fromJust <<< A.init

0 commit comments

Comments
 (0)