Examples
- Hello world
- Run a shell command
- Fibonacci numbers
- Reading from standard input
- Reading from a file
- Writing to a file
- Guessing game
- FizzBuzz
- Iterating over a range
Hello world
You can echo
any value to the standard output stream:
echo "hello world";
The value doesn't have to be a string — echo
stringifies the value before printing it:
echo 123;
This is equivalent to calling $str()
on the value before printing it.
Note that echo
automatically adds a newline character to the output.
Pyro also has a family of $print()
/$println()
functions:
$println("hello world");
You can use format strings with these functions to interpolate variables:
var target = "world"; $println("hello {}", target);
The only difference between $print()
and $println()
is that $println()
automatically adds a newline character to the output.
Run a shell command
The $()
function runs a shell command and returns its output as a string, e.g.
var output = $("pwd");
The $shell()
function provides more control over input and output. It returns a three-item tuple containing the command's exit code as an integer, its stdout
output as a string, and its stderr
output as a string:
var (exit_code, output, error_output) = $shell("pwd");
You can provide an input string to $shell()
which will be written to the command's stdin
:
assert $shell("cat", "foo bar") == $tup(0, "foo bar", "");
Fibonacci numbers
Calculate the n-th Fibonacci number:
def fib(n) { if n < 2 { return n; } return fib(n - 1) + fib(n - 2); }
Reading from standard input
Read a single line from the standard input stream:
var line = $input();
Read a sequence of lines in a loop from the standard input stream:
loop { $print(">>> "); var line = $input(); if line == null || line == "exit" { break; } echo "input was: " + line; }
The global $input()
function is provided for simple use-cases. You can also access the standard input stream as a file
:
var stdin = $std::pyro::stdin(); var input = stdin:read_string();
Reading from a file
Read the content of a file into a string:
var string = $read_file("input.txt");
This is a convenience function — a file
object provides more fine-grained control:
var file = $file("input.txt", "r"); var string = file:read_string(); file:close();
Writing to a file
Write a string to a file:
$write_file("output.txt", "Content for file...");
This is a convenience function — a file
object provides more fine-grained control:
var file = $file("output.txt", "w"); file:write("Content for file..."); file:close();
Guessing game
The classic guess-a-random-number game:
var target = $std::prng::rand_int(10) + 1; loop { $print("Enter a number between 1 and 10: "); var guess = $input(); if guess == null || guess == "exit" { break; } if guess == $str(target) { echo "Correct!"; break; } else { echo "Wrong! Try again..."; } }
FizzBuzz
The classic interview question:
def fizzbuzz(n) { for i in $range(1, n + 1) { if i % 15 == 0 { echo "fizzbuzz"; } else if i % 3 == 0 { echo "fizz"; } else if i % 5 == 0 { echo "buzz"; } else { echo i; } } }
Iterating over a range
The $range()
function returns an iterator over a range of integers:
>>> for i in $range(5) { ... echo i; ... } 0 1 2 3 4
You can specify start
, stop
, and step
arguments:
>>> for i in $range(5, 15, 2) { ... echo i; ... } 5 7 9 11 13