Hike (hike v0.0.2)

The Hike module provides an implementation of the Optional data types. It defines

  • a struct Hike.Option with a single field value which can either be nil or any other value of type t.

  • a struct Hike.Either that represents an "either/or" value. It can contain either a left value or a right value, but not both

  • a struct Hike.MayFailthat represents an "either/or" value. It can contain either a Failure value or a Success 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.

purpose of this is to provide shorthand functions at Hike module itself.

## Example

iex> Hike.option(42)
%Hike.Option{value: 42}

iex> Hike.either(42)
%Hike.Either{l_value: nil, r_value: 42, is_left?: false}

iex> Hike.either({:ok, 42})
%Hike.Either{l_value: nil, r_value: 42, is_left?: false}

iex> Hike.either({:error, :ERROR_MSG})
%Hike.Either{l_value: :ERROR_MSG, r_value: nil, is_left?: true}

iex> Hike.mayfail(9)
%Hike.MayFail{failure: nil, success: 9, is_success?: true}

iex> Hike.mayfail({:ok, 9})
%Hike.MayFail{failure: nil, success: 9, is_success?: true}

iex> Hike.mayfail({:error, "ERR_MSG"})
%Hike.MayFail{failure: "ERR_MSG", success: nil, is_success?: false}

iex> option = Hike.option(42)
...> Hike.map(option, &(&1 * 2))
%Hike.Option{value: 84}

iex> eth = Hike.either(42)
...> Hike.map_left(eth, &(&1 * 2))
%Hike.Either{l_value: nil, r_value: 42, is_left?: false}
iex> eth = Hike.either(42)
...> Hike.map_right(eth, &(&1 * 2))
%Hike.Either{l_value: nil, r_value: 84, is_left?: false}

iex> may_fail = Hike.mayfail(9)
...> Hike.map_success(may_fail, &(&1 * 2))
%Hike.MayFail{failure: nil, success: 18, is_success?: true}


iex> defmodule HikeTest do
...>   alias Hike
...>   def divide(x, y), do: x / y
...>     def test_divide(x, y) do
...>      Hike.try(&divide/2, x, y)
...>    end
...> end

...> HikeTest.test_divide(4, 2) |> Hike.map_success(fn x -> x + 1 end)
...> HikeTest.test_divide(4, 0) |> Hike.map_success(fn x -> x + 1 end)
%Hike.MayFail{failure: nil, success: 3.0, is_success?: true}
%Hike.MayFail{
failure: "bad argument in arithmetic expression",
success: nil,
is_success?: false
}

Application didn't crash but successfully return error.

like this example all other function can be used from Hike module itself.

Link to this section Summary

Types

t()

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

@type exception() :: :error
@type t() :: any()

generic input type <T>.

@type tArg1() :: any()

generic input type <TArg1>.

@type tArg2() :: any()

generic input type <TArg2>.

@type tArg3() :: any()

generic input type <TArg3>.

@type tArg4() :: any()

generic input type <TArg4>.

@type tr() :: any()

generic return type <TR>.

Link to this section Functions

Link to this function

apply(opt, func)

Link to this function

apply_failure(mayfail, func)

Link to this function

apply_left(eth, func)

Link to this function

apply_right(eth, func)

Link to this function

apply_success(mayfail, func)

Link to this function

bind(opt, func)

Link to this function

bind_failure(mayfail, func)

Link to this function

bind_left(eth, func)

Link to this function

bind_right(eth, func)

Link to this function

bind_success(mayfail, func)

@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())
Link to this function

filter(opt, func)

@spec left(t()) :: Hike.Either.either_left(t())
Link to this function

map_failure(mayfail, func)

Link to this function

map_left(eth, func)

Link to this function

map_right(eth, func)

Link to this function

map_success(mayfail, func)

Link to this function

match(opt, some_func, none_func)

@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())
@spec option() :: Hike.Option.option()
@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())
@spec right(t()) :: Hike.Either.either_right(t())
@spec success(t()) :: Hike.MayFail.mayfail_success(t())
@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.

Link to this function

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}
Link to this function

try(func, arg1, arg2)

@spec try((tArg1(), tArg2() -> tr() | exception()), tArg1(), tArg2()) ::
  Hike.MayFail.mayfail()

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
  }
Link to this function

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.

Link to this function

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.