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 :: forall a. a -> Array arange :: Int -> Int -> Array IntCreate an array containing a range of integers, including both endpoints.
(..) :: Int -> Int -> Array Intnon-associative / precedence 8
An infix synonym for range.
replicate :: forall a. Int -> a -> Array aCreate an array with repeated instances of a value.
replicateM :: forall m a. (Monad m) => Int -> m a -> m (Array a)Perform a monadic action n times collecting all of the results.
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 :: 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 :: forall a. Array a -> Booleanlength :: forall a. Array a -> IntGet the number of elements in an array.
cons :: forall a. a -> Array a -> Array a(:) :: forall a. a -> Array a -> Array aright-associative / precedence 6
An infix alias for cons.
Note, the running time of this function is O(n).
snoc :: forall a. Array a -> a -> Array aAppend an element to the end of an array, creating a new array.
insert :: forall a. (Ord a) => a -> Array a -> Array aInsert an element into a sorted array.
insertBy :: forall a. (a -> a -> Ordering) -> a -> Array a -> Array aInsert an element into a sorted array, using the specified function to determine the ordering of elements.
head :: forall a. Array a -> Maybe alast :: forall a. Array a -> Maybe aGet the last element in an array, or Nothing if the array is empty
Running time: O(1).
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 :: 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 :: forall a. Array a -> Maybe { head :: a, tail :: Array a }Break an array into its first element, and the remaining elements
index :: forall a. Array a -> Int -> Maybe a(!!) :: forall a. Array a -> Int -> Maybe aleft-associative / precedence 8
An infix version of index.
elemIndex :: forall a. (Eq a) => a -> Array a -> Maybe IntFind the index of the first element equal to the specified element.
elemLastIndex :: forall a. (Eq a) => a -> Array a -> Maybe IntFind the index of the last element equal to the specified element.
findIndex :: forall a. (a -> Boolean) -> Array a -> Maybe IntFind the first index for which a predicate holds.
findLastIndex :: forall a. (a -> Boolean) -> Array a -> Maybe IntFind the last index for which a predicate holds.
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 :: 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 :: 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 :: 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 :: 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 :: forall a. Array a -> Array aconcat :: forall a. Array (Array a) -> Array aFlatten an array of arrays, creating a new array.
concatMap :: forall a b. (a -> Array b) -> Array a -> Array bApply a function to each element in an array, and flatten the results into a single, new array.
filter :: forall a. (a -> Boolean) -> Array a -> Array aFilter an array, keeping the elements which satisfy a predicate function, creating a new array.
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 :: forall a b. (a -> Maybe b) -> Array a -> Array bApply a function to each element in an array, keeping only the results which contain a value, creating a new array.
catMaybes :: forall a. Array (Maybe a) -> Array aFilter an array of optional values, keeping only the elements which contain a value, creating a new array.
sort :: forall a. (Ord a) => Array a -> Array asortBy :: forall a. (a -> a -> Ordering) -> Array a -> Array aSort the elements of an array in increasing order, where elements are compared using the specified partial ordering, creating a new array.
slice :: forall a. Int -> Int -> Array a -> Array atake :: forall a. Int -> Array a -> Array aKeep only a number of elements from the start of an array, creating a new array.
takeWhile :: forall a. (a -> Boolean) -> Array a -> Array aCalculate the longest initial subarray for which all element satisfy the specified predicate, creating a new array.
drop :: forall a. Int -> Array a -> Array aDrop a number of elements from the start of an array, creating a new array.
dropWhile :: forall a. (a -> Boolean) -> Array a -> Array aRemove the longest initial subarray for which all element satisfy the specified predicate, creating a new array.
span :: forall a. (a -> Boolean) -> Array a -> { init :: Array a, rest :: Array a }Split an array into two parts:
- the longest initial subarray for which all element satisfy the specified predicate
- the remaining elements
span (\n -> n % 2 == 1) [1,3,2,4,5] == { init: [1,3], rest: [2,4,5] }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' :: 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 :: 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 :: forall a. (Eq a) => Array a -> Array anubBy :: forall a. (a -> a -> Boolean) -> Array a -> Array aRemove the duplicates from an array, where element equality is determined by the specified equivalence relation, creating a new array.
union :: forall a. (Eq a) => Array a -> Array a -> Array aCalculate the union of two lists.
Running time: O(n^2)
unionBy :: forall a. (a -> a -> Boolean) -> Array a -> Array a -> Array aCalculate the union of two arrays, using the specified function to determine equality of elements.
delete :: forall a. (Eq a) => a -> Array a -> Array aDelete the first element of an array which is equal to the specified value, creating a new array.
deleteBy :: forall a. (a -> a -> Boolean) -> a -> Array a -> Array aDelete 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 anon-associative / precedence 5
Delete the first occurrence of each element in the second array from the first array, creating a new array.
intersect :: forall a. (Eq a) => Array a -> Array a -> Array aCalculate the intersection of two arrays, creating a new array.
intersectBy :: forall a. (a -> a -> Boolean) -> Array a -> Array a -> Array aCalculate the intersection of two arrays, using the specified equivalence relation to compare elements, creating a new array.
zipWith :: forall a b c. (a -> b -> c) -> Array a -> Array b -> Array czipWithA :: 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 :: 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 :: 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 :: forall m a b. (Monad m) => (a -> b -> m a) -> a -> Array b -> m a