-
Notifications
You must be signed in to change notification settings - Fork 70
Add short circuiting functions of elem
, notElem
, find
, findMap
, scanl
, and scanr
#189
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
Changes from all commits
2e5fe5e
b65647d
32fab1d
466d6ef
5a7e0dd
3052bee
fe8e0b7
5d822bc
654aaa0
8148cf3
58422ad
d27fd68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import Data.Array as A | |
import Data.Array.NonEmpty as NEA | ||
import Data.Const (Const(..)) | ||
import Data.Foldable (for_, foldMapDefaultR, class Foldable, all, traverse_) | ||
import Data.Traversable (scanl, scanr) | ||
import Data.Maybe (Maybe(..), isNothing, fromJust) | ||
import Data.Tuple (Tuple(..)) | ||
import Data.Unfoldable (replicateA) | ||
|
@@ -131,6 +132,14 @@ testArray = do | |
assert $ [1, 2, 3] !! 6 == Nothing | ||
assert $ [1, 2, 3] !! (-1) == Nothing | ||
|
||
log "elem should return true if the array contains the given element at least once" | ||
assert $ (A.elem 1 [1, 2, 1]) == true | ||
assert $ (A.elem 4 [1, 2, 1]) == false | ||
|
||
log "notElem should return true if the array does not contain the given element" | ||
assert $ (A.notElem 1 [1, 2, 1]) == false | ||
assert $ (A.notElem 4 [1, 2, 1]) == true | ||
|
||
log "elemIndex should return the index of an item that a predicate returns true for in an array" | ||
assert $ (A.elemIndex 1 [1, 2, 1]) == Just 0 | ||
assert $ (A.elemIndex 4 [1, 2, 1]) == Nothing | ||
|
@@ -139,6 +148,15 @@ testArray = do | |
assert $ (A.elemLastIndex 1 [1, 2, 1]) == Just 2 | ||
assert $ (A.elemLastIndex 4 [1, 2, 1]) == Nothing | ||
|
||
log "find should return the first element for which a predicate returns true in an array" | ||
assert $ (A.find (_ /= 1) [1, 2, 1]) == Just 2 | ||
assert $ (A.find (_ == 3) [1, 2, 1]) == Nothing | ||
|
||
log "findMap should return the mapping of the first element that satisfies the given predicate" | ||
assert $ (A.findMap (\x -> if x > 3 then Just x else Nothing) [1, 2, 4]) == Just 4 | ||
assert $ (A.findMap (\x -> if x > 3 then Just x else Nothing) [1, 2, 1]) == Nothing | ||
assert $ (A.findMap (\x -> if x > 3 then Just x else Nothing) [4, 1, 5]) == Just 4 | ||
|
||
log "findIndex should return the index of an item that a predicate returns true for in an array" | ||
assert $ (A.findIndex (_ /= 1) [1, 2, 1]) == Just 1 | ||
assert $ (A.findIndex (_ == 3) [1, 2, 1]) == Nothing | ||
|
@@ -237,6 +255,22 @@ testArray = do | |
assert $ A.modifyAtIndices [0, 2, 8] not [true, true, true, true] == | ||
[false, true, false, true] | ||
|
||
log "scanl should return an array that stores the accumulated value at each step" | ||
assert $ A.scanl (+) 0 [1,2,3] == [1, 3, 6] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we test that these agree with the Foldable versions please? It's probably not necessary to compare with the Foldable versions for the other functions, but this one differs from Haskell and so there's more than one "sensible" option for how to implement it, so I think it's worth being a little bit more careful/deliberate in the tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you clarify what you mean by this? The tests I used were taken from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rereading this again, I think you mean this: assert $ A.scanl (+) 0 [1,2,3] == scanl (+) 0 [1,2,3] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, exactly. |
||
assert $ A.scanl (-) 10 [1,2,3] == [9, 7, 4] | ||
|
||
log "scanl should return the same results as its Foldable counterpart" | ||
assert $ A.scanl (+) 0 [1,2,3] == scanl (+) 0 [1,2,3] | ||
assert $ A.scanl (-) 10 [1,2,3] == scanl (-) 10 [1,2,3] | ||
|
||
log "scanr should return an array that stores the accumulated value at each step" | ||
assert $ A.scanr (+) 0 [1,2,3] == [6,5,3] | ||
assert $ A.scanr (flip (-)) 10 [1,2,3] == [4,5,7] | ||
|
||
log "scanr should return the same results as its Foldable counterpart" | ||
assert $ A.scanr (+) 0 [1,2,3] == scanr (+) 0 [1,2,3] | ||
assert $ A.scanr (flip (-)) 10 [1,2,3] == scanr (flip (-)) 10 [1,2,3] | ||
|
||
log "sort should reorder a list into ascending order based on the result of compare" | ||
assert $ A.sort [1, 3, 2, 5, 6, 4] == [1, 2, 3, 4, 5, 6] | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good to also add a test case for an array where there is more than one element which satisfies the predicate here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in latest commits
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I should have been clearer: I think we should have a test which rules out the possibility that our
findMap
actually returns the last match.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the tests to account for that.