Tracks
/
Factor
Factor
/
Exercises
/
Log Levels
Log Levels

Log Levels

Learning Exercise

Introduction

A string in Factor is a sequence of characters. Most words from the sequences vocabulary work on strings, in addition to the dedicated string-handling words in splitting, ascii, and friends.

String literals

Strings are written in double quotes. The usual escapes are supported — \n, \t, \r, \\, \":

"hello, world" .                 ! => "hello, world"
"first\nsecond" print            ! prints two lines

Length and indexing

Because a string is a sequence, the sequences words length and nth work on it directly.

length returns the number of characters:

"hello" length .    ! => 5

nth takes a 0-based index and the sequence, and returns the element at that position. For a string the element is a character — a code point (an integer), not a one-character substring:

0 "hello" nth .     ! => 104   (CHAR: h)
4 "hello" nth .     ! => 111   (CHAR: o)

From a character back to a string

To turn a single code point into a one-character string, use 1string ( char -- str ) from the strings vocabulary. CHAR: is the parser-time form of a literal code point:

CHAR: A 1string .                ! => "A"
0 "hello" nth 1string .          ! => "h"

Splitting

The splitting vocabulary cuts a string into pieces.

split1 cuts on the first occurrence of a separator and returns both halves:

split1 ( seq subseq -- before after )
"foo: bar" ": " split1 .s
! => "foo"
! => "bar"

split cuts on any of the characters in a separator set, possibly producing empty pieces:

"[ERROR]: Stack overflow" "[]" split .
! => { "" "ERROR" ": Stack overflow" }

harvest (in sequences) drops the empty pieces:

"[ERROR]: Stack overflow" "[]" split harvest .
! => { "ERROR" ": Stack overflow" }

Picking out a piece

Once a string is split into an array of pieces, first, second, third, and fourth (in sequences) return the piece at that position:

"apple,bee,carrot,duck" "," split first .    ! => "apple"
"apple,bee,carrot,duck" "," split second .   ! => "bee"
"apple,bee,carrot,duck" "," split third .    ! => "carrot"
"apple,bee,carrot,duck" "," split fourth .   ! => "duck"

When you want all the leading pieces on the stack at once, first2 and first3 unpack two or three of them in one step:

"apple,bee,carrot,duck" "," split first2 .s
! => "apple"
! => "bee"

"apple,bee,carrot,duck" "," split first3 .s
! => "apple"
! => "bee"
! => "carrot"

On a string itself, first returns the first character — a code point — rather than a substring:

"hello" first .                         ! => 104   (CHAR: h)

Trimming

[ blank? ] trim (from sequences, with blank? from the ascii vocabulary) removes leading and trailing whitespace:

"  Disk full \r\n" [ blank? ] trim .
! => "Disk full"

Case conversion

>lower and >upper (from ascii) return a new string with the case changed:

"WARNING" >lower .    ! => "warning"
"hello" >upper .      ! => "HELLO"

Joining

Two sequences words assemble strings:

surround wraps a string with a prefix and a suffix:

surround ( seq pre post -- new-seq )
"warning" "(" ")" surround .    ! => "(warning)"

glue joins two strings with a separator between them:

glue ( s1 s2 sep -- s )
"Disk full" "(error)" " " glue .    ! => "Disk full (error)"

Instructions

In this exercise you'll be processing log lines.

Each log line is a string formatted as "[<LEVEL>]: <MESSAGE>".

There are three log levels: INFO, WARNING, and ERROR.

You have three tasks. Each takes a single log line off the stack.

1. Get message from a log line

Define message to return the log line's message. Any leading or trailing whitespace should be removed.

"[ERROR]: Invalid operation" message .
! => "Invalid operation"

"[WARNING]:  Disk almost full\r\n" message .
! => "Disk almost full"

2. Get log level from a log line

Define log-level to return the log line's level, lowercased.

"[ERROR]: Invalid operation" log-level .
! => "error"

3. Reformat a log line

Define reformat to put the message first and the lowercase log level in parentheses after it.

"[INFO]: Operation completed" reformat .
! => "Operation completed (info)"

Note: All strings in this exercise are ASCII. Later exercises will tackle Unicode-aware string handling.

Edit via GitHub The link opens in a new window or tab
Factor Exercism

Ready to start Log Levels?

Sign up to Exercism to learn and master Factor with 47 concepts163 exercises, and real human mentoring, all for free.