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 03-20-2009, 01:30 AM   #16
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244

Quote:
Originally Posted by Sergei Steshenko View Post
For example, encapsulation and abstraction can be achieved in functional programming - if you are interested, I can publish some examples. And the examples will most likely show the difference between Perl and Python - according to my limited Python knowledge you won't be able to implement the same concepts in Python.
there are many discussions about Python encapsulation
and functional programming in the newsgroup.
also, if you really want to know how your examples can be implemented in Python, make a post to the newsgroup. The experts there can help you. Otherwise, i don't really think you should judge something based on limited knowledge.
 
Old 03-20-2009, 01:54 AM   #17
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
there are many discussions about Python encapsulation
and functional programming in the newsgroup.
also, if you really want to know how your examples can be implemented in Python, make a post to the newsgroup. The experts there can help you. Otherwise, i don't really think you should judge something based on limited knowledge.
I can post my examples here, and you as an expert will tell me whether they can be implemented in Python.
...
AFAIR, Python does not fully support lexical scoping (according to Wiki article), and my examples are based on lexical scoping.

Last edited by Sergei Steshenko; 03-20-2009 at 02:02 AM.
 
Old 03-20-2009, 02:14 AM   #18
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
I can post my examples here, and you as an expert will tell me whether it can be implemented in Python.
i am not an expert in the area of OO, functional programming, lexical scoping, that's why i suggest to you if you want to know more or you want to find Python equivalent of those examples you have, go to the Python newsgroup. There are lots of experts there who can answer your questions. Until it is proven that it can't be implemented, no point in judging anything.
 
Old 03-20-2009, 02:17 AM   #19
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
i am not an expert in the area of OO, functional programming, lexical scoping, that's why i suggest to you if you want to know more or you want to find Python equivalent of those examples you have, go to the Python newsgroup. There are lots of experts there who can answer your questions. Until it is proven that it can't be implemented, no point in judging anything.
The only thing that needs to be verified is that Python doesn't fully support lexical scoping - it it is so, my examples can't be implemented in Python.

And Python hasn't fully supported lexical scoping from the very beginning.
 
Old 03-20-2009, 02:38 AM   #20
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
The only thing that needs to be verified is that Python doesn't fully support lexical scoping - it it is so, my examples can't be implemented in Python.
post 1 example then. Not too long, but sufficient to make you believe it can't be implemented.
 
Old 03-20-2009, 03:02 AM   #21
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
post 1 example then. Not too long, but sufficient to make you believe it can't be implemented.
Here it is - it's just the beginning, I also intend later to show code reuse and inheritance - kind of.

The point if this example is scoping + closures + no need to thik about variable names because lexical scoping encapsulates the variables:

Code:
sergei@amdam2:~/junk> cat -n ./points_setters_getters.pl
     1  #!/usr/bin/perl -w
     2
     3  use strict;
     4
     5
     6  my $setter_1d_sub;
     7  my $getter_1d_sub;
     8
     9  # 1d point:
    10    {
    11    my $x; # private data which is invisible in outer scopes
    12    my $setter_sub = sub{$x = shift};
    13    my $getter_sub = sub{$x};
    14
    15    # "connection" with the external world:
    16
    17    $setter_1d_sub = $setter_sub;
    18    $getter_1d_sub = $getter_sub;
    19    }
    20
    21
    22  my $setter_2d_sub;
    23  my $getter_2d_sub;
    24
    25  # 2d point:
    26    {
    27    my ($x, $y); # private data which is invisible in outer scopes
    28
    29    my $setter_sub = sub{$x = shift; $y = shift};
    30    my $getter_sub = sub{($x, $y)};
    31
    32    # "connection" with the external world:
    33
    34    $setter_2d_sub = $setter_sub;
    35    $getter_2d_sub = $getter_sub;
    36    }
    37
    38  &{$setter_1d_sub}(1);
    39  &{$setter_2d_sub}(2, 3);
    40
    41  warn "1d point: ", &{$getter_1d_sub}(), " 2d point: ", join(' ', &{$getter_2d_sub}());
sergei@amdam2:~/junk> ./points_setters_getters.pl
1d point: 1 2d point: 2 3 at ./points_setter_getters.pl line 41.
Also, I do not functionally need to introduce internal $setter_sub, $getter_sub and then "connect" them - I could have directly assigned values to $setter_1d_sub, $getter_1d_sub, $setter_2d_sub, $getter_2d_sub inside the lexical scopes, but I have some more things on my mind to be shown later.
 
