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

Add example for Uppercase and Lowercase ASCII using builtins #222

Open
lukewilliamboswell opened this issue Dec 20, 2024 · 0 comments
Open

Comments

@lukewilliamboswell
Copy link
Collaborator

See zulip discussion for context

We should add a new example which shows people how to make conversions to/from Upper and Lower case using only the builtins.

We should briefly explain the trade-offs, and how there will be packages available to provide additional functionality. Could even point people towards https://github.com/Hasnep/roc-ascii

Below is a quick and dirty implementation... we should modify this to handle the Result correctly, and polish it a little with some explanation and examples.

module [to_upper, to_lower]

to_upper : Str -> Str
to_upper = \str ->
    str
    |> Str.toUtf8
    |> List.map to_upper_help
    |> Str.fromUtf8
    |> unwrap

to_lower : Str -> Str
to_lower = \str ->
    str
    |> Str.toUtf8
    |> List.map to_lower_help
    |> Str.fromUtf8
    |> unwrap

expect to_upper "hello" == "HELLO"
expect to_upper "Hello" == "HELLO"

expect to_lower "HELLO" == "hello"
expect to_lower "Hello" == "hello"

to_upper_help = \c -> if c >= 'a' && c <= 'z' then c - delta else c
to_lower_help = \c -> if c >= 'A' && c <= 'Z' then c + delta else c
delta = 'a' - 'A'

unwrap : Result a _ -> a
unwrap = \result ->
    when result is
        Ok a -> a
        Err _ -> crash "quick and dirty error handling"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant