Repository navigation
Recipes
These examples combine features into complete tasks. Each starts independently;
copy one into a .shy file and run it with Shelly.
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
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
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
Begin here
Values and expressions
- Language basics
- Variables and scope
- Numbers and logic
- Strings and paths
- Arrays
- Hash maps
- Ranges and iteration
- Control flow
- Match and type guards
Functions and types
- Functions and calls
- Closures and function types
- Methods and properties
- Structs
- Enums
- Named types and unions
- Conversions
- Types and properties
Shell and modules
- Pipelines
- Pipeline splits
- Pipeline formats
- Commands and exit status
- Redirection
- Environment and exported variables
- Modules and visibility
- Nested modules and directory packages
- Conditional imports and declarations
- Errors and cleanup
- Supervised processes and terminals
Library guides
- Standard library
- The implicit prelude
- Bytes, files, readers, and writers
- Explicit item streams
- JSON documents and typed mapping
- Sockets, DNS, and TLS
- HTTP clients
- HTTP servers
- Framing, server-sent events, and JSON-RPC
- Terminal colors, styles, and geometry
- Build an application console
Interactive use