Skip to content

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

Merged
merged 12 commits into from
Dec 17, 2020
Merged

Add short circuiting functions of elem, notElem, find, findMap, scanl, and scanr #189

merged 12 commits into from
Dec 17, 2020

Conversation

JordanMartinez
Copy link
Contributor

@JordanMartinez
Copy link
Contributor Author

Can I get a review on this?

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"
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in latest commits

Copy link
Contributor

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.

Copy link
Contributor Author

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.

@@ -237,6 +253,14 @@ 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]
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 scanl docs and scanr docs

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, exactly.

scanl :: forall a b. (b -> a -> b) -> b -> Array a -> Array b
scanl = scanlImpl

foreign import scanlImpl :: forall a b. (b -> a -> b) -> b -> Array a -> Array b
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the FFI implementation is ready to use as-is, we can just import it as scanl straight away; I don't think there's any need to alias it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in latest commits

@JordanMartinez
Copy link
Contributor Author

I believe I've addressed all your comments.

Copy link
Contributor

@hdgarrood hdgarrood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants