Skip to content

Latest commit

 

History

History
557 lines (362 loc) · 11.3 KB

File metadata and controls

557 lines (362 loc) · 11.3 KB

Module Data.Array

Helper functions for working with immutable Javascript arrays.

Note: Depending on your use-case, you may prefer to use Data.List or Data.Sequence instead, which might give better performance for certain use cases. This module is useful when integrating with JavaScript libraries which use arrays, but immutable arrays are not a practical data structure for many use cases due to their poor asymptotics.

singleton

singleton :: forall a. a -> Array a

range

range :: Int -> Int -> Array Int

Create an array containing a range of integers, including both endpoints.

(..)

(..) :: Int -> Int -> Array Int

non-associative / precedence 8

An infix synonym for range.

replicate

replicate :: forall a. Int -> a -> Array a

Create an array with repeated instances of a value.

replicateM

replicateM :: forall m a. (Monad m) => Int -> m a -> m (Array a)

Perform a monadic action n times collecting all of the results.

some

some :: forall f a. (Alternative f, Lazy (f (Array a))) => f a -> f (Array a)

Attempt a computation multiple times, requiring at least one success.

The Lazy constraint is used to generate the result lazily, to ensure termination.

many

many :: forall f a. (Alternative f, Lazy (f (Array a))) => f a -> f (Array a)

Attempt a computation multiple times, returning as many successful results as possible (possibly zero).

The Lazy constraint is used to generate the result lazily, to ensure termination.

null

null :: forall a. Array a -> Boolean

length

length :: forall a. Array a -> Int

Get the number of elements in an array.

cons

cons :: forall a. a -> Array a -> Array a

(:)

(:) :: forall a. a -> Array a -> Array a

right-associative / precedence 6

An infix alias for cons.

Note, the running time of this function is O(n).

snoc

snoc :: forall a. Array a -> a -> Array a

Append an element to the end of an array, creating a new array.

insert

insert :: forall a. (Ord a) => a -> Array a -> Array a

Insert an element into a sorted array.

insertBy

insertBy :: forall a. (a -> a -> Ordering) -> a -> Array a -> Array a

Insert an element into a sorted array, using the specified function to determine the ordering of elements.

head

head :: forall a. Array a -> Maybe a

last

last :: forall a. Array a -> Maybe a

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

Running time: O(1).

tail

tail :: forall a. Array a -> Maybe (Array a)

Get all but the first element of an array, creating a new array, or Nothing if the array is empty

Running time: O(n) where n is the length of the array

init

init :: forall a. Array a -> Maybe (Array a)

Get all but the last element of an array, creating a new array, or Nothing if the array is empty.

Running time: O(n) where n is the length of the array

uncons

uncons :: forall a. Array a -> Maybe { head :: a, tail :: Array a }

Break an array into its first element, and the remaining elements

index

index :: forall a. Array a -> Int -> Maybe a

(!!)

(!!) :: forall a. Array a -> Int -> Maybe a

left-associative / precedence 8

An infix version of index.

elemIndex

elemIndex :: forall a. (Eq a) => a -> Array a -> Maybe Int

Find the index of the first element equal to the specified element.

elemLastIndex

elemLastIndex :: forall a. (Eq a) => a -> Array a -> Maybe Int

Find the index of the last element equal to the specified element.

findIndex

findIndex :: forall a. (a -> Boolean) -> Array a -> Maybe Int

Find the first index for which a predicate holds.

findLastIndex

findLastIndex :: forall a. (a -> Boolean) -> Array a -> Maybe Int

Find the last index for which a predicate holds.

insertAt

insertAt :: forall a. Int -> a -> Array a -> Maybe (Array a)

Insert an element at the specified index, creating a new array, or returning Nothing if the index is out of bounds.

deleteAt

deleteAt :: forall a. Int -> Array a -> Maybe (Array a)

Delete the element at the specified index, creating a new array, or returning Nothing if the index is out of bounds.

updateAt

updateAt :: forall a. Int -> a -> Array a -> Maybe (Array a)

Change the element at the specified index, creating a new array, or returning Nothing if the index is out of bounds.

modifyAt

modifyAt :: forall a. Int -> (a -> a) -> Array a -> Maybe (Array a)

Apply a function to the element at the specified index, creating a new array, or returning Nothing if the index is out of bounds.

alterAt

alterAt :: forall a. Int -> (a -> Maybe a) -> Array a -> Maybe (Array a)

Update or delete the element at the specified index by applying a function to the current value, returning a new array or Nothing if the index is out-of-bounds.

reverse

reverse :: forall a. Array a -> Array a

concat

concat :: forall a. Array (Array a) -> Array a

Flatten an array of arrays, creating a new array.

concatMap

concatMap :: forall a b. (a -> Array b) -> Array a -> Array b

Apply a function to each element in an array, and flatten the results into a single, new array.

