Skip to content

Commit bd1d9e7

Browse files
committed
Merge pull request #62 from sharkdp/negative-arguments
Treat negative arguments as zero, fixes #25
2 parents db95f35 + a4798de commit bd1d9e7

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src/Data/Array.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ exports.slice = function (s) {
243243
};
244244
};
245245

246+
exports.take = function (n) {
247+
return function (l) {
248+
return n < 1 ? [] : l.slice(0, n);
249+
};
250+
};
251+
246252
exports.drop = function (n) {
247253
return function (l) {
248254
return n < 1 ? l : l.slice(n);

src/Data/Array.purs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,7 @@ foreign import slice :: forall a. Int -> Int -> Array a -> Array a
441441

442442
-- | Keep only a number of elements from the start of an array, creating a new
443443
-- | array.
444-
take :: forall a. Int -> Array a -> Array a
445-
take = slice 0
444+
foreign import take :: forall a. Int -> Array a -> Array a
446445

447446
-- | Calculate the longest initial subarray for which all element satisfy the
448447
-- | specified predicate, creating a new array.

test/Test/Data/Array.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ testArray = do
233233
assert $ (dropWhile (_ /= 2) [1, 2, 3]) == [2, 3]
234234
assert $ (dropWhile (_ /= 1) nil) == nil
235235

236+
log "take and drop should treat negative arguments as zero"
237+
assert $ (take (-2) [1, 2, 3]) == nil
238+
assert $ (drop (-2) [1, 2, 3]) == [1, 2, 3]
239+
236240
log "span should split an array in two based on a predicate"
237241
let spanResult = span (_ < 4) [1, 2, 3, 4, 5, 6, 7]
238242
assert $ spanResult.init == [1, 2, 3]

0 commit comments

Comments
 (0)