Skip to content

replace FFI cons, snoc, drop, take with purescript implementations #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 0 additions & 30 deletions src/Data/Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,6 @@ exports.length = function (xs) {
return xs.length;
};

//------------------------------------------------------------------------------
// Extending arrays ------------------------------------------------------------
//------------------------------------------------------------------------------

exports.cons = function (e) {
return function (l) {
return [e].concat(l);
};
};

exports.snoc = function (l) {
return function (e) {
var l1 = l.slice();
l1.push(e);
return l1;
};
};

//------------------------------------------------------------------------------
// Non-indexed reads -----------------------------------------------------------
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -264,18 +246,6 @@ exports.slice = function (s) {
};
};

exports.take = function (n) {
return function (l) {
return n < 1 ? [] : l.slice(0, n);
};
};

exports.drop = function (n) {
return function (l) {
return n < 1 ? l : l.slice(n);
};
};

//------------------------------------------------------------------------------
// Zipping ---------------------------------------------------------------------
//------------------------------------------------------------------------------
Expand Down
14 changes: 9 additions & 5 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ import Control.Alternative (class Alternative)
import Control.Lazy (class Lazy, defer)
import Control.Monad.Rec.Class (class MonadRec, Step(..), tailRecM2)
import Control.Monad.ST as ST
import Data.Array.NonEmpty.Internal (NonEmptyArray)
import Data.Array.ST as STA
import Data.Array.ST.Iterator as STAI
import Data.Array.NonEmpty.Internal (NonEmptyArray)
import Data.Foldable (class Foldable, foldl, foldr, traverse_)
import Data.Foldable (foldl, foldr, foldMap, fold, intercalate, elem, notElem, find, findMap, any, all) as Exports
import Data.Maybe (Maybe(..), maybe, isJust, fromJust)
Expand Down Expand Up @@ -229,7 +229,8 @@ foreign import length :: forall a. Array a -> Int
-- | ```
-- |
-- | Note, the running time of this function is `O(n)`.
foreign import cons :: forall a. a -> Array a -> Array a
cons :: forall a. a -> Array a -> Array a
cons x xs = [x] <> xs

-- | An infix alias for `cons`.
-- |
Expand All @@ -246,7 +247,8 @@ infixr 6 cons as :
-- | snoc [1, 2, 3] 4 = [1, 2, 3, 4]
-- | ```
-- |
foreign import snoc :: forall a. Array a -> a -> Array a
snoc :: forall a. Array a -> a -> Array a
snoc xs x = ST.run (STA.withArray (STA.push x) xs)

-- | Insert an element into a sorted array.
-- |
Expand Down Expand Up @@ -742,7 +744,8 @@ foreign import slice :: forall a. Int -> Int -> Array a -> Array a
-- | take 100 letters = ["a", "b", "c"]
-- | ```
-- |
foreign import take :: forall a. Int -> Array a -> Array a
take :: forall a. Int -> Array a -> Array a
take n xs = if n < 1 then [] else slice 0 n xs

-- | Keep only a number of elements from the end of an array, creating a new
-- | array.
Expand Down Expand Up @@ -777,7 +780,8 @@ takeWhile p xs = (span p xs).init
-- | drop 10 letters = []
-- | ```
-- |
foreign import drop :: forall a. Int -> Array a -> Array a
drop :: forall a. Int -> Array a -> Array a
drop n xs = if n < 1 then xs else slice n (length xs) xs

-- | Drop a number of elements from the end of an array, creating a new array.
-- |
Expand Down