Changing data
Each change you enter in the REPL is an action. An action keeps all its changes if it succeeds, and none if any part fails.
Change stored values
Section titled “Change stored values”+= adds to a number:
visits += 1visits // {1}|= adds Values to a Bag, and \= removes them:
tags |= "gift"tags \= "sale"tags // {"gift", "new"}Replace a Value
Section titled “Replace a Value”Adding a second count for "pen" fails, because stock allows one Value for each key:
stock |= "pen" -> 11 // error: the key pen occurs more than onceAn update block makes several changes at once. Remove the old Value and add the new one together:
update { stock \= "pen" -> 12 stock |= "pen" -> 11}stock // {"book" -> 3, "pen" -> 11}Add members to a role
Section titled “Add members to a role”Name a role and give its properties to add a member:
Book(title: "Dune", pages: 412)Book(title: "Emma", pages: 474)The REPL shows each new Book’s ref, its identity in the database, such as <ref:1GpWNsiDASAR18jy33CRep>.
Book.title // {"Dune", "Emma"}Change a property
Section titled “Change a property”Book::pages holds a Map from each Book to its page count. To change one Book’s count, remove its Map and add a new one:
update { Book::pages \= Book if .title == "Dune" to .pages Book::pages |= Book if .title == "Dune" to 415}Book if .title == "Dune" finds the Book, and to pairs it with a value; Transforming Bags explains both. Each Book needs a page count, so the two changes must happen in one block.
Procedures
Section titled “Procedures”A procedure names a change so that you can make it again. Its parameters work like a function’s, and : void says it returns nothing:
proc set_author(title! str, author! str): void = { update { Book::author \= Book if .title == title to .author Book::author |= Book if .title == title to author }}set_author("Dune", "Frank Herbert")The REPL answers ():{}, an empty result. Books without an author have an empty Bag there, and ?? fills it in:
Book map (title: .title, author: .author ?? "unknown")// {// (author: "Frank Herbert", title: "Dune")// (author: "unknown", title: "Emma")// }Flows follow the data
Section titled “Flows follow the data”A Flow always reads the current data:
def long_books = Book if .pages > 450long_books.title // {"Emma"}Adding a long Book changes its result:
Book(title: "Ulysses", pages: 730)long_books.title // {"Emma", "Ulysses"}Remove members
Section titled “Remove members”\= removes members from a role, and they lose their properties:
Book \= Book if .title == "Ulysses"long_books.title // {"Emma"}All or nothing
Section titled “All or nothing”This fails, because a Book needs a title, and it leaves the data unchanged:
Book(pages: 10) // error: Book::title requires exactly 1 value for each memberUndo and redo
Section titled “Undo and redo”:undo takes back the last change, which removed Ulysses:
:undolong_books.title // {"Emma", "Ulysses"}:redo makes the change again:
:redoEdit → Undo and Edit → Redo in the menu do the same. Undo goes back one change at a time and stops at a change to declarations, such as def long_books. See Undo and redo.