#! /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;
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Discussion on: Daily Coding Puzzles - Oct 29th - Nov 2nd
Replies for: Thursday Calculating with Functions: This time we want to write calculations using functions and get the results. For example: seven(times(five...
For further actions, you may consider blocking this person and/or reporting abuse