Hike.MayFail (hike v0.0.1)

Link to this section Summary

Types

binder() represent a binding(mapping) function which take no argument and return a Mayfail of type <TR>.

binder(t) represent a binding(mapping) function which take an argument of type <T> and return a Mayfail of type <TR>.

func() represent a function which take no argument and return value of type <TR>.

func(t) represent a function which take an argument of type <T> and return a value of type <TR>.

mapper() represent a mapping function which take no argument and return a value of type <TR>.

mapper(t) represent a mapping function which take an argument of type <T> and return a value of type <TR>.

represent a type of Mayfail that could be in either Failure or Success state

represent a type of Mayfail that could be in either failure or success state with a value of given type.

represent a type of Mayfail in Failure state.

represent a type of Mayfail in Failure state.

Elevated data type of Mayfail struct that represents Success state.

Elevated data type of Mayfail struct that represents Success state and have a value of type <T>.

t()

generic input type <T>.

generic input type <T_Failure> represent a type of value on Failurestate.

generic input type <T_Success>represent a type of value on Successstate.

generic return type <TR>.

Functions

%Hike.Mayfail{failure: t_failure(), success: t_success(), is_success?:boolean()} is a struct that represents an "either/or" value. It can contain either a Failure value or a Success value, but not both.

Example

iex> apply_func = fn x -> x + 2 end
iex> MayFail.failure(1) |> MayFail.apply_failure(apply_func)
%Hike.MayFail{failure: 3, success: nil, is_success?: false}

iex> MayFail.success(1) |> MayFail.apply_failure(apply_func)
%Hike.MayFail{failure: nil, success: 1, is_success?: true}

Example

iex> apply_func = fn x -> (x + 2) end
iex> MayFail.failure(1) |> MayFail.apply_success(apply_func)
%Hike.MayFail{failure: 1, success: nil, is_success?: false}

iex> MayFail.success(1) |> MayFail.apply_success(apply_func)
%Hike.MayFail{failure: nil, success: 3, is_success?: true}

Binds a function that returns a MayFail value for an MayFail in the Failure state. If the input is in the Success state, the function is not executed and the input is returned as it is.

Binds a function that returns a MayFail value for an MayFail in the Failure state. If the input is in the Success state, the function is not executed and the input is returned as it is.

Check whether MayFail is in Failure state or not.

Check whether MayFail is in Success state or not.

Example

iex> mapper = fn x -> x + 2 end
iex> MayFail.failure(1) |> MayFail.map_failure(mapper)
%Hike.MayFail{failure: 3, success: nil, is_success?: false}

iex> MayFail.success(1) |> MayFail.map_failure(mapper)
%Hike.MayFail{failure: nil, success: 1, is_success?: true}

Example

iex> mapper = fn x -> (x + 2) end
iex> MayFail.failure(1) |> MayFail.map_success(mapper)
%Hike.MayFail{failure: 1, success: nil, is_success?: false}

iex> MayFail.success(1) |> MayFail.map_success(mapper)
%Hike.MayFail{failure: nil, success: 3, is_success?: true}

Matches an Mayfail value and applies the corresponding function.

Link to this section Types

@type binder() :: (() -> mayfail_success(tr()) | mayfail_failure(tr()))

binder() represent a binding(mapping) function which take no argument and return a Mayfail of type <TR>.

example

Example

iex> success_bind_func = fn () -> Hike.MayFail.success(:ok) end
iex> failure_bind_func = fn () -> Hike.MayFail.failure(:error) end
@type binder(t) :: (t -> mayfail_success(tr()) | mayfail_failure(tr()))

binder(t) represent a binding(mapping) function which take an argument of type <T> and return a Mayfail of type <TR>.

example

Example

iex> success_bind_func = fn (x) -> Mayfail.success(x) end
iex> failure_bind_func = fn (y) -> Mayfail.failure(y) end
@type func() :: (() -> tr())

func() represent a function which take no argument and return value of type <TR>.

@type func(t) :: (t -> tr())

func(t) represent a function which take an argument of type <T> and return a value of type <TR>.

@type mapper() :: (() -> tr())

mapper() represent a mapping function which take no argument and return a value of type <TR>.

@type mapper(t) :: (t -> tr())

mapper(t) represent a mapping function which take an argument of type <T> and return a value of type <TR>.

Link to this opaque

mayfail()

(opaque)
@opaque mayfail()

represent a type of Mayfail that could be in either Failure or Success state

Link to this type

mayfail(t_failure, t_success)

@type mayfail(t_failure, t_success) :: %Hike.MayFail{
  failure: t_failure,
  is_success?: boolean(),
  success: t_success
}

represent a type of Mayfail that could be in either failure or success state with a value of given type.

Link to this type

mayfail_failure()

@type mayfail_failure() :: %Hike.MayFail{
  failure: t_failure(),
  is_success?: false,
  success: nil
}

represent a type of Mayfail in Failure state.

Link to this type

mayfail_failure(t)

@type mayfail_failure(t) :: %Hike.MayFail{
  failure: t,
  is_success?: false,
  success: nil
}

represent a type of Mayfail in Failure state.

Link to this type

mayfail_success()

@type mayfail_success() :: %Hike.MayFail{
  failure: nil,
  is_success?: true,
  success: t_success()
}

Elevated data type of Mayfail struct that represents Success state.

Link to this type

mayfail_success(t)

@type mayfail_success(t) :: %Hike.MayFail{failure: nil, is_success?: true, success: t}

Elevated data type of Mayfail struct that represents Success state and have a value of type <T>.

@type t() :: any()

generic input type <T>.

@type t_failure() :: any()

generic input type <T_Failure> represent a type of value on Failurestate.

@type t_success() :: any()

