{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "collections-intro",
      "metadata": {},
      "source": "# Aner: core collections\n\nUse a rebuilt Aner executable and compatible VS Code extension. Choose **Run All**. This notebook uses shared native state; collections require no imports. Read `docs/COLLECTIONS.md` for complete methods, time/space Big O, and memory limits.\n"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "collections-create",
      "metadata": {},
      "outputs": [],
      "source": [
        "let values = List(10, \"hello\", 1.5, true, null)\n",
        "let record = Dictionary()\n",
        "record.set(\"values\", values)\n",
        "record.set(\"optional\", null)\n",
        "let unique = Set(1, \"1\", 1.0, true, null, 1)\n",
        "print(values)\n",
        "print(unique.len())\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "collections-mutation",
      "metadata": {},
      "source": "## Values and aliases persist\n\nRetrieving a nested collection requires a checked cast because Dictionary values have static type `Any`. Its reference is shared, so appending through the Dictionary changes `values`. Expected outputs: **6**, **true**, **null**, **missing**.\n"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "collections-shared",
      "metadata": {},
      "outputs": [],
      "source": [
        "cast<List>(record.get(\"values\")).append(\"next cell\")\n",
        "print(values.len())\n",
        "print(record.contains(\"optional\"))\n",
        "print(record.get(\"optional\"))\n",
        "print(record.get_or(\"missing\", \"missing\"))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "collections-copy",
      "metadata": {},
      "source": "## Shallow copies and order\n\n`copy()` creates a separate outer container and shares nested collection/object references. Set traversal uses insertion order; integer `1`, float `1.0`, string `\"1\"`, and Boolean `true` are distinct. Expected lengths are **6**, **7**, and **5**.\n"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "collections-copy-run",
      "metadata": {},
      "outputs": [],
      "source": [
        "let copied = values.copy()\n",
        "copied.append(\"copy only\")\n",
        "print(values.len())\n",
        "print(copied.len())\n",
        "print(unique.len())\n",
        "print(record.keys())\n",
        "print(unique.to_list())\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "collections-sort",
      "metadata": {},
      "source": "## Stable ascending sorting\n\nInt64 and Float64 values can be sorted together without changing their stored types. Strings sort lexicographically by encoded bytes; Bools sort false before true. Incompatible mixtures such as integer and String, null, NaN, or composite values report R2901 before mutation, then reset the Session like other runtime errors. Sorting needs O(n log n) comparisons and O(n) auxiliary space; String comparisons add byte cost. Expected output: `List(-2.0, 1, 1.5, 3)` and `List(\"apple\", \"banana\", \"pear\")`.\n"
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "collections-sort-run",
      "metadata": {},
      "outputs": [],
      "source": [
        "let sortable_numbers = List(3, 1.5, 1, -2.0)\n",
        "sortable_numbers.sort()\n",
        "print(sortable_numbers)\n",
        "let sortable_words = List(\"pear\", \"apple\", \"banana\")\n",
        "sortable_words.sort()\n",
        "print(sortable_words)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "collections-cost",
      "metadata": {},
      "source": "## Choose operations by cost\n\nList indexed lookup is O(1); append is amortized O(1), with O(n) growth steps. Dictionary and Set lookup is expected O(1), worst O(n) under hash collisions. String hashing/copying adds work proportional to the string length. Container storage is linear in entries/capacity; returned snapshots allocate their own outer storage. `docs/COLLECTIONS.md` gives per operation time and auxiliary/result space, error rules, reference ownership, and the shared object/slot/string ceilings. Saved output does not restore live values after a session restart.\n"
    }
  ],
  "metadata": {
    "kernelspec": {
      "name": "aner",
      "display_name": "Aner",
      "language": "aner"
    },
    "language_info": {
      "name": "aner",
      "file_extension": ".aner",
      "mimetype": "text/x-aner"
    },
    "aner": {
      "status": "example",
      "execution_mode": "persistent-session"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
