Skip to content

Commit

Permalink
Use Arrays.Protocol.empty in implementation of other functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Qqwy committed Sep 6, 2021
1 parent deea649 commit 5993c42
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/arrays.ex
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ contents = quote do
)

options = Keyword.delete(options, :implementation)
impl_module.empty(options)
Module.concat(Arrays.Protocol, impl_module).empty(options)
end

defp default_array_implementation() do
Expand Down
8 changes: 1 addition & 7 deletions lib/arrays/implementations/erlang_array.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ defmodule Arrays.Implementations.ErlangArray do

defstruct contents: :array.new([default: nil])

@impl Arrays.Behaviour
def empty(options) do
contents = :array.new([default: nil] ++ options)
%ErlangArray{contents: contents}
end

@doc """
Create an `%ErlangArray{}`-struct from an `:array`-record.
Expand Down Expand Up @@ -276,7 +270,7 @@ defmodule Arrays.Implementations.ErlangArray do

@impl true
def slice(array = %ErlangArray{}, start, amount) do
@for.build_slice(array, start, amount, @for.empty(default: :array.default(array.contents)))
@for.build_slice(array, start, amount, empty(default: :array.default(array.contents)))
end

@impl true
Expand Down
24 changes: 9 additions & 15 deletions lib/arrays/implementations/map_array.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@ defmodule Arrays.Implementations.MapArray do

defstruct contents: %{}, default: nil

@impl Arrays.Behaviour
def empty(options) do
default = Keyword.get(options, :default, nil)
size = Keyword.get(options, :size, 0)
%MapArray{contents: construct(default, size), default: default}
end

defp construct(_default, 0), do: %{}

defp construct(default, size) do
Enum.into(0..(size - 1), %{}, &{&1, default})
end

if Code.ensure_loaded?(FunLand.Mappable) do
Module.eval_quoted(__MODULE__,
Expand Down Expand Up @@ -209,7 +197,7 @@ defmodule Arrays.Implementations.MapArray do

@impl true
def resize(%MapArray{default: default}, 0) do
MapArray.empty(default: default)
empty(default: default)
end

def resize(array = %MapArray{contents: contents}, size) when map_size(contents) == size do
Expand Down Expand Up @@ -250,14 +238,20 @@ defmodule Arrays.Implementations.MapArray do

@impl true
def slice(array = %MapArray{}, start, amount) do
@for.build_slice(array, start, amount, @for.empty(default: array.default))
@for.build_slice(array, start, amount, empty(default: array.default))
end

@impl true
def empty(options) when is_list(options) do
default = Keyword.get(options, :default, nil)
size = Keyword.get(options, :size, 0)
%MapArray{contents: MapArray.construct(default, size), default: default}
%MapArray{contents: construct(default, size), default: default}
end

defp construct(_default, 0), do: %{}

defp construct(default, size) do
Enum.into(0..(size - 1), %{}, &{&1, default})
end
end
end

0 comments on commit 5993c42

Please sign in to comment.