roc-lang / roc-lang/examples

Add example for Uppercase and Lowercase ASCII using builtins

Open
#222 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

example suggestion good first issue
Dominant language
Shell
Stars
46
Forks
19
Avg merge
13h 38m
Merged PRs (30d)
19

Description

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"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the quick-and-dirty Roc implementation included in the issue and review the linked Zulip discussion for context. The example is done when it demonstrates uppercase and lowercase ASCII conversion using builtins, handles the Result correctly, and briefly explains trade-offs and available packages.

Written by the indexing model from the issue text.

Assessment

Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
Half a day
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.