question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

return 'sum' from a function like '(x) => sum=0; for(i=1;i<=x;i++) sum+=i; sum;'

See original GitHub issue

Hi!

Is it siomehow possible to return a specific value from a function defined in Mages, i.e. if i define a function

   var myfunc = engine.Interpret("(x) => x*2");
   var result = myfunc.Invoke(new Object[] { 7.0 });

then i get 14.0. fine!

but if i’ve got a ‘method’-like function(*1) like:

   var myfunc = engine.Interpret("(x) => sum=0; for(i=1;i<=x;i++) sum+=i; sum;");
   var result = myfunc.Invoke(new Object[] { 100 });

I’d expect that the call returns 5050. but it’s returning 100.

interpreting (i.e. engine.Interpret(…))

    var result = engine.Interpret("sum=0; for(i=1;i<=100;i+=1) {sum+=i;} sum;");

returns 5050.

is it somehow possible to return ‘sum’ from the above defined function *1)?

more info: it returns 100 if there is no variable “sum” defined in Engine.Scope otherwise it returns the value of “sum” (if it is defined in Engine.Scope)

thanks!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
hanlectincommented, Jul 1, 2022

Thanks Florain, using “var” does the trick 😃 and BTW: Kudos to MAGES! It’s awesome!!!

0reactions
FlorianRapplcommented, Jul 1, 2022

Cool! Glad you like it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python function "sum" with syntax like: sum() -> 0 sum(1)(2) ...
The built-in sum just takes an iterable and returns a number, which feels much less surprising. However, assuming that you are certain you ......
Read more >
Python's sum(): The Pythonic Way to Sum Values
In this step-by-step tutorial, you'll learn how to use Python's sum() function to add numeric values together. You also learn how to concatenate...
Read more >
sum() function in Python
Sum () Function in Python Syntax​​ sum(a) : a is the list , it adds up all the numbers in the list a...
Read more >
Various ways to sum numbers in Python
The sum() function returns the sum of all items in an iterable. It is a Python built-in function. There are various methods to...
Read more >
SUMIF function
To sum cells based on multiple criteria, see SUMIFS function. Important: The SUMIF function returns incorrect results when you use it to match...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found