-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
71 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule Dasie.Maybe do | ||
@moduledoc """ | ||
Represent something empty/missing or not. Also known as Optional in some languages. | ||
This is a pretty contrived thing, but wanted to see how it would look :) | ||
Another take would be to use a single or zero element list. | ||
""" | ||
defstruct value: nil | ||
|
||
@type t :: %__MODULE__{ | ||
value: any | ||
} | ||
|
||
@spec of(value :: any) :: __MODULE__.t() | ||
def of(value), do: %__MODULE__{value: value} | ||
|
||
@spec present?(any) :: boolean | ||
def present?(%__MODULE__{value: nil}), do: false | ||
def present?(nil), do: false | ||
def present?(_), do: true | ||
|
||
@doc """ | ||
If a value is present in this Maybe, returns `{:ok, value}`, otherwise returns `{:error, :no_value}`. | ||
""" | ||
@spec get(maybe :: __MODULE__.t()) :: {:ok, any} | {:error, :no_value} | ||
def get(%__MODULE__{value: nil}), do: {:error, :no_value} | ||
def get(%__MODULE__{value: val}), do: {:ok, val} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
defmodule Dasie.MaybeTest do | ||
use ExUnit.Case | ||
|
||
alias Dasie.Maybe | ||
|
||
describe "present?/1" do | ||
test "maybe with value" do | ||
thing = Maybe.of(:something) | ||
assert Maybe.present?(thing) | ||
end | ||
|
||
test "maybe with no value" do | ||
thing = Maybe.of(nil) | ||
refute Maybe.present?(thing) | ||
end | ||
|
||
test "raw nil" do | ||
refute Maybe.present?(nil) | ||
end | ||
|
||
test "raw value" do | ||
assert Maybe.present?(1) | ||
end | ||
end | ||
|
||
describe "get/1" do | ||
test "maybe with value" do | ||
thing = Maybe.of(:something) | ||
assert Maybe.get(thing) == {:ok, thing.value} | ||
end | ||
|
||
test "maybe with no value" do | ||
thing = Maybe.of(nil) | ||
assert Maybe.get(thing) == {:error, :no_value} | ||
end | ||
end | ||
end |