text
text copied to clipboard
Capitalize first letter of a string
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.
I don’t have a strong opinion. How often do you need such routine in practice (excluding educational purposes)?
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?
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.
I think it would be better to extend documentation, explaining how to implement some version of capitalize
yourself.