Data Pipeline: Filter, Transform, Aggregate

// pipeline.ls
let orders = [
  {product: "Widget",  qty: 5,  price: 9.99},
  {product: "Gadget",  qty: 2,  price: 24.99},
  {product: "Widget",  qty: 3,  price: 9.99},
  {product: "Gizmo",   qty: 1,  price: 49.99},
  {product: "Gadget",  qty: 4,  price: 24.99}
]

// Total revenue per product, sorted
for (o in orders order by o.product)
  {product: o.product, total: o.qty * o.price}

Output:

[
  {product: "Gadget", total: 49.98},
  {product: "Gadget", total: 99.96},
  {product: "Gizmo",  total: 49.99},
  {product: "Widget", total: 49.95},
  {product: "Widget", total: 29.97}
]

Vector Arithmetic

// vectors.ls
let prices  = [10, 20, 30, 40, 50]
let taxRate = 0.08

// Apply tax to all prices at once
let withTax = prices * (1 + taxRate)
withTax

Output:

[10.8, 21.6, 32.4, 43.2, 54.0]

HTML Document Generation

// generate.ls — Build a styled HTML page
let items = ["Lambda", "is", "functional"]

let page = <html
  <head
    <title "Generated Page">
    <style "
      body { font-family: sans-serif; max-width: 600px; margin: 2em auto; }
      li { padding: 0.3em 0; }
    ">
  >
  <body
    <h1 "Hello from Lambda Script">
    <ul (for (item in items) <li item>)>
  >
>

format(page, 'html')

JSON Processing with Queries

// query.ls — Process a JSON API response
let data = input("users.json", 'json')

// Find all admins
let admins = data?{role: "admin"} | ~.name

// Count by role
for (u in data, let role = u.role)
  {role, count: len(data that ~.role == role)}

Pattern Matching Dispatch

// dispatch.ls — Type-safe dispatch with pattern matching
fn process(input) => match input {
    case int:                 input * 2
    case float:               math.floor(input)
    case string:              len(input)
    case [number]:            input | ~ * ~ | sum
    case {name: string}:      "Hello, " ++ input.name
    case <img src: string>:   "Image: " ++ input.src
    default:                  "Unknown input"
}

process(42)                       // 84
process(3.7)                      // 3
process("hello")                  // 5
process([1, 2, 3])                // 14 (1+4+9)
process({name: "Alice"})          // "Hello, Alice"

Schema Validation

// config_schema.ls — Validate server configuration
type Port = int that (1024 <= ~ <= 65535)
type LogLevel = "debug" | "info" | "warn" | "error"

type ServerConfig = {
    host: string,
    port: Port,
    log_level: LogLevel,
    max_connections: int that (~ > 0),
    tls: {
        enabled: bool,
        cert_file: string?,
        key_file: string?
    }?
}
# Validate a config file
lambda validate config.yaml -s config_schema.ls

Markdown → HTML with Custom Layout

// render_doc.ls — Read Markdown and wrap in styled HTML
let doc = input("README.md", 'markdown')

// Extract title from first h1
let title = (doc?<h1>)[0][string]

<html
  <head
    <title title>
    <style "
      body { max-width: 800px; margin: 2em auto; line-height: 1.6; }
      pre { background: #f5f5f5; padding: 1em; border-radius: 4px; }
      code { font-size: 0.9em; }
    ">
  >
  <body doc>
>
# Render to PDF
lambda render_doc.ls | lambda render -o readme.pdf