http://qs321.pair.com?node_id=11138797


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.