LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2011, 12:46 PM   #31
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454

Quote:
Originally Posted by MTK358 View Post
Exactly what I was saying.

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.
Old 02-13-2011, 01:12 PM   #32
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
Old 02-13-2011, 01:30 PM   #33
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
@Nominal Animal

Does your example code only change the container's data field?

Because what I need is to be able to actually replace one of its children with a different container.
 
Old 02-13-2011, 02:53 PM   #34
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by MTK358 View Post
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 View Post
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 View Post
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 View Post
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.
 
Old 02-13-2011, 03:13 PM   #35
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by MTK358 View Post
Does your example code only change the container's data field?
Right, only the data (storage). I was lazy.

Quote:
Originally Posted by MTK358 View Post
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.
 
Old 02-13-2011, 03:42 PM   #36
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Nominal Animal View Post
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.

Last edited by MTK358; 02-13-2011 at 04:15 PM.
 
Old 02-13-2011, 04:29 PM   #37
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Another example:

Code:
number = 5

number + 3 # evaluates to 8

# "5 + 3" gets translated to "5.__op_add(3)"

number.__op_add = number.__op_sub

number + 3 # evaluates to 2
Remember that when you type a number, it actaully creates an env, not some strange intermediate value.
 
Old 02-13-2011, 06:00 PM   #38
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 02-13-2011, 07:11 PM   #39
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Sergei Steshenko View Post
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 that's not the purpose of closures!
 
Old 02-13-2011, 07:15 PM   #40
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by MTK358 View Post
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 View Post
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 View Post
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.
 
Old 02-13-2011, 08:24 PM   #41
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
So how would you do assignment considering that everything is an object and that variables are references to objects contained in env's?
 
Old 02-13-2011, 08:28 PM   #42
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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 View Post
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.
 
Old 02-13-2011, 09:30 PM   #43
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by MTK358 View Post
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 View Post
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.
 
Old 02-14-2011, 12:16 AM   #44
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
But that's not the purpose of closures!
And that was what you were against - you were against a function which depends on existence of a variable defined outside of it.
 
Old 02-14-2011, 01:44 AM   #45
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
would and could somebody help me with my homework assignment please... DarDevy Linux - Newbie 3 04-20-2009 02:43 PM
LXer: Java Data Objects and Service Data Objects in SOA LXer Syndicated Linux News 0 01-17-2009 06:10 AM
Objects in C?? kickzha Programming 6 06-17-2006 08:38 PM
IP address assignment n3tw0rk Linux - Networking 1 01-05-2004 12:23 AM
need help with class assignment tenraek Linux - General 4 04-03-2003 12:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration