-
Couldn't load subscription status.
- Fork 8
Elixir 1.1 support #2
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ defmodule Array do | |
| Creates a new, extendible array with initial size zero. | ||
| The default value is the atom nil, not undefined. | ||
| """ | ||
| @spec new() :: t | ||
| @spec new() :: t | ||
| def new() do | ||
| %Array{content: :array.new({:default, nil})} | ||
| end | ||
|
|
@@ -85,7 +85,7 @@ defmodule Array do | |
|
|
||
| @doc """ | ||
| Folds the elements of the array using the given function and initial accumulator value. | ||
| The elements are visited in order from the lowest index to the highest. | ||
| The elements are visited in order from the lowest index to the highest. | ||
|
|
||
| If `fun` is not a function, the call raises `ArgumentError`. | ||
| """ | ||
|
|
@@ -96,7 +96,7 @@ defmodule Array do | |
| @doc """ | ||
| Folds the elements of the array right-to-left using the given function and initial accumulator value. | ||
| The elements are visited in order from the highest index to the lowest. | ||
|
|
||
| If `fun` is not a function, the call raises `ArgumentError`. | ||
| """ | ||
| @spec foldr(t, acc, (index, element, acc -> acc)) :: acc when acc: var | ||
|
|
@@ -155,11 +155,36 @@ defmodule Array do | |
|
|
||
| @doc """ | ||
| Gets the value of entry `idx`. If `idx` is not a nonnegative integer, or if the array has | ||
| fixed size and `idx` is larger than the maximum index, the call raises `ArgumentError`. | ||
| fixed size and `idx` is larger than the maximum index, the call returns :error | ||
| """ | ||
| @spec get(t, index) :: element | ||
| def get(%Array{content: c}, idx), | ||
| do: :array.get(idx, c) | ||
| @spec fetch(t, index) :: element | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This typespec is out of date now I think. |
||
| def fetch(%Array{content: c}, idx) do | ||
| cond do | ||
| :array.size(c) == 0 -> :error | ||
| idx in 0..(:array.size(c) - 1) -> {:ok, :array.get(idx, c)} | ||
| true -> :error | ||
| end | ||
| end | ||
|
|
||
| @doc """ | ||
| Gets the value of entry `idx`. If `idx` is not a nonnegative integer, or if the array has | ||
| fixed size and `idx` is larger than the maximum index it returns `default` or array's default value. | ||
| """ | ||
| @spec get(t, index, any) :: element | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This typespec is also out of date. |
||
| def get(array, idx, default \\ nil) do | ||
| case Array.fetch(array, idx) do | ||
| {:ok, value} -> value | ||
| :error -> default || Array.default(array) | ||
| end | ||
| end | ||
|
|
||
| @doc """ | ||
| Gets and updates the container’s value for the given key, in a single pass. | ||
| """ | ||
| def get_and_update(arr, idx, fun) do | ||
| {get, update} = fun.(Array.get(arr, idx)) | ||
| {get, Array.set(arr, idx, update)} | ||
| end | ||
|
|
||
| @doc """ | ||
| Returns `true` if `arr` appears to be an array, otherwise `false`. | ||
|
|
@@ -184,7 +209,7 @@ defmodule Array do | |
| @doc """ | ||
| Maps the given function onto each element of the array. | ||
| The elements are visited in order from the lowest index to the highest. | ||
|
|
||
| If `fun` is not a function, the call raises `ArgumentError`. | ||
| """ | ||
| @spec map(t, (index, element -> any)) :: t | ||
|
|
@@ -260,7 +285,7 @@ defmodule Array do | |
| Folds the elements of the array right-to-left using the given function and initial accumulator value, | ||
| skipping default-valued entries. | ||
| The elements are visited in order from the highest index to the lowest. | ||
|
|
||
| If `fun` is not a function, the call raises `ArgumentError`. | ||
| """ | ||
| @spec sparse_foldr(t, acc, (index, element, acc -> acc)) :: acc when acc: var | ||
|
|
@@ -270,7 +295,7 @@ defmodule Array do | |
| @doc """ | ||
| Maps the given function onto each element of the array, skipping default-valued entries. | ||
| The elements are visited in order from the lowest index to the highest. | ||
|
|
||
| If `fun` is not a function, the call raises `ArgumentError`. | ||
| """ | ||
| @spec sparse_map(t, (element -> any)) :: t | ||
|
|
@@ -322,17 +347,6 @@ defmodule Array do | |
| do: :array.to_orddict(c) | ||
| end | ||
|
|
||
| defimpl Access, for: Array do | ||
| def get(arr, idx) do | ||
| Array.get(arr, idx) | ||
| end | ||
|
|
||
| def get_and_update(arr, idx, fun) do | ||
| {get, update} = fun.(Array.get(arr, idx)) | ||
| {get, Array.set(arr, idx, update)} | ||
| end | ||
| end | ||
|
|
||
| defimpl Enumerable, for: Array do | ||
| def count(arr), do: {:ok, Array.size(arr)} | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the use of error tuples now.