Erlang basics · Lesson 27 (open contents)
01 · Open erl02 · Meet common terms03 · Tell two kinds of text apart04 · Bind a variable once05 · Split and filter a list06 · Parse a number safely07 · Choose one path with case08 · Let a function keep going09 · Put code in a module10 · Compare terms and Boolean results11 · Read and update nested maps12 · Build and inspect UTF-8 binaries13 · Let clauses choose the function path14 · Solve a list twice15 · Choose with patterns, guards, and errors16 · Describe a module and one record17 · Put one tested module in Rebar318 · Return success or failure as data19 · Read and write one file safely20 · Build a data pipeline with functions21 · Keep records behind a module boundary22 · Define a module contract23 · Describe the data contract24 · Test small rules and whole flows25 · Shape builds and make 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
ERLANG · PROJECT · LESSON 2735 minutes

Parse one real line

Trim a binary, split once, normalize its 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.

Erlang
%% Keep outside text at one boundary
Parse = fun(Line) ->
  case binary:split(string:trim(Line), <<" ">>, [trim]) of
    [] -> skip;
    [<<"INFO">>, Message] -> {ok, {info, Message}};
    [<<"WARN">>, Message] -> {ok, {warn, Message}};
    _ -> {error, invalid_line}
  end
end,
{Parse(<<"INFO ready">>), Parse(<<>>), Parse(<<"BROKEN">>)}.
Check the result first
  • The results show one accepted record, skip, and {error,invalid_line}.
02 · Take the code apart

Read from the first line down

  1. Read binary:split/3: Split the level from the remaining message.
  2. Read string:trim/1: Remove surrounding whitespace.
  3. Read {error, Reason}: Return malformed input as data.
03 · Meet the new symbols

Symbols are not secret signs

binary:split/3

Split the level from the remaining message.

string:trim/1

Remove surrounding whitespace.

{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 accepted spellings to one internal value.

03

complete 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 accepted spellings to one internal value.

06 · Change it yourself

Close the answer and try

Accept an ERROR line.

Practice starting point
[<<"ERROR">>, Message] -> {ok, {____, Message}};

Target result: Use the atom error.

Stuck? Read one hint

The internal level is a known atom.

After you run it, see one answer
One answer
[<<"ERROR">>, Message] -> {ok, {error, Message}};
Think about it: Why avoid creating atoms from arbitrary input?

Atoms are not garbage-collected in the usual way; unlimited outside values can exhaust the atom table.

Take with you

Remember these three lines

  1. 1

    Normalize at the boundary.

  2. 2

    Return tagged failures.

  3. 3

    Convert only known text to atoms.

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