haskell / haskell/text

Capitalize first letter of a string

Open
#409 4 comments 0 reactions 0 assignees View on GitHub
feature request question
Dominant language
Haskell
Stars
421
Forks
163
PR merge metrics
No merged PRs in 30d

Description

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:

```scala
/** 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:

```kotlin
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.