in reply to perl module structure
There's Java's collections as an example where interface List could represent an AbstractList, AbstractSequentialList, ArrayList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector, or something else that implements what a list does, add(), and remove() and whatever else.
Tree::Binary uses Visitor_pattern to separate interface and implementation. You have Visitor::BreadFirstTraversal and Visitor::InOrderTraversal. Both are called with the visit method.
You could just pass in a sub reference to your interface containing the implementation.
Well I guess multiply should use the default.. by default if a third param is not passed in. Like how sort uses standard string comparison order by default. A whole lotta ways to do it.#!/usr/bin/env perl use strict; use warnings; use feature 'say'; sub multiply { my ( $x, $y, $implementation ) = @_; $implementation->( $x, $y ); } sub whateverthedefaultis { my ( $x, $y ) = @_; return $x * $y; } # https://blogs.msdn.microsoft.com/matthew_van_eerde/2009/07/23/bad-pe +rl-russian-peasant-multiplication-algorithm/ sub peasant { my ( $a, $b ) = @_; my $c; map { $c += $_ * $b } grep { $a & $_ } map { 1 << $_ } ( 0 .. log($a) / log 2 ); return $c; } say multiply( 7, 3, \&whateverthedefaultis ); say multiply( 7, 3, \&peasant );
|
|---|