Skip to content

Recipes

Chloë Strainge edited this page Oct 14, 2026 · 1 revision

Small complete programs

These examples combine features into complete tasks. Each starts independently; copy one into a .shy file and run it with Shelly.

Summarize named measurements

Use a distinct type to avoid accidentally mixing a distance with an unrelated Float. The conversion at the end makes the presentation boundary explicit.

type Kilometers = Float
let $legs: [Kilometers] = [1.5, 2.0, 0.5]
let $distance: Kilometers = 0.0
for $leg in $legs { $distance = $distance + $leg }
echo "Distance:" Float($distance) "km"
Distance: 4.0 km

Turn CSV into application text

The decoder yields records incrementally. The final array is an intentional collection point, rather than a batch hidden between stages.

fn manifest_line($row: [String]): String
{
    let ($label, $count) = $row
    let $bottles = Integer($count) * 6
    "${label}: ${bottles} bottles"
}
let $manifest: [String]
printf 'amber,3\ngreen,2\n' csv:| manifest_line | $manifest
for $line in $manifest { echo $line }
amber: 18 bottles
green: 12 bottles

Validate and transform a JSON configuration

import std::json
struct Reminder { pub title: String, pub minutes: Integer }
let $reminder: Reminder = std::json::decode_as '{"title":"Stretch","minutes":15}' Reminder
let $seconds = $reminder.minutes * 60
echo "${reminder.title} in ${seconds} seconds"
Stretch in 900 seconds

For longer-running programs, add typed errors and cleanup around resources. For reusable code, split the policy into modules, keeping private implementation declarations out of the interface.


Related: Quick start · Pipelines · Standard library

Clone this wiki locally