From 453d3512f1a3b244e3c4121b2a81a2b04cd77a39 Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Wed, 13 Jun 2018 23:37:40 +0200 Subject: [PATCH 1/2] Allow Algae.Maybe.new to return a Nothing based on option --- lib/algae/maybe.ex | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/algae/maybe.ex b/lib/algae/maybe.ex index 89b83a9..d8b8a3c 100644 --- a/lib/algae/maybe.ex +++ b/lib/algae/maybe.ex @@ -54,9 +54,24 @@ defmodule Algae.Maybe do iex> new(9) %Algae.Maybe.Just{just: 9} + iex> new(nil) + %Algae.Maybe.Just{just: nil} + + iex> new(nil, nothing: nil) + %Algae.Maybe.Nothing{} + + iex> new(9, nothing: 9) + %Algae.Maybe.Nothing{} + + iex> new(9, nothing: 1) + %Algae.Maybe.Just{just: 9} + """ - @spec new(any) :: Just.t() - defdelegate new(value), to: Just, as: :new + @spec new(any(), [nothing: any()]) :: Just.t() | Nothing.t() + def new(nothing_value, [nothing: nothing_value]), do: Nothing.new() + def new(value, _), do: Just.new(value) + + def new(value), do: Just.new(value) @doc """ Extract a value from a `Maybe`, falling back to a set value in the `Nothing` case. From e0335356f722964ec5cd15176e6036f083b7425a Mon Sep 17 00:00:00 2001 From: Steven Vandevelde Date: Wed, 27 Jun 2018 23:18:24 +0200 Subject: [PATCH 2/2] Add Maybe.from_nillable --- lib/algae/maybe.ex | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/algae/maybe.ex b/lib/algae/maybe.ex index d8b8a3c..32c8b44 100644 --- a/lib/algae/maybe.ex +++ b/lib/algae/maybe.ex @@ -71,8 +71,24 @@ defmodule Algae.Maybe do def new(nothing_value, [nothing: nothing_value]), do: Nothing.new() def new(value, _), do: Just.new(value) + @spec new(any()) :: Just.t() def new(value), do: Just.new(value) + @doc """ + Alias for `new(value, nothing: nil)`. + + ## Examples + + iex> from_nillable(9) + %Algae.Maybe.Just{just: 9} + + iex> from_nillable(nil) + %Algae.Maybe.Nothing{} + + """ + @spec from_nillable(any()) :: Just.t() + def from_nillable(value), do: new(value, nothing: nil) + @doc """ Extract a value from a `Maybe`, falling back to a set value in the `Nothing` case.