Skip to content

Conversation

@ddseapy
Copy link
Contributor

@ddseapy ddseapy commented Feb 25, 2016

Signed-off-by: David Seapy ddseapy@ccri.com

Signed-off-by: David Seapy <ddseapy@ccri.com>
idx = ctx.indexOf(n)
}
ctx.get(idx)
if (idx < 0) null else ctx.get(idx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if we could code this a little smarter - we're going to be calling ctx.indexOf for every eval if the idx is -1. Nothing is springing to mind though...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well actually we could just make it default to -2 - but we're still doing 2 checks every time. probably doesn't really matter...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could wrap the get in a try/catch, which would be basically free for the normal case, but make the null case way more expensive...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this?

     var doEval: EvaluationContext => Any = null
     override def eval(args: Array[Any])(implicit ctx: EvaluationContext): Any = {
       if (idx == -1) {
         idx = ctx.indexOf(n)
         doEval = 
           if(idx < 0)  _  => null
           else         ec => ec.get(idx)
       }
       doEval(ctx)
     }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see how this would work if default and the "idx ==" check where both -2 instead of -1. Then it would only do ctx.indexOf on the first eval() execution.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or if "idx == -1" was "doEval == null", which is a bit clearer, basically saying "if we don't have a doEval defined then define it")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this work to avoid the check altogether?:

var doEval: EvaluationContext => Any = { ctx =>
      val idx = ctx.indexOf(n)
      doEval =
        if(idx < 0)  _  => null
        else         ec => ec.get(idx)
      doEval(ctx)
    }
    override def eval(args: Array[Any])(implicit ctx: EvaluationContext): Any = doEval(ctx)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks reasonable

Signed-off-by: David Seapy <ddseapy@ccri.com>
@elahrvivaz
Copy link
Contributor

hehe - @ddseapy you used master for your working branch?
merged as e5ecf99

@elahrvivaz elahrvivaz closed this Mar 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants