New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

best way to execute specific operations within Go? #2

Closed
dadatawajue opened this Issue Oct 7, 2017 · 6 comments

Comments

Projects
None yet
3 participants
@dadatawajue

dadatawajue commented Oct 7, 2017

Hello, I think this is extremely cool. I did however notice that both you, the Android/iOS ports, as well as Rob Pike's command line program all worked the same way (basically giving user's free reign over which commands that they'd like to execute).

My question is, what if you provided two html input fields with the purpose of (e.g.) subtracting X% from Y value. How would you go about executing this command with Rob Pike's code?

To make myself more clear, let's say the user submitted the form values, on the server you get them by:

percentageVal, err := strconv.ParseFloat(r.FormValue("percentage"), 64)
numVal, err := strconv.ParseFloat(r.FormValue("number"), 64)
// how would you proceed?

Hope you don't mind me 'abusing' your repo with this question, but I doubt Rob Pike would reply, and besides him then I feel you're the best person to ask (:

@z505

This comment has been minimized.

Show comment
Hide comment
@z505

z505 Oct 7, 2017

How about writing an equation out, then sending the final equation to the function? i.e. "1" is in one edit field, and "2" is in another field... so create a final string "1+2" and send that equation string to the necessary function. This would simply be an equation an writer that takes multiple inputs, then puts it into a single final equation to send to an ivy function. Sorry, not sure if this helps you or if it is what you are after.

z505 commented Oct 7, 2017

How about writing an equation out, then sending the final equation to the function? i.e. "1" is in one edit field, and "2" is in another field... so create a final string "1+2" and send that equation string to the necessary function. This would simply be an equation an writer that takes multiple inputs, then puts it into a single final equation to send to an ivy function. Sorry, not sure if this helps you or if it is what you are after.

@dmitshur dmitshur added the question label Oct 8, 2017

@dmitshur

This comment has been minimized.

Show comment
Hide comment
@dmitshur

dmitshur Oct 8, 2017

Member

ivy is described as:

Ivy is an interpreter for an APL-like language.

All the current versions of ivy offer a terminal-like UI for interacting with it.

As @z505 said, I think your best bet is to merge the values into an entire command, and send that to the ivy interpreter for processing.

Keep in mind it has state, e.g., you can declare variables, then use them in later invocations. You may or may not want that.

I'm not sure how else to answer this.

How would you go about executing this command with Rob Pike's code?

There's an "execute" flag in ivy that makes it execute a single expression. Look at how that works. See here.

Member

dmitshur commented Oct 8, 2017

ivy is described as:

Ivy is an interpreter for an APL-like language.

All the current versions of ivy offer a terminal-like UI for interacting with it.

As @z505 said, I think your best bet is to merge the values into an entire command, and send that to the ivy interpreter for processing.

Keep in mind it has state, e.g., you can declare variables, then use them in later invocations. You may or may not want that.

I'm not sure how else to answer this.

How would you go about executing this command with Rob Pike's code?

There's an "execute" flag in ivy that makes it execute a single expression. Look at how that works. See here.

@z505

This comment has been minimized.

Show comment
Hide comment
@z505

z505 Oct 8, 2017

And remember if you build an equation string, you may need to use ( ) brackets to enforce the order you desire the equation to be properly ordered the way you want... (1+2) * (5) versus (1)+(2 * 5)

z505 commented Oct 8, 2017

And remember if you build an equation string, you may need to use ( ) brackets to enforce the order you desire the equation to be properly ordered the way you want... (1+2) * (5) versus (1)+(2 * 5)

@dadatawajue

This comment has been minimized.

Show comment
Hide comment
@dadatawajue

dadatawajue Oct 8, 2017

Thanks a lot :)
I tried to put it together as simple as possible, and it "kinda works", but not sure how to use the execute flag, and I guess there probably is a much better and cleaner way to achieve this?

package main

import (
	"robpike.io/ivy/run"
	"flag"
	"robpike.io/ivy/parse"
	"robpike.io/ivy/scan"
	"robpike.io/ivy/config"
	"robpike.io/ivy/exec"
	"strings"
	"fmt"
	"robpike.io/ivy/value"
)

var (
	format    = flag.String("format", "", "use `fmt` as format for printing numbers; empty sets default format")
	conf    config.Config
	context value.Context
)

func main() {
	val1 := "4"
	val2 := "5"

	conf.SetFormat(*format)
	context = exec.NewContext(&conf)
	scanner := scan.New(context, "<args>", strings.NewReader(fmt.Sprintf("%v + %v", val1, val2)))
	parser := parse.NewParser("<args>", scanner,context)
	ok := run.Run(parser, context, false)
	if (ok) {
		// it prints 9 in console but not sure where to get this value
	}
}

dadatawajue commented Oct 8, 2017

Thanks a lot :)
I tried to put it together as simple as possible, and it "kinda works", but not sure how to use the execute flag, and I guess there probably is a much better and cleaner way to achieve this?

package main

import (
	"robpike.io/ivy/run"
	"flag"
	"robpike.io/ivy/parse"
	"robpike.io/ivy/scan"
	"robpike.io/ivy/config"
	"robpike.io/ivy/exec"
	"strings"
	"fmt"
	"robpike.io/ivy/value"
)

var (
	format    = flag.String("format", "", "use `fmt` as format for printing numbers; empty sets default format")
	conf    config.Config
	context value.Context
)

func main() {
	val1 := "4"
	val2 := "5"

	conf.SetFormat(*format)
	context = exec.NewContext(&conf)
	scanner := scan.New(context, "<args>", strings.NewReader(fmt.Sprintf("%v + %v", val1, val2)))
	parser := parse.NewParser("<args>", scanner,context)
	ok := run.Run(parser, context, false)
	if (ok) {
		// it prints 9 in console but not sure where to get this value
	}
}
@dmitshur

This comment has been minimized.

Show comment
Hide comment
@dmitshur

dmitshur Oct 9, 2017

Member

it prints 9 in console but not sure where to get this value

You can use Config.SetOutput to change where the output goes.

Member

dmitshur commented Oct 9, 2017

it prints 9 in console but not sure where to get this value

You can use Config.SetOutput to change where the output goes.

@dadatawajue

This comment has been minimized.

Show comment
Hide comment
@dadatawajue

dadatawajue Oct 10, 2017

Great, thanks a lot both of you <3

dadatawajue commented Oct 10, 2017

Great, thanks a lot both of you <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment