The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Venus::Code - Code Class

ABSTRACT

Code Class for Perl 5

SYNOPSIS

  package main;

  use Venus::Code;

  my $code = Venus::Code->new(sub {
    my (@args) = @_;

    return [@args];
  });

  # $code->call(1..4);

DESCRIPTION

This package provides methods for manipulating code data.

INHERITS

This package inherits behaviors from:

Venus::Kind::Value

METHODS

This package provides the following methods:

call

  call(Any @data) (Any)

The call method executes and returns the result of the code.

Since 0.01

call example 1
  package main;

  use Venus::Code;

  my $code = Venus::Code->new(sub { ($_[0] // 0) + 1 });

  my $call = $code->call;

  # 1
call example 2
  package main;

  use Venus::Code;

  my $code = Venus::Code->new(sub { ($_[0] // 0) + 1 });

  my $call = $code->call(1);

  # 2
call example 3
  package main;

  use Venus::Code;

  my $code = Venus::Code->new(sub { ($_[0] // 0) + 1 });

  my $call = $code->call(2);

  # 3

compose

  compose(CodeRef $code, Any @data) (CodeRef)

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.

Since 0.01

compose example 1
  package main;

  use Venus::Code;

  my $code = Venus::Code->new(sub { [@_] });

  my $compose = $code->compose($code, 1, 2, 3);

  # sub { ... }

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

conjoin

  conjoin(CodeRef $code) (CodeRef)

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.

Since 0.01

conjoin example 1
  package main;

  use Venus::Code;

  my $code = Venus::Code->new(sub { $_[0] % 2 });

  my $conjoin = $code->conjoin(sub { 1 });

  # sub { ... }

  # $conjoin->(0); # 0
  # $conjoin->(1); # 1
  # $conjoin->(2); # 0
  # $conjoin->(3); # 1
  # $conjoin->(4); # 0

curry

  curry(Any @data) (CodeRef)

The curry method returns a code reference which executes the code passing it the arguments and any additional parameters when executed.

Since 0.01

curry example 1
  package main;

  use Venus::Code;

  my $code = Venus::Code->new(sub { [@_] });

  my $curry = $code->curry(1, 2, 3);

  # sub { ... }

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

default

  default() (CodeRef)

The default method returns the default value, i.e. sub{}.

Since 0.01

default example 1
  # given: synopsis;

  my $default = $code->default;

  # sub {}

disjoin

  disjoin(CodeRef $code) (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.

Since 0.01

disjoin example 1
  package main;

  use Venus::Code;

  my $code = Venus::Code->new(sub { $_[0] % 2 });

  my $disjoin = $code->disjoin(sub { -1 });

  # sub { ... }

  # disjoin->(0); # -1
  # disjoin->(1); #  1
  # disjoin->(2); # -1
  # disjoin->(3); #  1
  # disjoin->(4); # -1

next

  next(Any @data) (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.

Since 0.01

next example 1
  package main;

  use Venus::Code;

  my $code = Venus::Code->new(sub { $_[0] * 2 });

  my $next = $code->next(72);

  # 144

rcurry

  rcurry(Any @data) (CodeRef)

The rcurry method returns a code reference which executes the code passing it the any additional parameters and any arguments when executed.

Since 0.01

rcurry example 1
  package main;

  use Venus::Code;

  my $code = Venus::Code->new(sub { [@_] });

  my $rcurry = $code->rcurry(1,2,3);

  # sub { ... }

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

OPERATORS

This package overloads the following operators:

operation: (&{})

This package overloads the &{} operator.

example 1

  # given: synopsis;

  my $result = &$code(1..4);

  # [1..4]

AUTHORS

Cpanery, cpanery@cpan.org

LICENSE

Copyright (C) 2021, Cpanery

Read the "license" file.