Skip to content
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

Use current environment for user-defined functions #125

Closed
sharkdp opened this issue Aug 26, 2017 · 5 comments · Fixed by #371
Closed

Use current environment for user-defined functions #125

sharkdp opened this issue Aug 26, 2017 · 5 comments · Fixed by #371

Comments

@sharkdp
Copy link
Owner

sharkdp commented Aug 26, 2017

This is the current behavior:

>>> a=2
>>> f(x)=a*x  # global environment is closed over here
>>> f(1)
   = 2
>>> a=3  # has no effect, environment was closed over at function definition
>>> f(1)
   = 2

However, it would probably be more useful if the current environment would be used at call-time:

>>> a=3
>>> f(1)
   = 3
@fattredd
Copy link

fattredd commented Sep 1, 2017

I disagree. If you'd like a function to be able to change in the future, use two parameters like this:

>>> f(x, y) = x * y
>>> f(1, 2)
   = 2
>>> f(1, 3)
   = 3

Though it can be tedious to type two parameters every time. Perhaps a better solution would be the ability to set function defaults like this:

>>> f(x, y=2) = x * y
>>> f(1)
   = 2  
>>> f(1, 3)  
   = 3

@sharkdp
Copy link
Owner Author

sharkdp commented Sep 2, 2017

I initially implemented it like this, because I thought that it might be a good idea (and "mutability is bad"). However, most scripting languages (and I tend to think of Insect as a simple scripting language) would imitate the mutable behavior:

Python:

>>> a = 2
>>> def f(x): return a * x
... 
>>> a = 3
>>> f(1)
3

JavaScript:

> var a = 2;
> function f(x) { return a*x; }
> a = 3;
> f(1)
3

It's even possible to define the function f before a is defined. That's currently not possible in Insect:

>>> f(x) = a * x
>>> a = 2
>>> f(1)

  Unknown identifier: a

Things get even more weird (in my opinion) if two functions are involved. At function definition, Insect captures the entire environment - including all function definitions:

>>> f1(x) = x²
>>> f2(x) = 2 f1(3 x)    # environment (including f1) is stored here
>>> f2(4)
   = 288
>>> f1(x) = x³    # this change does not have any effect
>>> f2(4)
   = 288

Coming back to your point: A way to secure a function definition from being changed in the future would be to only use constants, not global variables.

(JavaScript):

> const a = 2;
> function f(x) { return a*x; }
> a = 3;
TypeError: Assignment to constant variable.

Currently, Insect only supports built-in constants (like pi, speedOfLight, etc.). But user-defined constants could be a useful addition.

@sharkdp
Copy link
Owner Author

sharkdp commented Nov 10, 2017

@fattredd Any thoughts on this?

@sharkdp sharkdp added this to the v5.1 milestone Dec 19, 2017
@sharkdp sharkdp removed this from the v5.1 milestone Jul 10, 2019
@triallax
Copy link
Contributor

This change makes sense to me. @sharkdp has your opinion on this issue changed? If not, let's add it in v5.9.0.

@sharkdp
Copy link
Owner Author

sharkdp commented Dec 28, 2022

Sounds good. My opinion has not changed.

@triallax triallax added this to the v5.9.0 milestone Dec 28, 2022
triallax added a commit to triallax/insect that referenced this issue Mar 25, 2023
triallax added a commit that referenced this issue Apr 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants