FAQ
Hi
I'm new to Go and have limited experience on programming. I'm a hw engineer
but Go made me more intrested in sw.

My problem is to find a reasonable way to attack a problem. I want to
define calculation in runtime and use inside my code. Naturally will it be
slower than compiled code but thats ok for me.

I found a simple example in the funcmap from mikespook:
https://bitbucket.org/mikespook/golib/src/83102c62c958500fe4120ed79470a2ad91cf0143/funcmap?at=default

It maps a string to a func and make a call.

A bit more generic would be to allow a text input to be converted to some
data structure in a more complex scenario.
Example of a stupid input:
min(max(a,5),b)

In general a function that can take functions,variables and constants as
inputs like a expression in a assignment in code. The set of available
functions and variables will be predefined.

My current thinking is to map input to tree datastructure representing the
hierarchy pointing out the function and its input in form of pointing to
next level of function below, variables and constants in any mix.At input
of textline should it be chect and a datastructure populated and then will
input be evaluated as call to function to evaluate according to
datastructure. Evaluation could maybe be based on funcmap above and when
input parameter is a function call for evaluation of that function in order
to evaluate the tree.
But i'm not sure if this approach is possible or if there are other better
ways to do it.

/Anders Edman




--

Search Discussions

  • Rémy Oudompheng at Nov 25, 2012 at 5:09 pm

    On 2012/11/25 Anders Edman wrote:
    My problem is to find a reasonable way to attack a problem. I want to
    define calculation in runtime and use inside my code. Naturally will it be
    slower than compiled code but thats ok for me.
    [...]
    My current thinking is to map input to tree datastructure representing the
    hierarchy pointing out the function and its input in form of pointing to
    next level of function below, variables and constants in any mix.At input of
    textline should it be chect and a datastructure populated and then will
    input be evaluated as call to function to evaluate according to
    datastructure. Evaluation could maybe be based on funcmap above and when
    input parameter is a function call for evaluation of that function in order
    to evaluate the tree.
    But i'm not sure if this approach is possible or if there are other better
    ways to do it.
    I think it is a good approach. Here is an example that uses the Go
    language parser to parse arithmetic expressions that look like Go
    syntax and use a function map and a map of variable values to evaluate
    it.

    http://play.golang.org/p/HA8-8lxdtV

    Rémy.

    --
  • Egon at Nov 25, 2012 at 5:20 pm
    You may also look at https://github.com/sbinet/go-eval that provides some
    runtime compilation capabilities. Although Rémys solution is probably
    simpler.

    Egon
    On Sunday, November 25, 2012 5:21:18 PM UTC+2, Anders Edman wrote:

    Hi
    I'm new to Go and have limited experience on programming. I'm a hw
    engineer but Go made me more intrested in sw.

    My problem is to find a reasonable way to attack a problem. I want to
    define calculation in runtime and use inside my code. Naturally will it be
    slower than compiled code but thats ok for me.

    I found a simple example in the funcmap from mikespook:

    https://bitbucket.org/mikespook/golib/src/83102c62c958500fe4120ed79470a2ad91cf0143/funcmap?at=default

    It maps a string to a func and make a call.

    A bit more generic would be to allow a text input to be converted to some
    data structure in a more complex scenario.
    Example of a stupid input:
    min(max(a,5),b)

    In general a function that can take functions,variables and constants as
    inputs like a expression in a assignment in code. The set of available
    functions and variables will be predefined.

    My current thinking is to map input to tree datastructure representing the
    hierarchy pointing out the function and its input in form of pointing to
    next level of function below, variables and constants in any mix.At input
    of textline should it be chect and a datastructure populated and then will
    input be evaluated as call to function to evaluate according to
    datastructure. Evaluation could maybe be based on funcmap above and when
    input parameter is a function call for evaluation of that function in order
    to evaluate the tree.
    But i'm not sure if this approach is possible or if there are other better
    ways to do it.

    /Anders Edman



    --
  • Anders Edman at Nov 25, 2012 at 7:23 pm
    Thanks Rémy and Egon


    Now i have a lot to study. The go-eval looks a bit complex for me but i
    will check it.

    Regarding the proposal from Rémy it suprise me how simple it could be, but
    that's typical in Go.

    Thanks again
    /Anders


    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 25, '12 at 4:53p
activeNov 25, '12 at 7:23p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2017 Grokbase