Old 03-20-2009, 07:43 AM   #22
mannclay
Member
 
Registered: Nov 2007
Location: Brooklyn, NY
Distribution: eeeMint
Posts: 52

Original Poster
Rep: Reputation: 15
yeah, I did purchase a copy of 'the llama book' and wanted to ask what you guys thought of certifications like SCO ACE etc. for pumping up a resume?

Last edited by mannclay; 03-20-2009 at 08:34 AM.
 
Old 03-20-2009, 02:58 PM   #23
The_Kernel
LQ Newbie
 
Registered: Nov 2008
Posts: 19

Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
Here it is - it's just the beginning, I also intend later to show code reuse and inheritance - kind of.

The point if this example is scoping + closures + no need to thik about variable names because lexical scoping encapsulates the variables:

Code:
sergei@amdam2:~/junk> cat -n ./points_setters_getters.pl
     1  #!/usr/bin/perl -w
     2
     3  use strict;
     4
     5
     6  my $setter_1d_sub;
     7  my $getter_1d_sub;
     8
     9  # 1d point:
    10    {
    11    my $x; # private data which is invisible in outer scopes
    12    my $setter_sub = sub{$x = shift};
    13    my $getter_sub = sub{$x};
    14
    15    # "connection" with the external world:
    16
    17    $setter_1d_sub = $setter_sub;
    18    $getter_1d_sub = $getter_sub;
    19    }
    20
    21
    22  my $setter_2d_sub;
    23  my $getter_2d_sub;
    24
    25  # 2d point:
    26    {
    27    my ($x, $y); # private data which is invisible in outer scopes
    28
    29    my $setter_sub = sub{$x = shift; $y = shift};
    30    my $getter_sub = sub{($x, $y)};
    31
    32    # "connection" with the external world:
    33
    34    $setter_2d_sub = $setter_sub;
    35    $getter_2d_sub = $getter_sub;
    36    }
    37
    38  &{$setter_1d_sub}(1);
    39  &{$setter_2d_sub}(2, 3);
    40
    41  warn "1d point: ", &{$getter_1d_sub}(), " 2d point: ", join(' ', &{$getter_2d_sub}());
sergei@amdam2:~/junk> ./points_setters_getters.pl
1d point: 1 2d point: 2 3 at ./points_setter_getters.pl line 41.
Also, I do not functionally need to introduce internal $setter_sub, $getter_sub and then "connect" them - I could have directly assigned values to $setter_1d_sub, $getter_1d_sub, $setter_2d_sub, $getter_2d_sub inside the lexical scopes, but I have some more things on my mind to be shown later.
All I see you doing in the above example is creating two unnamed functions within the main body of your script. I don't see what's so good about that? Wouldn't it be more sensible to move the two blocks ("1d point", "2d point") into separate functions, which would accomplish not introducing the variables into your main namespace as well make them available to be called later?
 
Old 03-21-2009, 01:54 AM   #24
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by The_Kernel View Post
All I see you doing in the above example is creating two unnamed functions within the main body of your script. I don't see what's so good about that? Wouldn't it be more sensible to move the two blocks ("1d point", "2d point") into separate functions, which would accomplish not introducing the variables into your main namespace as well make them available to be called later?
My point was to show a piece of code which, albeit being simple, cannot (AFAIK) be implemented in Python and to show the power of lexical scoping in avoiding name conflict.

My intent was then to provide a better approximation to OO by indeed creating constructors for 1d and 2d points, and the constructors could be called as many times as one wants. I.e. I meant to convert the example from kind of "inline object" into "create on demand object".

Furthermore, the constructors will return setter and getter - functions are first class objects, aren't they ?

So, you've exactly guessed my intent.

Hopefully later today I'll publish a working example, and this step is not the final one - I want to also show kind of inheritance/enhancement.
 
Old 03-21-2009, 02:16 AM   #25
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
My point was to show a piece of code which, albeit being simple, cannot (AFAIK) be implemented in Python and to show the power of lexical scoping in avoiding name conflict.
Python, does have support for lexical closures it seems. by the way, the sub{} anonymous function in your Perl example is called lambda in Python.

