Skip to content

Commit 603c332

Browse files
authored
replace foreign cons, snoc, drop, take with purescript implementations (#180)
1 parent b90334b commit 603c332

File tree

2 files changed

+9
-35
lines changed

2 files changed

+9
-35
lines changed

src/Data/Array.js

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,6 @@ exports.length = function (xs) {
8181
return xs.length;
8282
};
8383

84-
//------------------------------------------------------------------------------
85-
// Extending arrays ------------------------------------------------------------
86-
//------------------------------------------------------------------------------
87-
88-
exports.cons = function (e) {
89-
return function (l) {
90-
return [e].concat(l);
91-
};
92-
};
93-
94-
exports.snoc = function (l) {
95-
return function (e) {
96-
var l1 = l.slice();
97-
l1.push(e);
98-
return l1;
99-
};
100-
};
101-
10284
//------------------------------------------------------------------------------
10385
// Non-indexed reads -----------------------------------------------------------
10486
//------------------------------------------------------------------------------
@@ -264,18 +246,6 @@ exports.slice = function (s) {
264246
};
265247
};
266248

267-
exports.take = function (n) {
268-
return function (l) {
269-
return n < 1 ? [] : l.slice(0, n);
270-
};
271-
};
272-
273-
exports.drop = function (n) {
274-
return function (l) {
275-
return n < 1 ? l : l.slice(n);
276-
};
277-
};
278-
279249
//------------------------------------------------------------------------------
280250
// Zipping ---------------------------------------------------------------------
281251
//------------------------------------------------------------------------------

src/Data/Array.purs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ import Control.Alternative (class Alternative)
122122
import Control.Lazy (class Lazy, defer)
123123
import Control.Monad.Rec.Class (class MonadRec, Step(..), tailRecM2)
124124
import Control.Monad.ST as ST
125+
import Data.Array.NonEmpty.Internal (NonEmptyArray)
125126
import Data.Array.ST as STA
126127
import Data.Array.ST.Iterator as STAI
127-
import Data.Array.NonEmpty.Internal (NonEmptyArray)
128128
import Data.Foldable (class Foldable, foldl, foldr, traverse_)
129129
import Data.Foldable (foldl, foldr, foldMap, fold, intercalate, elem, notElem, find, findMap, any, all) as Exports
130130
import Data.Maybe (Maybe(..), maybe, isJust, fromJust)
@@ -229,7 +229,8 @@ foreign import length :: forall a. Array a -> Int
229229
-- | ```
230230
-- |
231231
-- | Note, the running time of this function is `O(n)`.
232-
foreign import cons :: forall a. a -> Array a -> Array a
232+
cons :: forall a. a -> Array a -> Array a
233+
cons x xs = [x] <> xs
233234

234235
-- | An infix alias for `cons`.
235236
-- |
@@ -246,7 +247,8 @@ infixr 6 cons as :
246247
-- | snoc [1, 2, 3] 4 = [1, 2, 3, 4]
247248
-- | ```
248249
-- |
249-
foreign import snoc :: forall a. Array a -> a -> Array a
250+
snoc :: forall a. Array a -> a -> Array a
251+
snoc xs x = ST.run (STA.withArray (STA.push x) xs)
250252

251253
-- | Insert an element into a sorted array.
252254
-- |
@@ -742,7 +744,8 @@ foreign import slice :: forall a. Int -> Int -> Array a -> Array a
742744
-- | take 100 letters = ["a", "b", "c"]
743745
-- | ```
744746
-- |
745-
foreign import take :: forall a. Int -> Array a -> Array a
747+
take :: forall a. Int -> Array a -> Array a
748+
take n xs = if n < 1 then [] else slice 0 n xs
746749

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

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

0 commit comments

Comments
 (0)