Skip to content

Commit

Permalink
ivy: add print unary (#170)
Browse files Browse the repository at this point in the history
I held off sending this for a while, but it really is invaluable when
debugging custom operators.
  • Loading branch information
rsc authored Dec 11, 2024
1 parent 44f09ae commit 45141f8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 57 deletions.
1 change: 1 addition & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Unary operators
Phase phase Phase of the value in the complex plane (-π to π)
Conjugate +B conj Complex conjugate of the value
System functions ⎕ sys Argument is a string; run "sys 'help'" for details
Print print Print and evaluate to argument; useful for debugging
Binary operators
Expand Down
1 change: 1 addition & 0 deletions mobile/help.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 59 additions & 57 deletions parse/help.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions value/unary.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package value

import (
"fmt"
"math/big"
"unicode/utf8"
)
Expand Down Expand Up @@ -121,6 +122,11 @@ var IvyEval func(context Context, s string) Value

var UnaryOps = make(map[string]UnaryOp)

func printValue(c Context, v Value) Value {
fmt.Printf("%s\n\n", v.Sprint(c.Config()))
return v
}

func init() {
ops := []*unaryOp{
{
Expand Down Expand Up @@ -698,6 +704,19 @@ func init() {
},
},

{
name: "print",
fn: [numType]unaryFn{
intType: printValue,
charType: printValue,
bigIntType: printValue,
bigRatType: printValue,
bigFloatType: printValue,
vectorType: printValue,
matrixType: printValue,
},
},

{
name: ",",
fn: [numType]unaryFn{
Expand Down

0 comments on commit 45141f8

Please sign in to comment.