Skip to content
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

Add unsafe versions of delete, insert, update, and modify #182

Closed
wants to merge 4 commits into from
Closed
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
38 changes: 38 additions & 0 deletions src/Data/Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ exports._insertAt = function (just) {
};
};

exports.unsafeInsertAtImpl = function (i) {
return function (a) {
return function (l) {
var l1 = l.slice();
l1.splice(i, 0, a);
return l1;
};
};
};

exports._deleteAt = function (just) {
return function (nothing) {
return function (i) {
Expand All @@ -161,6 +171,14 @@ exports._deleteAt = function (just) {
};
};

exports.unsafeDeleteAtImpl = function (i) {
return function (l) {
var l1 = l.slice();
l1.splice(i, 1);
return l1;
};
};

exports._updateAt = function (just) {
return function (nothing) {
return function (i) {
Expand All @@ -176,6 +194,26 @@ exports._updateAt = function (just) {
};
};

exports.unsafeUpdateAtImpl = function (i) {
return function (a) {
return function (l) {
var l1 = l.slice();
l1[i] = a;
return l1;
};
};
};

exports.unsafeModifyAtImpl = function (i) {
return function (f) {
return function (l) {
var l1 = l.slice();
l1[i] = f(l1[i]);
return l1;
};
};
};

//------------------------------------------------------------------------------
// Transformations -------------------------------------------------------------
//------------------------------------------------------------------------------
Expand Down
44 changes: 44 additions & 0 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ module Data.Array
, foldRecM

, unsafeIndex
, unsafeDeleteAt
, unsafeInsertAt
, unsafeUpdateAt
, unsafeModifyAt

, module Exports
) where
Expand Down Expand Up @@ -1145,3 +1149,43 @@ unsafeIndex :: forall a. Partial => Array a -> Int -> a
unsafeIndex = unsafeIndexImpl

foreign import unsafeIndexImpl :: forall a. Array a -> Int -> a

-- | Delete the element at index `i` in an array
-- |
-- | ```purescript
-- | unsafePartial $ unsafeDeleteAt 1 ["a", "b", "c"] = ["a", "c"]
-- | ```
unsafeDeleteAt :: forall a. Partial => Int -> Array a -> Array a
unsafeDeleteAt = unsafeDeleteAtImpl

foreign import unsafeDeleteAtImpl :: forall a. Int -> Array a -> Array a

-- | Insert the element at index `i` in an array
-- |
-- | ```purescript
-- | unsafePartial $ unsafeInsertAt 1 "b" ["a", "c"] = ["a", "b", "c"]
-- | ```
unsafeInsertAt :: forall a. Partial => Int -> a -> Array a -> Array a
unsafeInsertAt = unsafeInsertAtImpl

foreign import unsafeInsertAtImpl :: forall a. Int -> a -> Array a -> Array a

-- | Overwrite the element at index `i` in an array
-- |
-- | ```purescript
-- | unsafePartial $ unsafeUpdateAt 1 "e" ["a", "b", "c"] = ["a", "e", "c"]
-- | ```
unsafeUpdateAt :: forall a. Partial => Int -> a -> Array a -> Array a
unsafeUpdateAt = unsafeUpdateAtImpl

foreign import unsafeUpdateAtImpl :: forall a. Int -> a -> Array a -> Array a

-- | Overwrite the element at index `i` in an array
-- |
-- | ```purescript
-- | unsafePartial $ unsafeModifyAt 1 (\a -> a <> "e") ["a", "b", "c"] = ["a", "be", "c"]
-- | ```
unsafeModifyAt :: forall a. Partial => Int -> (a -> a) -> Array a -> Array a
unsafeModifyAt = unsafeModifyAtImpl

foreign import unsafeModifyAtImpl :: forall a. Int -> (a -> a) -> Array a -> Array a
16 changes: 16 additions & 0 deletions test/Test/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -155,27 +155,43 @@ testArray = do
log "insertAt should return Nothing if the index is out of A.range"
assert $ (A.insertAt 2 1 nil) == Nothing

log "unsafeInsertAt should insert the element into the array at the given index if the index is valid"
assert $ (unsafePartial $ A.unsafeInsertAt 0 1 [0]) == [1, 0]
assert $ (unsafePartial $ A.unsafeInsertAt 1 1 [0]) == [0, 1]

log "deleteAt should remove an item at the specified index"
assert $ (A.deleteAt 0 [1, 2, 3]) == Just [2, 3]
assert $ (A.deleteAt 1 [1, 2, 3]) == Just [1, 3]

log "deleteAt should return Nothing if the index is out of A.range"
assert $ (A.deleteAt 1 nil) == Nothing

log "unsafeDeleteAt should delete the element at the given index into the array"
assert $ (unsafePartial $ A.unsafeDeleteAt 0 [0, 1]) == [1]
assert $ (unsafePartial $ A.unsafeDeleteAt 1 [0, 1]) == [0]

log "updateAt should replace an item at the specified index"
assert $ (A.updateAt 0 9 [1, 2, 3]) == Just [9, 2, 3]
assert $ (A.updateAt 1 9 [1, 2, 3]) == Just [1, 9, 3]

log "updateAt should return Nothing if the index is out of A.range"
assert $ (A.updateAt 1 9 nil) == Nothing

log "unsafeUpdateAt should update the element at the given index into the array"
assert $ (unsafePartial $ A.unsafeUpdateAt 0 2 [0, 1]) == [2, 1]
assert $ (unsafePartial $ A.unsafeUpdateAt 1 2 [0, 1]) == [0, 2]

log "modifyAt should update an item at the specified index"
assert $ (A.modifyAt 0 (_ + 1) [1, 2, 3]) == Just [2, 2, 3]
assert $ (A.modifyAt 1 (_ + 1) [1, 2, 3]) == Just [1, 3, 3]

log "modifyAt should return Nothing if the index is out of A.range"
assert $ (A.modifyAt 1 (_ + 1) nil) == Nothing

log "unsafeModifyAt should update the element at the given index into the array"
assert $ (unsafePartial $ A.unsafeModifyAt 0 (_ + 1) [0, 1]) == [1, 1]
assert $ (unsafePartial $ A.unsafeModifyAt 1 (_ + 1) [0, 1]) == [0, 2]

log "alterAt should update an item at the specified index when the function returns Just"
assert $ (A.alterAt 0 (Just <<< (_ + 1)) [1, 2, 3]) == Just [2, 2, 3]
assert $ (A.alterAt 1 (Just <<< (_ + 1)) [1, 2, 3]) == Just [1, 3, 3]
Expand Down