Skip to content

Commit

Permalink
Add thaw, and functions that rely on having a mutable spreadsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaikas committed Sep 21, 2024
1 parent a41c33d commit bfa6dc7
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
4 changes: 4 additions & 0 deletions env/spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func (s *Spreadsheet) AddRow(vals SpreadsheetRow) {
s.Rows = append(s.Rows, vals)
}

func (s *Spreadsheet) RemoveRowByIndex(index int64) {
s.Rows = append(s.Rows[:index], s.Rows[index+1:]...)
}

func (s *Spreadsheet) GetRows() []SpreadsheetRow {
return s.Rows
}
Expand Down
16 changes: 16 additions & 0 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,22 @@ var builtins = map[string]*env.Builtin{
},
},

"thaw": {
Argsn: 1,
Doc: "Makes a value (currently only spreadsheets) mutable instead of immutable",
Pure: true,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch sp := arg0.(type) {
case env.Spreadsheet:
return &sp
case *env.Spreadsheet:
return sp
default:
return MakeArgError(ps, 1, []env.Type{env.SpreadsheetType}, "thaw")
}
},
},

"get_": { // *** find a name or decide on order of naming with generic words clashes with
Argsn: 1,
Doc: "Returns value of the word in context",
Expand Down
52 changes: 51 additions & 1 deletion evaldo/builtins_spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,54 @@ var Builtins_spreadsheet = map[string]*env.Builtin{
},
},

"add-rows!": {
Argsn: 2,
Doc: "Add one or more rows to a spreadsheet",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch spr := arg0.(type) {
case *env.Spreadsheet:
switch data1 := arg1.(type) {
case env.Block:
data := data1.Series
for data.Pos() < data.Len() {
rowd := make([]any, len(spr.Cols))
for ii := 0; ii < len(spr.Cols); ii++ {
k1 := data.Pop()
rowd[ii] = k1
}
spr.AddRow(*env.NewSpreadsheetRow(rowd, spr))
}
return spr
case env.Native:
spr.Rows = append(spr.Rows, data1.Value.([]env.SpreadsheetRow)...)
return spr
default:
return MakeArgError(ps, 2, []env.Type{env.BlockType, env.NativeType}, "add-rows!")
}
default:
return MakeArgError(ps, 1, []env.Type{env.SpreadsheetType}, "add-rows!")
}
},
},
"remove-row!": {
Argsn: 2,
Doc: "Remove a row from a spreadsheet by index",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch spr := arg0.(type) {
case *env.Spreadsheet:
switch data1 := arg1.(type) {
case env.Integer:
spr.RemoveRowByIndex(data1.Value)
return spr
default:
return MakeArgError(ps, 2, []env.Type{env.BlockType, env.NativeType}, "remove-row!")
}
default:
return MakeArgError(ps, 1, []env.Type{env.SpreadsheetType}, "remove-row!")
}
},
},

// TODO 2 -- this could move to a go function so it could be called by general load that uses extension to define the loader
"load\\csv": {
Argsn: 1,
Expand Down Expand Up @@ -577,10 +625,12 @@ var Builtins_spreadsheet = map[string]*env.Builtin{
Doc: "Gets the column names (header) as block.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch spr := arg0.(type) {
case *env.Spreadsheet:
return spr.GetColumns()
case env.Spreadsheet:
return spr.GetColumns()
default:
return MakeArgError(ps, 1, []env.Type{env.SpreadsheetType}, "columns?")
return MakeArgError(ps, 1, []env.Type{env.SpreadsheetType}, "headers?")
}
},
},
Expand Down
18 changes: 18 additions & 0 deletions examples/spreadsheet/in-place-mods.rye
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; Create a spreadsheet

deliveries: thaw spreadsheet { "street" "city" "zip" "contents" }
[
"Maple" "Onowa" "55555" "A bottle of Maple Syrup"
"Oak" "Springfield" "11111" "A donut-shaped stuffed animal"
"Acorn" "Springfield" "11112" "A handheld fan"
"Birch" "Springfield" "11112" "A laptop"
]

deliveries .to-json |print
print ""
deliveries |add-rows! [ "Vega" "Bayton" "12345" "A Nova" ]
deliveries .to-json |print
print ""
deliveries |remove-row! 1
deliveries .to-json |print
print ""

0 comments on commit bfa6dc7

Please sign in to comment.