Ancient
view release on metacpan or search on metacpan
lib/util.pm view on Meta::CPAN
=head2 memo
my $cached = memo(\&expensive_function);
my $result = $cached->($arg);
Returns a memoized version of the given function. Results are cached
based on arguments, so repeated calls with the same arguments return
instantly from the cache.
=head2 pipeline
my $result = pipeline($initial_value, \&fn1, \&fn2, \&fn3);
Pipes a value through a series of functions, passing the result of each
function as the argument to the next. Equivalent to C<fn3(fn2(fn1($value)))>
but more readable.
=head2 compose
my $pipeline = compose(\&fn3, \&fn2, \&fn1);
my $result = $pipeline->($value);
Creates a new function that composes the given functions right-to-left.
C<compose(\&c, \&b, \&a)> creates a function equivalent to C<sub { c(b(a(@_))) }>.
=head2 partial
my $add5 = partial(\&add, 5);
my $result = $add5->(3); # add(5, 3) = 8
Creates a partially applied function with some arguments pre-bound.
The returned function, when called, prepends the bound arguments
to any new arguments.
B<Note:> Creating AND calling a partial is 125% faster than pure Perl.
However, repeatedly calling an already-created partial is ~20% slower
than a hand-written closure. Use partial when you create once and call
many times from different contexts, or for cleaner functional code.
=head2 lazy
my $deferred = lazy { expensive_computation() };
Creates a lazy value that defers computation until forced. The computation
runs at most once; subsequent forces return the cached result.
=head2 force
my $result = force($lazy_value);
Forces evaluation of a lazy value, returning the computed result.
If the value has already been forced, returns the cached result.
Non-lazy values pass through unchanged.
=head2 dig
my $val = dig($hashref, @keys);
my $val = dig($hashref, 'a', 'b', 'c'); # $hashref->{a}{b}{c}
Safely traverses a nested hash structure. Returns undef if any key
is missing, without throwing an exception.
=head2 tap
my $result = tap(\&block, $value);
my $result = tap(sub { print "Debug: $_\n" }, $value);
Executes a side-effect block with the value (setting C<$_> and passing
as argument), then returns the original value unchanged. Useful for
debugging pipelines without affecting data flow.
=head2 clamp
my $clamped = clamp($value, $min, $max);
Constrains a numeric value to a range. Returns C<$min> if C<$value E<lt> $min>,
C<$max> if C<$value E<gt> $max>, otherwise returns C<$value>.
=head2 identity
my $same = identity($value);
Returns the argument unchanged. Uses compile-time optimization to
eliminate the function call entirely. Useful as a default transformer
in pipelines or when an API requires a function but you want a no-op.
=head2 always
my $get_value = always($constant);
$get_value->(); # Returns $constant
$get_value->(1,2,3); # Still returns $constant (args ignored)
Creates a function that always returns the same value, ignoring any arguments.
Useful for callbacks that need to return a fixed value.
=head2 noop
noop(); # Returns undef
noop(1, 2, 3); # Ignores args, returns undef
Does nothing, returns undef. Ignores all arguments. Useful as a default
callback or placeholder.
B<Note:> This returns C<undef> (not empty list) for correct behavior in
map contexts. The standalone C<noop> module returns empty list which is
~45% faster but produces different results in C<map { noop() } @list>.
=head2 stub_true, stub_false
stub_true(); # Always returns 1
stub_false(); # Always returns ''
Constant functions that always return true or false. Useful as default
predicates:
my @all = grep { stub_true() } @items; # Accepts all
my @none = grep { stub_false() } @items; # Rejects all
=head2 stub_array, stub_hash
my $arr = stub_array(); # Returns new []
( run in 1.178 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )