-
Notifications
You must be signed in to change notification settings - Fork 9
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
Open
mhib
wants to merge
4
commits into
takscape:master
Choose a base branch
from
mhib:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -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)} | ||
|
||
|
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.