Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^6: What technical benfits perl offers over python + few more questions. (generator/iterator/closure)

by LanX (Sage)
on Nov 13, 2021 at 20:20 UTC ( #11138797=note: print w/replies, xml ) Need Help??


in reply to Re^5: What technical benfits perl offers over python + few more questions.
in thread What technical benfits perl offers over python + few more questions.

> My understanding of an iterator function

here an example of a generator creating an iterator and using closure variables.

use strict; use warnings; sub gen_range_step { my ( $start,$stop,$step ) = @_ ; my $current = $start; return sub { my $val = $current; $current += $step; return $val if $val <=$stop; return; } } my $iter = gen_range_step(5,20,4); while ( my ($val) = $iter->() ) { print "$val\n"; }

C:/Strawberry/perl/bin\perl.exe -w d:/tmp/pm/generator_iterator.pl 5 9 13 17 Compilation finished at Sat Nov 13 21:18:20

> but keep in mind I studied engineering and not computer science

I studied mathematics and some CS, and FP was never mentioned.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

  • Comment on Re^6: What technical benfits perl offers over python + few more questions. (generator/iterator/closure)
  • Select or Download Code

Replies are listed 'Best First'.
Re^7: What technical benfits perl offers over python + few more questions.
by talexb (Chancellor) on Nov 13, 2021 at 21:55 UTC

    Thanks for the explanation -- the difference is that Perl can return a CODEREF, but C doesn't have that functionality. It would be possible to initialize a function to behave like an iterator, but without a great deal of jumping through hoops, you wouldn't be able to run multiple different iterators, as your Perl example could.

    That's OK -- without C, you wouldn't have pretty much any of the modern languages I can think of (Perl, Python, PHP). And C is still an amazing language.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11138797]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2022-05-21 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (76 votes). Check out past polls.

    Notices?