Skip to content

Default values #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions lib/array.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ defmodule Array do
@type opt :: {:fixed, boolean} | :fixed | {:default, any} | {:size, non_neg_integer} | non_neg_integer
@type orddict :: [{index, element}]

@doc """
Creates a new, extendible array with initial size zero.
The default value is the atom nil, not undefined.
"""
@spec new() :: t
def new() do
%Array{content: :array.new({:default, nil})}
end

@doc """
Creates a new fixed array according to the given options.
By default, the array is extendible and has initial size zero.
Expand All @@ -38,7 +29,7 @@ defmodule Array do
* Sets the default value for the array to `value`.
"""
@spec new(opts) :: t
def new(options) do
def new(options \\ []) do
if is_list(options) do
%Array{content: :array.new([{:default, nil} | options])}
else
Expand Down Expand Up @@ -103,30 +94,16 @@ defmodule Array do
def foldr(%Array{content: c}, acc, fun),
do: :array.foldr(fun, acc, c)

@doc """
Equivalent to `from_list(list, nil)`.
"""
@spec from_list(list) :: t
def from_list(list),
do: %Array{content: :array.from_list(list, nil)}

@doc """
Converts a list to an extendible array.
`default` is used as the value for uninitialized entries of the array.

If `list` is not a proper list, the call raises `ArgumentError`.
"""
@spec from_list(list, any) :: t
def from_list(list, default),
def from_list(list, default \\ nil),
do: %Array{content: :array.from_list(list, default)}

@doc """
Equivalent to `from_orddict(orddict, nil)`.
"""
@spec from_orddict(orddict) :: t
def from_orddict(orddict),
do: %Array{content: :array.from_orddict(orddict, nil)}

@doc """
Converts an ordered list of pairs `{index, value}` to a corresponding extendible array.
`default` is used as the value for uninitialized entries of the array.
Expand All @@ -135,7 +112,7 @@ defmodule Array do
the call raises `ArgumentError`.
"""
@spec from_orddict(orddict, any) :: t
def from_orddict(orddict, default),
def from_orddict(orddict, default \\ nil),
do: %Array{content: :array.from_orddict(orddict, default)}

@doc """
Expand Down