/
Partial.purs
35 lines (29 loc) · 937 Bytes
/
Partial.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
-- | Partial helper functions for working with immutable arrays.
module Data.Array.Partial
( head
, tail
, last
, init
) where
import Prelude
import Data.Array (length, slice, unsafeIndex)
-- | Get the first element of a non-empty array.
-- |
-- | Running time: `O(1)`.
head :: forall a. Partial => Array a -> a
head xs = unsafeIndex xs 0
-- | Get all but the first element of a non-empty array.
-- |
-- | Running time: `O(n)`, where `n` is the length of the array.
tail :: forall a. Partial => Array a -> Array a
tail xs = slice 1 (length xs) xs
-- | Get the last element of a non-empty array.
-- |
-- | Running time: `O(1)`.
last :: forall a. Partial => Array a -> a
last xs = unsafeIndex xs (length xs - 1)
-- | Get all but the last element of a non-empty array.
-- |
-- | Running time: `O(n)`, where `n` is the length of the array.
init :: forall a. Partial => Array a -> Array a
init xs = slice 0 (length xs - 1) xs