Data.Array
- Package
- purescript-arrays
- Repository
- purescript/purescript-arrays
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.
In addition to the functions in this module, Arrays have a number of useful instances:
Functor
, which providesmap :: forall a b. (a -> b) -> Array a -> Array b
Apply
, which provides(<*>) :: forall a b. Array (a -> b) -> Array a -> Array b
. This function works a bit like a Cartesian product; the result array is constructed by applying each function in the first array to each value in the second, so that the result array ends up with a length equal to the product of the two arguments' lengths.Bind
, which provides(>>=) :: forall a b. (a -> Array b) -> Array a -> Array b
(this is the same asconcatMap
).Semigroup
, which provides(<>) :: forall a. Array a -> Array a -> Array a
, for concatenating arrays.Foldable
, which provides a slew of functions for folding (also known as reducing) arrays down to one value. For example,Data.Foldable.or
tests whether an array ofBoolean
values contains at least onetrue
value.Traversable
, which provides the PureScript version of a for-loop, allowing you to iterate over an array and accumulate effects.
#toUnfoldable Source
toUnfoldable :: forall f. Unfoldable f => Array ~> f
Convert an Array
into an Unfoldable
structure.
#fromFoldable Source
fromFoldable :: forall f. Foldable f => f ~> Array
Convert a Foldable
structure into an Array
.
#(..) Source
Operator alias for Data.Array.range (non-associative / precedence 8)
An infix synonym for range
.
#some Source
some :: forall a f. 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 Source
many :: forall a f. 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.
#(:) Source
Operator alias for Data.Array.cons (right-associative / precedence 6)
An infix alias for cons
.
Note, the running time of this function is O(n)
.
#uncons Source
uncons :: forall a. Array a -> Maybe { head :: a, tail :: Array a }
Break an array into its first element and remaining elements.
Using uncons
provides a way of writing code that would use cons patterns
in Haskell or pre-PureScript 0.7:
f (x : xs) = something
f [] = somethingElse
Becomes:
f arr = case uncons arr of
Just { head: x, tail: xs } -> something
Nothing -> somethingElse
#(!!) Source
Operator alias for Data.Array.index (left-associative / precedence 8)
An infix version of index
.
#elemLastIndex Source
elemLastIndex :: forall a. Eq a => a -> Array a -> Maybe Int
Find the index of the last element equal to the specified element.
#findLastIndex Source
findLastIndex :: forall a. (a -> Boolean) -> Array a -> Maybe Int
Find the last index for which a predicate holds.
#filterA Source
filterA :: forall f a. Applicative f => (a -> f Boolean) -> Array a -> f (Array a)
Filter where the predicate returns a Boolean
in some Applicative
.
powerSet :: forall a. Array a -> Array (Array a)
powerSet = filterA (const [true, false])
#mapWithIndex Source
mapWithIndex :: forall b a. (Int -> a -> b) -> Array a -> Array b
Apply a function to each element in an array, supplying a generated zero-based index integer along with the element, creating an array with the new elements.
#span Source
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 elements satisfy the specified predicate
- the remaining elements
span (\n -> n % 2 == 1) [1,3,2,4,5] == { init: [1,3], rest: [2,4,5] }
Running time: O(n)
.
#difference Source
difference :: forall a. Eq a => Array a -> Array a -> Array a
Delete the first occurrence of each element in the second array from the first array, creating a new array.
Running time: O(n*m)
, where n is the length of the first array, and m is
the length of the second.
#intersectBy Source
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. Note that duplicates in the first array are preserved while duplicates in the second array are removed.
#zipWith Source
zipWith :: forall c b a. (a -> b -> c) -> Array a -> Array b -> Array c
Apply a function to pairs of elements at the same index in two arrays, collecting the results in a new array.
If one array is longer, elements will be discarded from the longer array.
For example
zipWith (*) [1, 2, 3] [4, 5, 6, 7] == [4, 10, 18]
#zipWithA Source
zipWithA :: forall c b a m. Applicative m => (a -> b -> m c) -> Array a -> Array b -> m (Array c)
A generalization of zipWith
which accumulates results in some
Applicative
functor.
#unsafeIndex Source
unsafeIndex :: forall a. Partial => Array a -> Int -> a
Find the element of an array at the specified index.
Re-exports from Data.Foldable
#intercalate Source
intercalate :: forall m f. Foldable f => Monoid m => m -> f m -> m
Fold a data structure, accumulating values in some Monoid
,
combining adjacent elements using the specified separator.
#any Source
any :: forall f b a. Foldable f => HeytingAlgebra b => (a -> b) -> f a -> b
any f
is the same as or <<< map f
; map a function over the structure,
and then get the disjunction of the results.
#all Source
all :: forall f b a. Foldable f => HeytingAlgebra b => (a -> b) -> f a -> b
all f
is the same as and <<< map f
; map a function over the structure,
and then get the conjunction of the results.
Re-exports from Data.Traversable
#scanr Source
scanr :: forall f b a. Traversable f => (a -> b -> b) -> b -> f a -> f b
Fold a data structure from the right, keeping all intermediate results
instead of only the final result. Note that the initial value does not
appear in the result (unlike Haskell's Prelude.scanr
).
scanr (+) 0 [1,2,3] = [1,3,6]
scanr (flip (-)) 10 [1,2,3] = [4,5,7]
#scanl Source
scanl :: forall f b a. Traversable f => (b -> a -> b) -> b -> f a -> f b
Fold a data structure from the left, keeping all intermediate results
instead of only the final result. Note that the initial value does not
appear in the result (unlike Haskell's Prelude.scanl
).
scanl (+) 0 [1,2,3] = [1,3,6]
scanl (-) 10 [1,2,3] = [9,7,4]