Last edited by ghostdog74; 03-21-2009 at 02:18 AM.
 
Old 03-21-2009, 03:06 AM   #26
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
Python, does have support for lexical closures it seems. by the way, the sub{} anonymous function in your Perl example is called lambda in Python.
From your link ( http://en.wikipedia.org/wiki/Python_..._and_semantics ) :

Quote:
Python's limited support for anonymous functions is the lambda construct. Since the availability of full anonymous functions is non-existent then named functions is the primary use of functions in Python. Lambdas are limited to containing expressions rather than statements, although control flow can still be implemented less elegantly within lambda by using short-circuiting.[3]
 
Old 03-21-2009, 04:05 AM   #27
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
for your example, they are good enough.
 
Old 03-21-2009, 06:03 AM   #28
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
for your example, they are good enough.
Then translate this and consequent examples into Python. If you need explanations on what is happening in the Perl code, I'll explain.
 
Old 03-21-2009, 10:02 AM   #29
The_Kernel
LQ Newbie
 
Registered: Nov 2008
Posts: 19

Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
My point was to show a piece of code which, albeit being simple, cannot (AFAIK) be implemented in Python and to show the power of lexical scoping in avoiding name conflict.

My intent was then to provide a better approximation to OO by indeed creating constructors for 1d and 2d points, and the constructors could be called as many times as one wants. I.e. I meant to convert the example from kind of "inline object" into "create on demand object".

Furthermore, the constructors will return setter and getter - functions are first class objects, aren't they ?

So, you've exactly guessed my intent.

Hopefully later today I'll publish a working example, and this step is not the final one - I want to also show kind of inheritance/enhancement.
So let me take a stab at doing that in Python (I honestly don't know Perl so if I'm missing some subtlety let me know)
Code:
if __name__ == '__main__':
    def point_1d():
        x = 10 
        return x, x + x  

    setter_1d_sub, getter_1d_sub = point_1d()
    print setter_1d_sub
    print getter_1d_sub

    setter_2d_sub = None
    getter_2d_sub = None
   
    def point_2d():
        x = 100

        setter_2d_sub, getter_2d_sub = x, x + x
        print setter_2d_sub
        print getter_2d_sub

        return x, x + x   

    setter_2d_sub, getter_2d_sub = point_2d()
    print setter_2d_sub
    print getter_2d_sub
 
Old 03-21-2009, 10:20 AM   #30
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by The_Kernel View Post
So let me take a stab at doing that in Python (I honestly don't know Perl so if I'm missing some subtlety let me know)
Code:
if __name__ == '__main__':
    def point_1d():
        x = 10 
        return x, x + x  

    setter_1d_sub, getter_1d_sub = point_1d()
    print setter_1d_sub
    print getter_1d_sub

    setter_2d_sub = None
    getter_2d_sub = None
   
    def point_2d():
        x = 100

        setter_2d_sub, getter_2d_sub = x, x + x
        print setter_2d_sub
        print getter_2d_sub

        return x, x + x   

    setter_2d_sub, getter_2d_sub = point_2d()
    print setter_2d_sub
    print getter_2d_sub

No, it's not a good translation.

At the moment I do not have entities called 'point_1d', 'point_2d'.

And I have no default values - do I understand correctly that 'x = 10' and 'x = 100' set default values ?

Also, I do not understand the difference between 'point_1d' and 'point_2d' except for the already mentioned 'x = 10' vs 'x = 100'.

I do not understand 'x + x' in 'return x, x + x' - does the '+' mean simple arithmetic addition ? Was 'x + x' inspired by 'shift' in my code ?

And I do not see the equivalent of my setting the 1d and 2d points, i.e. I do not see the equivalent of

Code:
    38  &{$setter_1d_sub}(1);
    39  &{$setter_2d_sub}(2, 3);
lines in my code.

Could you please publish the full working example which produces exactly the same results ?
 
  


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
Is Perl a dying language? Micro420 Programming 37 05-31-2007 12:49 PM
Documents to learn C language and Perl/python satimis Programming 5 03-19-2004 10:20 AM
need some advice on language choice(Perl vs PHP) coolman0stress Programming 8 11-17-2003 04:41 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
Perl programming AliceC Programming 4 05-17-2001 10:18 PM

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

All times are GMT -5. The time now is 04:54 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