Elixir basics · Lesson 27 (open contents)
01 · Let the code speak02 · Meet six kinds of values03 · Put values together04 · Build a new list05 · Make the shapes match06 · Choose with case07 · Parse a number safely08 · Unpack &1 and the pipe09 · Put code in a project10 · Ask precise true-or-false questions11 · Read and update nested data12 · Count visible text and bytes13 · Let function clauses choose14 · Solve a list twice15 · Choose the smallest clear control form16 · Give domain data a shape17 · Put one tested module in Mix18 · Make success and failure predictable19 · Read and write one real file20 · Take only the data you need21 · Give different data one shared action22 · Write a callback contract23 · Write down the data contract24 · Test behavior and boundaries25 · Turn a project into a command26 · Write the project promise27 · Parse one real line28 · Keep the public API small29 · Make the promise executable30 · Package the edge and leave clues31 · Prove the project is done
ELIXIR · PROJECT · LESSON 2735 minutes

Parse one real line

Trim input, split once, normalize the level, and return a tagged result.

01 · Start with the whole example

Run this first

Run it once, change one input, and compare the new result.

Elixir
# Parse without letting outside text leak inward
parse = fn line ->
  case String.split(String.trim(line), ~r/\s+/, parts: 2) do
    [""] -> :skip
    [level, message] when level in ["INFO", "WARN"] -> {:ok, {String.to_atom(String.downcase(level)), message}}
    _ -> {:error, :invalid_line}
  end
end

{parse.(" INFO ready "), parse.(""), parse.("BROKEN")}
Check the result first
  • The three results are {:ok, {:info, "ready"}}, :skip, and {:error, :invalid_line}.
02 · Take the code apart

Read from the first line down

  1. Read String.split(..., parts: 2): Split only the level from the remaining message.
  2. Read String.downcase/1: Normalize the level text.
  3. Read {:error, reason}: Return malformed input as data.
03 · Meet the new symbols

Symbols are not secret signs

String.split(..., parts: 2)

Split only the level from the remaining message.

String.downcase/1

Normalize the level text.

{:error, reason}

Return malformed input as data.

04 · Ideas inside the code

Match each name to its meaning

01

parser boundary

A parser turns outside text into a small internal data shape.

02

normalization

Normalization converts equivalent spellings to one internal value.

03

total result

Every input returns one documented result instead of falling through.

05 · Make the idea clear

Why these forms are useful

Run the example first. Predict one result, then change one input and run it again.

A parser turns outside text into a small internal data shape.

Normalization converts equivalent spellings to one internal value.

06 · Change it yourself

Close the answer and try

Accept ERROR disk as an error-level record.

Practice starting point
when level in ["INFO", "WARN", ____]

Target result: Add "ERROR".

Stuck? Read one hint

Keep external levels as strings in the guard.

After you run it, see one answer
One answer
when level in ["INFO", "WARN", "ERROR"]
Think about it: Why split with `parts: 2`?

The message may contain spaces; only the first boundary belongs to the level.

Take with you

Remember these three lines

  1. 1

    Normalize at the boundary.

  2. 2

    Return tagged failures.

  3. 3

    Do not create atoms from unlimited outside text.

Lesson completeRun the code once, then mark this lesson as done.