// The compile-time benchmark's std-heavy program (scripts/bench-compile.sh): // it uses most of the standard library, so most of std's code is // generated, compiled and linked. Its run time does not matter. import std/actor.{Continue, Stop} import std/bytes import std/dict.{type Dict} import std/html.{type Element, type Html} import std/io import std/list import std/option import std/process.{type Subject, type proc, Down} import std/ref import std/result import std/string import std/supervisor.{Child, OneForOne} pub type Item { Item(name: String, price: Int, tags: List) } type Shop { Put(Item), Total(Subject), Close(Subject) } fn shop_step :: items: Dict message: Shop -> actor.Next> / { match message { Put(item) -> Continue(dict.insert items item.name item) , Total(reply) -> { process.send reply (dict.fold items 0 fn acc _ item { acc + item.price }) Continue(items) } , Close(reply) -> { process.send reply (string.join (dict.keys items) ",") Stop } } } fn tag_view :: tag: String -> Element { {tag} } fn item_view :: item: Item -> Html {
  • {item.name}{list.map item.tags tag_view}
  • } fn page :: items: List -> Html {
      {list.map items item_view}
    } fn parse_prices :: text: String -> Result, String> { string.split text "," |> list.map fn part { string.to_int (string.trim part) |> result.replace_error ("not a price: " <> part) } |> result.all } fn checksum :: b: Bytes -> Int { list.fold (bytes.to_list b) 0 fn acc x { (acc * 31 + x) % 1000003 } } fn counted :: xs: List -> Int { region { let total = ref.new 0 list.each xs fn x { ref.set total (ref.get total + x) } ref.get total } } pub fn main :: -> () / { let items = [ Item("apple", 3, ["fruit", "red"]) , Item("bread", 5, ["bakery"]) , Item("cheese", 12, ["dairy", "aged"]) ] let shop = actor.start (dict.empty ()) fn stock message { shop_step stock message } list.each items fn item { actor.cast shop Put(item) } io.println ("total " <> show (actor.call shop Total 1000)) io.println ("closed " <> actor.call shop Close 1000) let markup = html.render (page items) io.println ("page " <> show (string.length markup)) let prices = parse_prices "3, 5, 12" io.println match prices { Ok(xs) -> "prices " <> show (counted xs), Error(e) -> e } io.println ( string.to_int "x" |> result.to_option |> option.map show |> option.unwrap_or "no int" ) let data = bytes.build (bytes.append_string (bytes.append_byte (bytes.builder ()) 65) markup) io.println ("bytes " <> show (bytes.length data) <> " " <> show (checksum data)) let sup = { use <- process.spawn supervisor.supervise OneForOne [Child("once", fn { io.println "child ran" })] 1 } let Down(_, _) = process.receive_forever (process.monitor sup) io.println "done" } //> total 20 //> closed apple,bread,cheese //> page 282 //> prices 20 //> no int //> bytes 283 494095 //> child ran //> done