diff --git a/.jshintrc b/.jshintrc index f3911591..2240be2a 100644 --- a/.jshintrc +++ b/.jshintrc @@ -5,7 +5,7 @@ "freeze": true, "funcscope": true, "futurehostile": true, - "globalstrict": true, + "strict": "global", "latedef": true, "maxparams": 1, "noarg": true, diff --git a/docs/Data/Array.md b/docs/Data/Array.md index 4dd508c9..16538abd 100644 --- a/docs/Data/Array.md +++ b/docs/Data/Array.md @@ -101,6 +101,8 @@ termination. null :: forall a. Array a -> Boolean ``` +Test whether an array is empty. + #### `length` ``` purescript @@ -115,6 +117,14 @@ Get the number of elements in an array. cons :: forall a. a -> Array a -> Array a ``` +Attaches an element to the front of an array, creating a new array. + +```purescript +cons 1 [2, 3, 4] = [1, 2, 3, 4] +``` + +Note, the running time of this function is `O(n)`. + #### `(:)` ``` purescript @@ -158,6 +168,10 @@ determine the ordering of elements. head :: forall a. Array a -> Maybe a ``` +Get the first element in an array, or `Nothing` if the array is empty + +Running time: `O(1)`. + #### `last` ``` purescript @@ -215,6 +229,9 @@ f arr = case uncons arr of index :: forall a. Array a -> Int -> Maybe a ``` +This function provides a safe way to read a value at a particular index +from an array. + #### `(!!)` ``` purescript @@ -309,6 +326,8 @@ index is out-of-bounds. reverse :: forall a. Array a -> Array a ``` +Reverse an array, creating a new array. + #### `concat` ``` purescript @@ -335,6 +354,16 @@ filter :: forall a. (a -> Boolean) -> Array a -> Array a Filter an array, keeping the elements which satisfy a predicate function, creating a new array. +#### `partition` + +``` purescript +partition :: forall a. (a -> Boolean) -> Array a -> { yes :: Array a, no :: Array a } +``` + +Partition an array using a predicate function, creating a set of +new arrays. One for the values satisfying the predicate function +and one for values that don't. + #### `filterM` ``` purescript @@ -372,6 +401,8 @@ a value, creating a new array. sort :: forall a. (Ord a) => Array a -> Array a ``` +Sort the elements of an array in increasing order, creating a new array. + #### `sortBy` ``` purescript @@ -387,6 +418,8 @@ the specified partial ordering, creating a new array. slice :: forall a. Int -> Int -> Array a -> Array a ``` +Extract a subarray by a start and end index. + #### `take` ``` purescript diff --git a/src/Data/Array.js b/src/Data/Array.js index 847b2e96..f85cea7e 100644 --- a/src/Data/Array.js +++ b/src/Data/Array.js @@ -174,6 +174,21 @@ exports.filter = function (f) { }; }; +exports.partition = function (f) { + return function (xs) { + var yes = []; + var no = []; + for (var i = 0; i < xs.length; i++) { + var x = xs[i]; + if (f(x)) + yes.push(x); + else + no.push(x); + } + return { yes: yes, no: no }; + }; +}; + //------------------------------------------------------------------------------ // Sorting --------------------------------------------------------------------- //------------------------------------------------------------------------------ diff --git a/src/Data/Array.purs b/src/Data/Array.purs index a8185bbb..0498a9ea 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -64,6 +64,7 @@ module Data.Array , concat , concatMap , filter + , partition , filterM , mapMaybe , catMaybes @@ -369,6 +370,13 @@ concatMap = flip bind -- | creating a new array. foreign import filter :: forall a. (a -> Boolean) -> Array a -> Array a +-- | Partition an array using a predicate function, creating a set of +-- | new arrays. One for the values satisfying the predicate function +-- | and one for values that don't. +foreign import partition :: forall a. (a -> Boolean) + -> Array a + -> { yes :: Array a, no :: Array a } + -- | Filter where the predicate returns a monadic `Boolean`. -- | -- | ```purescript