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

Capitalize first letter of a string #409

Open
nartamonov opened this issue Feb 3, 2022 · 4 comments
Open

Capitalize first letter of a string #409

nartamonov opened this issue Feb 3, 2022 · 4 comments
Labels
feature request question Requires more investigation

Comments

@nartamonov
Copy link

nartamonov commented Feb 3, 2022

I am quite surprised by the absence of the common function to capitalize the first letter of a string. Yes, we have toTitle but its behaviour is different to what we have in other languages. Moreover, docs haven't any hints on that topic. Should a library user implement such method himself? If it is not implemented yet then what problems have this approach?

For example, Scala implements the method as following:

  /** Returns this string with first character converted to upper case.
    * If the first character of the string is capitalized, it is returned unchanged.
    * This method does not convert characters outside the Basic Multilingual Plane (BMP).
    */
  def capitalize: String =
    if (s == null || s.length == 0 || !s.charAt(0).isLower) s
    else updated(0, s.charAt(0).toUpper)

Kotlin developers decided that method has non-reliable behaviour in different circumstances, so they deprecated similar method and instead introduced more flexible alternative replaceFirstChar that delegates choosing the type of conversion to user:

public inline fun String.replaceFirstChar(transform: (Char) -> Char): String {
    return if (isNotEmpty()) transform(this[0]) + substring(1) else this
}

// Now instead of capitalize() they suggest to use something like:
replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }

We could have something like that in text library. It will help Haskell learners with the knowledge of other languages to adapt to Haskell more quickly.

@Bodigrim
Copy link
Contributor

Bodigrim commented Feb 4, 2022

I don’t have a strong opinion. How often do you need such routine in practice (excluding educational purposes)?

@Boarders
Copy link

Boarders commented Feb 4, 2022

Could you say more about what is bad about the docs for toTitle, I am happy to fix it if I know the problem better. I also don't have much of an opinion here on adding this function or not. What do you see as the main value?

@nartamonov
Copy link
Author

nartamonov commented Feb 5, 2022

I don’t have a strong opinion. How often do you need such routine in practice (excluding educational purposes)?

Not often at all, honestly. I needed that function when I wrote small console program that prints sentences (error messages/progress statuses) with dynamically-generated parts and that sentences should begin with the capital letter. toTitle was not appropriate in that case since it capitalizes all words in a sentence.

But I often encountered the situations when I needed the function for educational purposes (doing exercises and so on). Yes, it's niche and may be not so important, but I remember that I thought "Hm... other languages have such a simple function, sad." If we'll be able to ease learning process for newcomers it will be the main value of that contribution. Haskell ecosystem is rather foreign for newcomers, making it more similar to other ecosystems (if it doesn't compromise on basic Haskell principles) is worth it.

Could you say more about what is bad about the docs for toTitle, I am happy to fix it if I know the problem better.

Docs for toTitle is fine for me 👍 But if we add to its description something like: "If you want to capitalize only the first letter in a sencence, use capitalize." it would be even more useful and friendly for users.

@Bodigrim
Copy link
Contributor

Bodigrim commented Feb 5, 2022

I think it would be better to extend documentation, explaining how to implement some version of capitalize yourself.

@Lysxia Lysxia added feature request question Requires more investigation labels May 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request question Requires more investigation
Projects
None yet
Development

No branches or pull requests

4 participants