re: Daily Coding Puzzles - Oct 29th - Nov 2nd VIEW POST

TOP OF THREAD FULL DISCUSSION
re: Thursday Calculating with Functions: This time we want to write calculations using functions and get the results. For example: seven(times(five...
 
#! /usr/bin/perl
use warnings;
use strict;

sub one   { _num(@_, 1) }
sub two   { _num(@_, 2) }
sub three { _num(@_, 3) }
sub four  { _num(@_, 4) }
sub five  { _num(@_, 5) }
sub six   { _num(@_, 6) }
sub seven { _num(@_, 7) }
sub eight { _num(@_, 8) }
sub nine  { _num(@_, 9) }
sub zero  { _num(@_, 0) }

sub _num { ref $_[0] ? $_[0]->($_[1]) : $_[0] }

sub plus       { my $x = shift; sub {     shift() + $x } }
sub minus      { my $x = shift; sub {     shift() - $x } }
sub Times      { my $x = shift; sub {     shift() * $x } }
sub divided_by { my $x = shift; sub { int(shift() / $x) } }

use Test::More tests => 4;

is four(plus(nine())), 13;
is eight(minus(three())), 5;
is seven(Times(five())), 35;
is six(divided_by(two())), 3;
code of conduct - report abuse