ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I don't understand what your code example was about. You created two separate functions, each under a different scope with different variables. I don't understand how that proves that "copying and pasting is the same as closures".
I created one piece of code, which happens to be a function depending on a variable in external scope, and instantiated the piece of code twice.
The point is that compiler can inline the function body (no, Perl doesn't do it), so resolution of external scope variable becomes trivial in such a case.
Click here to see the post LQ members have rated as the most helpful post in this thread.
I created one piece of code, which happens to be a function depending on a variable in external scope, and instantiated the piece of code twice.
The point is that compiler can inline the function body (no, Perl doesn't do it), so resolution of external scope variable becomes trivial in such a case.
But in this case, you created two separate closures with different contents, not one closure.
What's the difference between the "resolve" and "evaluate" functions? Why does one return a container and the other an int?
resolve is for expressions: it takes an AST, and returns the thing the AST refers to.
evaluate is for statements: it takes an AST, and executes the statement.
Quote:
Originally Posted by MTK358
Not exactly. The function's scope will just contain the parameters, but it will also have the scope the function was created in as a parent.
Okay; the function scope is then persistent, and initially taken from the parent scope when defined. No problem here.
(Well, I guess I may have to start reference-counting the containers too, to save copy costs, but it's not a problem.)
Quote:
Originally Posted by MTK358
Code:
list = List()
10.foreach(
func (x)
list.push(x)
end
)
# list now contains [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
I think the syntax above is a bit ambiguous; I'd read that as defining ten anonymous functions (doing the same thing, adding the given parameter to list), but just dropping them on the floor without doing anything with them. But otherwise I get your point about having access to the scope when defined; that is no problem.
Quote:
Originally Posted by MTK358
Maybe it's a neat idea, but it seems like bad practice to rely on variables with certain names existing when a function is called.
We're both talking about "existing" in the callers scope, not the scope when the function was defined, here. I agree. However, functions in awk are defined this way, so it is not really that strange.
Nominal Animal
Last edited by Nominal Animal; 03-21-2011 at 08:01 AM.
Does your example code only change the container's data field?
Right, only the data (storage). I was lazy.
Quote:
Originally Posted by MTK358
Because what I need is to be able to actually replace one of its children with a different container.
In my example code, this is done by cloning the source container and all its children. Storage is not cloned, only the reference counts increased by one.
If I add reference counting to the containers too, then only the source container may have to be cloned; the reference counts for all of the children is increased by one. (The reference counts for the storage would not be modified. I also must throw away the parent pointer in the container structure, since it's not reliable anymore.)
I do differentiate between assigning a value, and assigning contained objects. You can either use separate operators, or decide that any container having children cannot have a value.
As weird as it is, basically I can support this:
Code:
a.b = 5 # Value assignment
a.b.c = 3 # Value assignment
a = 4 # Value assignment
c := a # Object assignment
print c
# 4
print c.b
# 5
print c.b.c
# 3
but I guess a more normal syntax would be more useful.
Nominal Animal
Last edited by Nominal Animal; 03-21-2011 at 08:00 AM.
I think the syntax above is a bit ambiguous; I'd read that as defining ten anonymous functions (doing the same thing, adding the given parameter to list), but just dropping them on the floor without doing anything with them. But otherwise I get your point about having access to the scope when defined; that is no problem.
It's not ambiguous whatsoever. I'm creating one function, and passing it to the function "foreach" belonging to the newly created number "10", which calls it 10 times (every time with a different value for "x").
Remember, "foreach" is not a statement, but just an ordinary method of a number object.
Quote:
I do differentiate between assigning a value, and assigning contained objects. You can either use separate operators, or decide that any container having children cannot have a value.
I have no clue what you're thinking, that makes no sense at all to me.
In my language, values and objects are the same concept, the value is just a concept internal to the interpreter so it can implement primitive types. And there will be no way to edit the object's internal data from the program.
And objects can have a value and children at the same time. In fact, all objects will have an internal value, but it will be NULL for everything but primitives.
Also, I don't really have a separate concept of expressions and statements. Assignments, lists of expressions, loops, and if/else constructs are all expressions that evaluate to a value and can be part of bigger expressions. For example:
Code:
x = 7
y = 2
a = if x > 5; 6 else 8 fi + y # evaluates to 8
# OR
i = 0
a = while i != 10; if i < 5; i = i + 2; i = i = 3 else i = i + 1 fi loop + 5 # evaluates to 15
a = \
while i != 10
if i < 5
i = i + 2
i = i + 3
else
i = i + 1
fi
loop + 5 # same as above, but broken up into lines so it's easier to read
is a valid expression (yes, that entire block of code is an expression, not a statement or statement list).
In case you didn't get it, all variables in my language are references.
But in this case, you created two separate closures with different contents, not one closure.
But in both cases exactly the same code (WRT to variables names) is used. I.e. in both cases the function whose body is in 'hello.prl' file depends on external WRT it $person variable.
But in both cases exactly the same code (WRT to variables names) is used. I.e. in both cases the function whose body is in 'hello.prl' file depends on external WRT it $person variable.
It's not ambiguous whatsoever. I'm creating one function, and passing it to the function "foreach" belonging to the newly created number "10", which calls it 10 times (every time with a different value for "x").
I understand, I just couldn't interpret it that way. Now that I understand that the foreach function will execute its parameter (anonymous function) with a single index parameter, it is much clearer.
Quote:
Originally Posted by MTK358
I have no clue what you're thinking, that makes no sense at all to me.
In that snipper, = assigns only to the internal value, and := is the normal assignment operation.
Quote:
Originally Posted by MTK358
Also, I don't really have a separate concept of expressions and statements.
I find it easier to differentiate the two, but it may not be an important distinction in the end. It does explain why I expected to see explicit return statements and you do not.
Nominal Animal
Last edited by Nominal Animal; 03-21-2011 at 07:59 AM.
So how would you do assignment considering that everything is an object and that variables are references to objects contained in env's?
Quote:
Originally Posted by Nominal Animal
In that snipper, = assigns only to the internal value, and := is the normal assignment operation.
I still don't understand this idea with exposing the internal value in the program. I just don't understand how it could work. I was thinking about it since you posted it and still doesn't make any sense to me.
So how would you do assignment considering that everything is an object and that variables are references to objects contained in env's?
It depends on where you define the object membership. If you have done a.b = 1 then where is the fact that b is a member of a defined? If there exists a.c.d, then where are the facts that d is a member of c and c a member of a defined? I mean the actual C data structures.
Quote:
Originally Posted by MTK358
I still don't understand this idea with exposing the internal value in the program. I just don't understand how it could work. I was thinking about it since you posted it and still doesn't make any sense to me.
It works because of the data structures I use. If you need example code, I can write it later this week.
Nominal Animal
Last edited by Nominal Animal; 03-21-2011 at 07:58 AM.
And that was what you were against - you were against a function which depends on existence of a variable defined outside of it.
No. Please stop trolling. MTK358 did not see any value in a function which is executed in the callers scope. He specifically wrote he needs functions to be able to run in the original scope to implement closures.
I used the term "macro" for these tentative functions, because the behaviour of a function executed in the callers scope closely resembles C preprocessor macros, and my example program is in C. I thought it would be descriptive enough for nobody to mistake them for something else. I was wrong.
Nominal Animal
Last edited by Nominal Animal; 03-21-2011 at 07:57 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.