stevengharms.com

Sententiae viri ex temporibus duobus

Benefits of “Functional Perl”: Ease of Modification

In an earlier post I provided code demonstrating my “functional” Perl idiom. The purpose of that code was to take a very simply formatted text file and to turn it into LaTeX Beamer formatting.

Well, recently I found the application iFlipr. In addition to being a site where you can upload flash cards, it also has an iTouch / iPhone version so that you can review when you’re in the bus, in a waiting room, etc.

So, I needed some code to transform my generic data set into–not LaTex–but iFlipr format. With but the most trivial of changes, I was able to accomplish this. The high readability of “functional” Perl made this, literally, a 3 minute affair.

Here’s the diff:

[code lang=“diff”] 56c56

< &produce;_beamer_body(

  &produce;_iflipr_body(

81c81

< sub produce_beamer_body

sub produce_iflipr_body 83c83

< (my $latex_output_file = $_[0]->{file} ) =~ s/..*$// ;

(my $iflipr_output_file = $_[0]->{file} ) =~ s/..*$// ; 85,93c85,86 < open (LATEX, “>$latex_output_file.tex”); <
< # A technique to tell Perl not to paginate < # ( i.e. re-print LATEX_TOP format ) again <
< my $ltx = select LATEX; < $= = 9990; < select $ltx;

<

open (IFLIPR, “>$iflipr_output_file.iflipr.txt”);

96,97c89 < my @order = sort { $a <=> $b } ( keys ( %$ds ) );

< for ( @order )

for ( keys %$ds ) 100,103c92,93 < $part = $ds->{$}[1]; < $meaning = $ds->{$}[2]; < chomp($word, $part, $meaning);

< write (LATEX);

  $meaning = $ds->{$_}[2];    
  print IFLIPR "$word\t$meaning\n";

105,107c95,96 < < print LATEX $end_of_document;

< close LATEX;

close IFLIPR;

[/code]

If you’ve not thought about writing code in this fashion, I hope this entices you! Either that or we should all take up Haskell or Lisp “Lisp (programming language) - Wikipedia, the free encyclopedia”)

Comments