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

The arguments for startsWith are back-to-front. #1

Open
robertmassaioli opened this issue Feb 8, 2017 · 7 comments
Open

The arguments for startsWith are back-to-front. #1

robertmassaioli opened this issue Feb 8, 2017 · 7 comments

Comments

@robertmassaioli
Copy link

Just try and read these two statements:

> "hello" `startsWith` "hello world"
true

And:

> "hello world" `startsWith` "hello"
false

I would expect the opposite results. It seems to break the standard pattern in Haskell and Purescript whereby the infix notation should read legibly as a sentence.

@kritzcreek
Copy link

kritzcreek commented Feb 8, 2017

Another "standard pattern" in Haskell or PureScript is that the data argument is the last to make composition and chaining with partial application nice:

all (startsWith "hello") ["hello world", "hello universe"]

So while your point is definitely valid it's not as clear-cut as you make it sound. I prefer the trade-offs the current API makes.

@robertmassaioli
Copy link
Author

The all notation could be written as:

all (`startsWith` "hello") ["hello world", "hello universe"]

Which would still be readable and looks the same in both applications. What would be your approach to removing the reading confusion in the infix invocation of this method?

I understand your position, it is valid, but I do think this style is cleaner.

@kritzcreek
Copy link

That's not valid PureScript though.

The PS equivalent:
all (_ `startsWith` "hello") ["hello world", "hello universe"]
doesn't look as nice imo.

In purescript-strings this is handled by wrapping the non-data argument in a Pattern constructor, thus removing the ambiguity between the two arguments.

stripSuffix :: Pattern -> String -> Maybe String

@hdgarrood
Copy link

Agree with @kritzcreek: in my mind, the "data argument comes last" pattern is much more useful and well-established than the "infix notation reads like a sentence" pattern.

@menelaos
Copy link
Owner

menelaos commented Feb 8, 2017

@robertmassaioli Thank you for your interest in this library!

You are right that unfortunately infix notation does not read like a sentence. However, I am inclined to leave the argument order for startsWith as it is due to the reasons mentioned by @kritzcreek and @hdgarrood. In the Haskell world I could point you to https://hackage.haskell.org/package/MissingH-1.4.0.1/docs/Data-String-Utils.html#v:startswith which makes the same trade-off.

I could offer two potential solutions:

  • Expose the same function as isPrefixOf which would make it more suitable for infix use. The function name would also be familiar to Haskell users.
    > "hello" `isPrefixOf` "hello world"
    true
    
    > "hello world" `isPrefixOf` "hello"
    false
  • Add a new module Data.String.Utils.Infix which (among others) would expose startsWith with the arguments flipped.

Please let me know what you think!

@bklaric
Copy link

bklaric commented Jan 5, 2018

I realize I'm late to the party, but I exclusively write "hello world" # startsWith "hello" for readable chaining.

@hadronized
Copy link

For what it’s worth, I had the issue with this function today as I was expecting the same order as in Haskell with isPrefixOf. Bumping a major version of the library for that seems overkill, but if you ever have to make any breaking change, I think it would make sense to swap the order.

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

No branches or pull requests

6 participants