NAME
Data::Monad::Either - The Either monad
SYNOPSIS
use Data::Monad::Either qw/left right/;
sub get_key {
my ($key) = @_;
return sub {
my ($data) = @_;
return left('value is not a hash') unless ref($data) eq 'HASH';
return exists($data->{$key}) ? right($data->{$key}) : left("data has no values for key:$key");
};
}
my $commit_data = { commit => { author => 'Larry' } };
my $right = right($data)->flat_map(get_key('commit'))->flat_map(get_key('author'));
$right->value; # => 'Larry'
my $not_hash = right(['Larry'])->flat_map(get_key('commit'))->flat_map(get_key('author'));
$not_hash->value; # => 'value is not a hash'
my $not_exists_key = right($data)->flat_map(get_key('parent_commit'))->flat_map(get_key('author'));
$not_exists_key->value; # => 'data has no values for key:parent_commit'
DESCRIPTION
Data::Monad::Either represents values with 2 possibilities.
METHODS
- $right = right(@values)
- $left = left(@values)
-
The constructors of this class.
- $bool = $either->is_right
- $bool = $either->is_left
-
Checks if
$either
is right (correct) or left (failure) - $either->unit(@values)
- $either->flat_map(sub { })
-
Overrides methods of Data::Monad::Base::Monad
- @values = $either->value
-
Returns a list of values which is contained by
$either
- $folded = $either->fold(sub { ... }, sub { ... });
-
Run the first function if left, or the second.
These given functions take a value contained by
$either
. - $either_or = $either->or_else($else);
-
Returns this Either monad if it is right, or returns the given value.
- $get_or_else = $either->get_or_else(@value_else);
-
Returns the values contains by this, or returns the given default value.
- $value_or = $either->value_or(sub { ... });
-
Returns the values contains by this, or runs the given default function on the left value.
- $swapped = $either->swap;
-
Flip the left right values.
- $either = $either->left_map(sub { ... });
-
Run the given function on the left value.
AUTHOR
aereal <aereal@aereal.org>
SEE ALSO
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.