NAME

Data::Object::Code

ABSTRACT

Data-Object Code Class

SYNOPSIS

use Data::Object::Code;

my $code = Data::Object::Code->new(sub { shift + 1 });

DESCRIPTION

This package provides routines for operating on Perl 5 code references.

INHERITANCE

This package inherits behaviors from:

Data::Object::Code::Base

INTEGRATIONS

This package integrates behaviors from:

Data::Object::Role::Dumpable

Data::Object::Role::Functable

Data::Object::Role::Throwable

LIBRARIES

This package uses type constraints defined by:

Data::Object::Library

METHODS

This package implements the following methods.

call

call(Any $arg1) : Any

The call method executes and returns the result of the code. This method returns a data type object to be determined after execution.

call example
# given sub { (shift // 0) + 1 }

$code->call; # 1
$code->call(0); # 1
$code->call(1); # 2
$code->call(2); # 3

compose

compose(CodeRef $arg1, Any $arg2) : CodeObject

The compose method creates a code reference which executes the first argument (another code reference) using the result from executing the code as it's argument, and returns a code reference which executes the created code reference passing it the remaining arguments when executed. This method returns a Data::Object::Code object.

compose example
# given sub { [@_] }

$code = $code->compose($code, 1,2,3);
$code->(4,5,6); # [[1,2,3,4,5,6]]

# this can be confusing, here's what's really happening:
my $listing = sub {[@_]}; # produces an arrayref of args
$listing->($listing->(@args)); # produces a listing within a listing
[[@args]] # the result

conjoin

conjoin(CodeRef $arg1) : CodeObject

The conjoin method creates a code reference which execute the code and the argument in a logical AND operation having the code as the lvalue and the argument as the rvalue. This method returns a Data::Object::Code object.

conjoin example
# given sub { $_[0] % 2 }

$code = $code->conjoin(sub { 1 });
$code->(0); # 0
$code->(1); # 1
$code->(2); # 0
$code->(3); # 1
$code->(4); # 0

curry

curry(CodeRef $arg1) : CodeObject

The curry method returns a code reference which executes the code passing it the arguments and any additional parameters when executed. This method returns a Data::Object::Code object.

curry example
# given sub { [@_] }

$code = $code->curry(1,2,3);
$code->(4,5,6); # [1,2,3,4,5,6]

defined

defined() : NumObject

The defined method returns true if the object represents a value that meets the criteria for being defined, otherwise it returns false. This method returns a Data::Object::Number object.

defined example
# given $code

$code->defined; # 1

disjoin

disjoin(CodeRef $arg1) : CodeRef

The disjoin method creates a code reference which execute the code and the argument in a logical OR operation having the code as the lvalue and the argument as the rvalue. This method returns a Data::Object::Code object.

disjoin example
# given sub { $_[0] % 2 }

$code = $code->disjoin(sub { -1 });
$code->(0); # -1
$code->(1); #  1
$code->(2); # -1
$code->(3); #  1
$code->(4); # -1

next

next(Any $arg1) : Any

The next method is an alias to the call method. The naming is especially useful (i.e. helps with readability) when used with closure-based iterators. This method returns a Data::Object::Code object. This method is an alias to the call method.

next example
$code->next;

rcurry

rcurry(Any $arg1) : CodeObject

The rcurry method returns a code reference which executes the code passing it the any additional parameters and any arguments when executed. This method returns a Data::Object::Code object.

rcurry example
# given sub { [@_] }

$code = $code->rcurry(1,2,3);
$code->(4,5,6); # [4,5,6,1,2,3]

self

self() : Object

The self method returns the calling object (noop).

self example
my $self = $code->self();

CREDITS

Al Newkirk, +319

Anthony Brummett, +10

Adam Hopkins, +2

José Joaquín Atria, +1

AUTHOR

Al Newkirk, awncorp@cpan.org

LICENSE

Copyright (C) 2011-2019, Al Newkirk, et al.

This is free software; you can redistribute it and/or modify it under the terms of the The Apache License, Version 2.0, as elucidated here, https://github.com/iamalnewkirk/do/blob/master/LICENSE.

PROJECT

Wiki

Project

Initiatives

Milestones

Contributing

Issues

SEE ALSO

To get the most out of this distribution, consider reading the following:

Do

Data::Object

Data::Object::Class

Data::Object::ClassHas

Data::Object::Role

Data::Object::RoleHas

Data::Object::Library