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

AUTHOR

aereal <aereal@aereal.org>

SEE ALSO

Data::Monad::Base::Monad

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.