haskell-hvr / missingh

Utility library [Haskell]
https://hackage.haskell.org/package/MissingH
Other
87 stars 40 forks source link

startswith should have its arguments flipped #43

Closed reuleaux closed 5 years ago

reuleaux commented 6 years ago

I like MissingH's Data.String.Utils module, but never use startswith, even though I would like to, and I think it's a good function name.

That's because I like my functions to make sense when read out loud:

>>> "ba" `isPrefixOf` "banana"
True

makes sense to me, because "ba" is a prefix of "banana". However

>>> "ba" `startswith` "banana"
True

I find odd, because "ba" does not start with "banana", it's the other way araound: "banana" starts with "ba". thus what I`m doing is this:

>>> mystartswith = flip startswith
>>> "banana" `mystartswith` "ba"
True

I think this should be fixed in MissingH in the long run (and I m sure this would break existing code).

hvr commented 5 years ago

This comes down to whether startswith and endswith are intended to be used in prefix or infix position. And I can see arguments for both; but then again, we already have the infix versions in Data.List; so I think it provides more value to keep startswith or endswith with the current argument order, as it allows you to write something like

filter (startswith "ba") ["apple","banana"]

So I don't see a compelling enough argument to justify changing the status-quo as well as incur code-breakages which would be hard to detect as the code would still compile without any warning if we swapped the argument order but would behave incorrectly.