Hike (hike v0.0.1)
The Hike
module provides an implementation of the Optional data types.
It defines
a struct
Hike.Option
with a single fieldvalue
which can either benil
or any other value of typet
.a struct
Hike.Either
that represents an "either/or" value. It can contain either aleft
value or aright
value, but not botha struct
Hike.MayFail
that represents an "either/or" value. It can contain either aFailure
value or aSuccess
value, but not both.
This implementation provides shorthand functions to work with Optional data, including mapping, filtering, applying and many more functions to the value inside the Optional data.
Link to this section Summary
Types
generic input type <T>
.
generic input type <TArg1>
.
generic input type <TArg2>
.
generic input type <TArg3>
.
generic input type <TArg4>
.
generic return type <TR>
.
Functions
wraps a function call if function runs successfully will return MayFail
in Success
state
otherwise return MayFail
in Failure
state.
wraps a function with arity 1 call if function runs successfully will return MayFail
in Success
state
otherwise return MayFail
in Failure
state.
wraps a function with arity 2 call if function runs successfully will return MayFail
in Success
state
otherwise return MayFail
in Failure
state.
wraps a function with arity 3 call if function runs successfully will return MayFail
in Success
state
otherwise return MayFail
in Failure
state.
wraps a function with arity 4 call if function runs successfully will return MayFail
in Success
state
otherwise return MayFail
in Failure
state.
Link to this section Types
exception()
@type exception() :: :error
@type t() :: any()
generic input type <T>
.
tArg1()
@type tArg1() :: any()
generic input type <TArg1>
.
tArg2()
@type tArg2() :: any()
generic input type <TArg2>
.
tArg3()
@type tArg3() :: any()
generic input type <TArg3>
.
tArg4()
@type tArg4() :: any()
generic input type <TArg4>
.
tr()
@type tr() :: any()
generic return type <TR>
.
Link to this section Functions
apply(opt, func)
@spec apply(Hike.Option.option(t()), Hike.Option.func(t())) :: Hike.Option.option(tr())
@spec apply(Hike.Option.option(), Hike.Option.func() | Hike.Option.func(t())) :: Hike.Option.option()
apply_failure(mayfail, func)
@spec apply_failure( Hike.MayFail.mayfail_failure(Hike.MayFail.t_failure()), Hike.MayFail.func(Hike.MayFail.t_failure()) ) :: Hike.MayFail.mayfail_failure(tr())
@spec apply_failure( Hike.MayFail.mayfail_success(Hike.MayFail.t_success()), Hike.MayFail.func(Hike.MayFail.t_failure()) ) :: Hike.MayFail.mayfail_success(Hike.MayFail.t_success())
apply_left(eth, func)
@spec apply_left( Hike.Either.either_left(Hike.Either.t_left()), (Hike.Either.t_left() -> tr()) ) :: Hike.Either.either_left(tr())
@spec apply_left( Hike.Either.either_right(Hike.Either.t_right()), (Hike.Either.t_left() -> tr()) ) :: Hike.Either.either_right(Hike.Either.t_right())
apply_right(eth, func)
@spec apply_right( Hike.Either.either_right(Hike.Either.t_right()), (Hike.Either.t_right() -> tr()) ) :: Hike.Either.either_right(tr())
@spec apply_right( Hike.Either.either_left(Hike.Either.t_left()), (Hike.Either.t_right() -> tr()) ) :: Hike.Either.either_left(Hike.Either.t_left())
apply_success(mayfail, func)
@spec apply_success( Hike.MayFail.mayfail_success(Hike.MayFail.t_success()), Hike.MayFail.func(Hike.MayFail.t_success()) ) :: Hike.MayFail.mayfail_success(tr())
@spec apply_success( Hike.MayFail.mayfail_failure(Hike.MayFail.t_failure()), Hike.MayFail.func(Hike.MayFail.t_success()) ) :: Hike.MayFail.mayfail_failure(Hike.MayFail.t_failure())
bind(opt, func)
@spec bind(Hike.Option.option(), Hike.Option.binder() | Hike.Option.binder(t())) :: Hike.Option.option()
@spec bind(Hike.Option.option(t()), Hike.Option.binder(t())) :: Hike.Option.option(tr())
bind_failure(mayfail, func)
@spec bind_failure( Hike.MayFail.mayfail_failure(Hike.MayFail.t_failure()), Hike.MayFail.binder(Hike.MayFail.t_failure()) ) :: Hike.MayFail.mayfail_failure(tr())
@spec bind_failure( Hike.MayFail.mayfail_failure(Hike.MayFail.t_success()), Hike.MayFail.binder(Hike.MayFail.t_failure()) ) :: Hike.MayFail.mayfail_failure(Hike.MayFail.t_success())
bind_left(eth, func)
@spec bind_left( Hike.Either.either_left(Hike.Either.t_left()), Hike.Either.binder(Hike.Either.t_left()) ) :: Hike.Either.either_left(tr()) | Hike.Either.either_right(tr())
@spec bind_left( Hike.Either.either_right(Hike.Either.t_right()), Hike.Either.binder(Hike.Either.t_left()) ) :: Hike.Either.either_right(Hike.Either.t_right())
bind_right(eth, func)
@spec bind_right( Hike.Either.either_right(Hike.Either.t_right()), Hike.Either.binder(Hike.Either.t_right()) ) :: Hike.Either.either_right(tr()) | Hike.Either.either_left(tr())
@spec bind_right( Hike.Either.either_left(Hike.Either.t_left()), Hike.Either.binder(Hike.Either.t_right()) ) :: Hike.Either.either_left(Hike.Either.t_left())
bind_success(mayfail, func)
@spec bind_success( Hike.MayFail.mayfail_success(Hike.MayFail.t_success()), Hike.MayFail.binder(Hike.MayFail.t_success()) ) :: Hike.MayFail.mayfail_success(tr())
@spec bind_success( Hike.MayFail.mayfail_failure(Hike.MayFail.t_failure()), Hike.MayFail.binder(Hike.MayFail.t_success()) ) :: Hike.MayFail.mayfail_failure(Hike.MayFail.t_failure())
either(value)
@spec either({:ok, t()}) :: Hike.Either.either_right(t())
@spec either({:error, t()}) :: Hike.Either.either_left(t())
@spec either(t()) :: Hike.Either.either_right(t())
failure(msg)
@spec failure(exception()) :: Hike.MayFail.mayfail_failure(exception())
filter(opt, func)
left(value)
@spec left(t()) :: Hike.Either.either_left(t())
map(opt, func)
@spec map(Hike.Option.option(), Hike.Option.mapper()) :: Hike.Option.option(t())
@spec map(Hike.Option.option(), Hike.Option.mapper() | Hike.Option.mapper(t())) :: Hike.Option.option()
map_failure(mayfail, func)
@spec map_failure( Hike.MayFail.mayfail_failure(Hike.MayFail.t_failure()), Hike.MayFail.mapper(Hike.MayFail.t_failure()) ) :: Hike.MayFail.mayfail_failure(tr())
@spec map_failure( Hike.MayFail.mayfail_failure(Hike.MayFail.t_success()), Hike.MayFail.mapper(Hike.MayFail.t_failure()) ) :: Hike.MayFail.mayfail_failure(Hike.MayFail.t_success())
map_left(eth, func)
@spec map_left( Hike.Either.either_left(Hike.Either.t_left()), (Hike.Either.t_left() -> tr()) ) :: Hike.Either.either_left(tr())
@spec map_left( Hike.Either.either_right(Hike.Either.t_right()), (Hike.Either.t_left() -> tr()) ) :: Hike.Either.either_right(Hike.Either.t_right())
map_right(eth, func)
@spec map_right( Hike.Either.either_right(Hike.Either.t_right()), (Hike.Either.t_right() -> tr()) ) :: Hike.Either.either_right(tr())
@spec map_right( Hike.Either.either_left(Hike.Either.t_left()), (Hike.Either.t_right() -> tr()) ) :: Hike.Either.either_left(Hike.Either.t_left())
map_success(mayfail, func)
@spec map_success( Hike.MayFail.mayfail_success(Hike.MayFail.t_success()), Hike.MayFail.mapper() ) :: Hike.MayFail.mayfail_success(tr())
@spec map_success( Hike.MayFail.mayfail_failure(Hike.MayFail.t_failure()), Hike.MayFail.mapper(Hike.MayFail.t_success()) ) :: Hike.MayFail.mayfail_failure(Hike.MayFail.t_failure())
match(opt, some_func, none_func)
@spec match( Hike.Option.option(t()) | Hike.Option.option(), Hike.Option.func(t()), Hike.Option.func() ) :: tr()
@spec match( Hike.Either.either(Hike.Either.t_left(), Hike.Either.t_right()), (Hike.Either.t_left() -> tr()), (Hike.Either.t_right() -> tr()) ) :: tr()
@spec match( Hike.MayFail.mayfail(Hike.MayFail.t_failure(), Hike.MayFail.t_success()), (Hike.MayFail.t_failure() -> tr()), (Hike.MayFail.t_success() -> tr()) ) :: tr()
mayfail(value)
@spec mayfail({:ok, t()}) :: Hike.MayFail.mayfail_success(t())
@spec mayfail({:error, exception()}) :: Hike.MayFail.mayfail_failure(exception())
@spec mayfail(t()) :: Hike.MayFail.mayfail_success(t())
option()
@spec option() :: Hike.Option.option()
option(value)
@spec option({:ok, t()}) :: Hike.Option.option(t())
@spec option({:error, exception()}) :: Hike.Option.option()
@spec option(t()) :: Hike.Option.option() | Hike.Option.option(t())
right(value)
@spec right(t()) :: Hike.Either.either_right(t())
success(value)
@spec success(t()) :: Hike.MayFail.mayfail_success(t())
try(func)
@spec try((() -> tr() | exception())) :: Hike.MayFail.mayfail()
wraps a function call if function runs successfully will return MayFail
in Success
state
otherwise return MayFail
in Failure
state.
try(func, arg1)
@spec try((tArg1() -> tr() | exception()), tArg1()) :: Hike.MayFail.mayfail()
wraps a function with arity 1 call if function runs successfully will return MayFail
in Success
state
otherwise return MayFail
in Failure
state.
example
Example
iex> add1 = fn (x) -> x + 1 end
iex> Hike.try(add1, 5) |> Hike.MayFail.map_success(fn x -> x end )
%Hike.MayFail{failure: nil, success: 6, is_success?: true}
try(func, arg1, arg2)
wraps a function with arity 2 call if function runs successfully will return MayFail
in Success
state
otherwise return MayFail
in Failure
state.
example
Example
iex> divide = fn (x, y) -> x / y end
iex> Hike.try(divide, 5, 0) |>
...> Hike.MayFail.map_success(fn x -> {:ok, x + 1} end ) |>
...> Hike.MayFail.map_failure(fn x -> String.upcase(x) end)
%Hike.MayFail{
failure: "BAD ARGUMENT IN ARITHMETIC EXPRESSION",
success: nil,
is_success?: false
}
try(func, arg1, arg2, arg3)
@spec try( (tArg1(), tArg2(), tArg3() -> tr() | exception()), tArg1(), tArg2(), tArg3() ) :: Hike.MayFail.mayfail()
wraps a function with arity 3 call if function runs successfully will return MayFail
in Success
state
otherwise return MayFail
in Failure
state.
try(func, arg1, arg2, arg3, arg4)
@spec try( (tArg1(), tArg2(), tArg3(), tArg4() -> tr() | exception()), tArg1(), tArg2(), tArg3(), tArg4() ) :: Hike.MayFail.mayfail()
wraps a function with arity 4 call if function runs successfully will return MayFail
in Success
state
otherwise return MayFail
in Failure
state.