Skip to content

Commit

Permalink
✨ Enable record updates on atoms with record payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
MystPi committed Apr 28, 2024
1 parent 971574c commit 7427eb2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
8 changes: 4 additions & 4 deletions templates/src/Spark/List.spark
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
def pub map\list, fn =
external "
return over.map((x) => fn(x));
return list.map((x) => fn(x));
"

def pub index_map\list, fn =
external "
return over.map((x, i) => fn(x, i));
return list.map((x, i) => fn(x, i));
"

def pub fold\over, acc, fn =
def pub fold\list, acc, fn =
external "
return over.reduce((acc, x) => fn(acc, x), acc);
return list.reduce((acc, x) => fn(acc, x), acc);
"
13 changes: 11 additions & 2 deletions templates/src/spark.prelude.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export class Atom {
this.name = name;
this.payload = payload ?? [];
}

static hasRecordPayload(atom) {
return atom instanceof Atom && atom.payload[0] instanceof Record;
}
}

export function ok(payload) {
Expand All @@ -22,14 +26,19 @@ export class Record extends Map {
if (!(record instanceof Record)) throw new Error(msg);
}

static updateRecord(record, fields) {
static update(record, fields) {
if (Atom.hasRecordPayload(record)) {
record = record.payload[0];
}

Record.assertRecord(record, 'Cannot update a non-record');

return new Record([...record, ...fields]);
}

static access(record, field) {
// An atom can be accessed if the first value in its payload is a record.
if (record instanceof Atom && record.payload[0] instanceof Record) {
if (Atom.hasRecordPayload(record)) {
record = record.payload[0];
}

Expand Down

0 comments on commit 7427eb2

Please sign in to comment.