generic input type <T_Success>represent a type of value on Successstate.

@type tr() :: any()

generic return type <TR>.

Link to this section Functions

Link to this function

%Hike.MayFail{}

(struct)

%Hike.Mayfail{failure: t_failure(), success: t_success(), is_success?:boolean()} is a struct that represents an "either/or" value. It can contain either a Failure value or a Success value, but not both.

  • failure: the failure value (if is_success? is false)
  • success: the success value (if is_success? is true)
  • is_success?: a boolean flag indicating whether the value is a success value (true) or a failure value (false)
Link to this function

apply_failure(mayfail, func)

@spec apply_failure(mayfail_failure(t_failure()), func(t_failure())) ::
  mayfail_failure(tr())
@spec apply_failure(mayfail_success(t_success()), func(t_failure())) ::
  mayfail_success(t_success())

example

Example

iex> apply_func = fn x -> x + 2 end
iex> MayFail.failure(1) |> MayFail.apply_failure(apply_func)
%Hike.MayFail{failure: 3, success: nil, is_success?: false}

iex> MayFail.success(1) |> MayFail.apply_failure(apply_func)
%Hike.MayFail{failure: nil, success: 1, is_success?: true}
Link to this function

apply_success(mayfail, func)

@spec apply_success(mayfail_success(t_success()), func(t_success())) ::
  mayfail_success(tr())
@spec apply_success(mayfail_failure(t_failure()), func(t_success())) ::
  mayfail_failure(t_failure())

example

Example

iex> apply_func = fn x -> (x + 2) end
iex> MayFail.failure(1) |> MayFail.apply_success(apply_func)
%Hike.MayFail{failure: 1, success: nil, is_success?: false}

iex> MayFail.success(1) |> MayFail.apply_success(apply_func)
%Hike.MayFail{failure: nil, success: 3, is_success?: true}
Link to this function

bind_failure(mayfail, binder)

Binds a function that returns a MayFail value for an MayFail in the Failure state. If the input is in the Success state, the function is not executed and the input is returned as it is.

example

Example

iex> binder = fn x -> MayFail.success(x + 2) end
iex> MayFail.failure(1) |> MayFail.bind_failure(binder)
%Hike.MayFail{failure: nil, success: 3, is_success?: true}

iex> binder = fn x -> MayFail.failure(x + 2) end
iex>  MayFail.failure(1) |> MayFail.bind_failure(binder)
%Hike.MayFail{failure: 3, success: nil, is_success?: false}

iex> MayFail.success(1) |> MayFail.bind_failure(binder)
%Hike.MayFail{failure: nil, success: 1, is_success?: true}
Link to this function

bind_success(mayfail, binder)

Binds a function that returns a MayFail value for an MayFail in the Failure state. If the input is in the Success state, the function is not executed and the input is returned as it is.

example

Example

iex> binder = fn x -> MayFail.success(x + 2) end
iex> MayFail.failure(1) |> MayFail.bind_success(binder)
%Hike.MayFail{failure: 1, success: nil, is_success?: false}

iex> MayFail.success(1) |> MayFail.bind_success(binder)
%Hike.MayFail{failure: nil, success: 3, is_success?: true}

iex> binder = fn x -> MayFail.failure(x + 2) end
iex>  MayFail.success(1) |> MayFail.bind_success(binder)
%Hike.MayFail{failure: 3, success: nil, is_success?: false}
@spec failure(t_failure()) :: mayfail_failure(t_failure())
Link to this function

is_failure?(mayfail)

@spec is_failure?(mayfail_failure()) :: true
@spec is_failure?(mayfail_success()) :: false

Check whether MayFail is in Failure state or not.

example

Example

iex> Hike.MayFail.success(4) |> Hike.MayFail.is_failure?
false
iex> Hike.MayFail.failure("fail") |> Hike.MayFail.is_failure?
true
Link to this function

is_success?(mayfail)

@spec is_success?(mayfail_success()) :: true
@spec is_success?(mayfail_failure()) :: false

Check whether MayFail is in Success state or not.

example

Example

iex> Hike.MayFail.success(4) |> Hike.MayFail.is_success?
true
iex> Hike.MayFail.failure("fail") |> Hike.MayFail.is_success?
false
Link to this function

map_failure(mayfail, mapper)

example

Example

iex> mapper = fn x -> x + 2 end
iex> MayFail.failure(1) |> MayFail.map_failure(mapper)
%Hike.MayFail{failure: 3, success: nil, is_success?: false}

iex> MayFail.success(1) |> MayFail.map_failure(mapper)
%Hike.MayFail{failure: nil, success: 1, is_success?: true}
Link to this function

map_success(mayfail, mapper)

@spec map_success(mayfail_success(t_success()), mapper()) :: mayfail_success(tr())
@spec map_success(mayfail_failure(t_failure()), mapper(t_success())) ::
  mayfail_failure(t_failure())

example

Example

iex> mapper = fn x -> (x + 2) end
iex> MayFail.failure(1) |> MayFail.map_success(mapper)
%Hike.MayFail{failure: 1, success: nil, is_success?: false}

iex> MayFail.success(1) |> MayFail.map_success(mapper)
%Hike.MayFail{failure: nil, success: 3, is_success?: true}
Link to this function

match(mayfail, failure_fn, success_fn)

@spec match(
  mayfail(t_failure(), t_success()),
  (t_failure() -> tr()),
  (t_success() -> tr())
) :: tr()

Matches an Mayfail value and applies the corresponding function.

example

Example

iex> Hike.MayFail.success(4) |> Hike.MayFail.match(fn x -> x + 3 end, fn y -> y + 2 end)
6
@spec success(t_success()) :: mayfail_success(t_success())