filter

filter :: forall a. (a -> Boolean) -> Array a -> Array a

Filter an array, keeping the elements which satisfy a predicate function, creating a new array.

filterM

filterM :: forall a m. (Monad m) => (a -> m Boolean) -> Array a -> m (Array a)

Filter where the predicate returns a monadic Boolean.

powerSet :: forall a. [a] -> [[a]]
powerSet = filterM (const [true, false])

mapMaybe

mapMaybe :: forall a b. (a -> Maybe b) -> Array a -> Array b

Apply a function to each element in an array, keeping only the results which contain a value, creating a new array.

catMaybes

catMaybes :: forall a. Array (Maybe a) -> Array a

Filter an array of optional values, keeping only the elements which contain a value, creating a new array.

sort

sort :: forall a. (Ord a) => Array a -> Array a

sortBy

sortBy :: forall a. (a -> a -> Ordering) -> Array a -> Array a

Sort the elements of an array in increasing order, where elements are compared using the specified partial ordering, creating a new array.

slice

slice :: forall a. Int -> Int -> Array a -> Array a

take

take :: forall a. Int -> Array a -> Array a

Keep only a number of elements from the start of an array, creating a new array.

takeWhile

takeWhile :: forall a. (a -> Boolean) -> Array a -> Array a

Calculate the longest initial subarray for which all element satisfy the specified predicate, creating a new array.

drop

drop :: forall a. Int -> Array a -> Array a

Drop a number of elements from the start of an array, creating a new array.

dropWhile

dropWhile :: forall a. (a -> Boolean) -> Array a -> Array a

Remove the longest initial subarray for which all element satisfy the specified predicate, creating a new array.

span

span :: forall a. (a -> Boolean) -> Array a -> { init :: Array a, rest :: Array a }

Split an array into two parts:

  1. the longest initial subarray for which all element satisfy the specified predicate
  2. the remaining elements
span (\n -> n % 2 == 1) [1,3,2,4,5] == { init: [1,3], rest: [2,4,5] }

group

group :: forall a. (Eq a) => Array a -> Array (Array a)

Group equal, consecutive elements of an array into arrays.

group [1,1,2,2,1] == [[1,1],[2,2],[1]]

group'

group' :: forall a. (Ord a) => Array a -> Array (Array a)

Sort and then group the elements of an array into arrays.

group' [1,1,2,2,1] == [[1,1,1],[2,2]]

groupBy

groupBy :: forall a. (a -> a -> Boolean) -> Array a -> Array (Array a)

Group equal, consecutive elements of an array into arrays, using the specified equivalence relation to detemine equality.

nub

nub :: forall a. (Eq a) => Array a -> Array a

nubBy

nubBy :: forall a. (a -> a -> Boolean) -> Array a -> Array a

Remove the duplicates from an array, where element equality is determined by the specified equivalence relation, creating a new array.

union

union :: forall a. (Eq a) => Array a -> Array a -> Array a

Calculate the union of two lists.

Running time: O(n^2)

unionBy

unionBy :: forall a. (a -> a -> Boolean) -> Array a -> Array a -> Array a

Calculate the union of two arrays, using the specified function to determine equality of elements.

delete

delete :: forall a. (Eq a) => a -> Array a -> Array a

Delete the first element of an array which is equal to the specified value, creating a new array.

deleteBy

deleteBy :: forall a. (a -> a -> Boolean) -> a -> Array a -> Array a

Delete the first element of an array which matches the specified value, under the equivalence relation provided in the first argument, creating a new array.

(\\)

(\\) :: forall a. (Eq a) => Array a -> Array a -> Array a

non-associative / precedence 5

Delete the first occurrence of each element in the second array from the first array, creating a new array.

intersect

intersect :: forall a. (Eq a) => Array a -> Array a -> Array a

Calculate the intersection of two arrays, creating a new array.

intersectBy

intersectBy :: forall a. (a -> a -> Boolean) -> Array a -> Array a -> Array a

Calculate the intersection of two arrays, using the specified equivalence relation to compare elements, creating a new array.

zipWith

zipWith :: forall a b c. (a -> b -> c) -> Array a -> Array b -> Array c

zipWithA

zipWithA :: forall m a b c. (Applicative m) => (a -> b -> m c) -> Array a -> Array b -> m (Array c)

A generalization of zipWith which accumulates results in some Applicative functor.

zip

zip :: forall a b. Array a -> Array b -> Array (Tuple a b)

Rakes two lists and returns a list of corresponding pairs. If one input list is short, excess elements of the longer list are discarded.

unzip

unzip :: forall a b. Array (Tuple a b) -> Tuple (Array a) (Array b)

Transforms a list of pairs into a list of first components and a list of second components.

foldM

foldM :: forall m a b. (Monad m) => (a -> b -> m a) -> a -> Array b -> m a