in reply to Currying--useful examples?
In both cases, you don't need to do it. Instead of creating the object, you could pass around all the parameters needed to construct the object everywhere you'd pass the object instead.
Both approaches "bundle" a bunch of information into a single context which can operate on things and be handily passed around.
Another way of looking at is both approaches allow you to make a specific tool (tailored object from factory or curried function) from a general one (factory or multi-arg function).
It's also very useful in 'map' or 'filter' (i.e. grep in perl), like situations, where you want a single-arg function to run the map or filter. e.g.
It's still a bit contrived, but I hope you can see the idea. In a more functional language 'grep' is written as 'filter' and would generally take a one-arg function as it's first argument.sub mk_less_than_filter { my $limit = shift; return sub { my $elt = shift; return $elt < $limit; }; } sub find_less_than { my $limit = shift; my $vals = shift; my $filter = mk_less_than_filter($limit); return grep {$filter->($_)} @$vals; } print join(", ", find_less_than(4, [1, 2, 7, 10, 3, 8])), "\n"; # Prints 1, 2, 3
|
|---|