Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions lib/array.ex
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,15 @@ defimpl Enumerable, for: Array do
def reduce(%Array{content: c}, acc, fun) do
Enumerable.reduce(:array.to_list(c), acc, fun)
end

def slice(arr) do
slice_fun = fn (start, length) ->
for i <- start..(start + length - 1) do
Array.get(arr, i)
end
end
{:ok, Array.size(arr), slice_fun}
end
end

defimpl Collectable, for: Array do
Expand Down
16 changes: 2 additions & 14 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,14 @@ defmodule Array.Mixfile do
version: "1.0.1",
elixir: ">= 1.0.0",
description: "An elixir wrapper library for Erlang's array.",
package: package,
deps: deps]
package: package(),
deps: deps()]
end

# Configuration for the OTP application
#
# Type `mix help compile.app` for more information
def application do
[applications: [:logger]]
end

# Dependencies can be Hex packages:
#
# {:mydep, "~> 0.3.0"}
#
# Or git/path repositories:
#
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
#
# Type `mix help deps` for more examples and options
defp deps do
[{:earmark, ">= 0.0.0", only: :dev},
{:ex_doc, "~> 0.6", only: :dev}]
Expand Down
12 changes: 10 additions & 2 deletions test/array_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ defmodule ArrayTest do
test "fix" do
a = Array.new()
a = Array.set(a, 100, 0)

a = Array.fix(a)
assert_raise ArgumentError, fn ->
Array.set(a, 101, 0)
Expand Down Expand Up @@ -405,7 +405,7 @@ defmodule ArrayTest do
test "to_erlang_array" do
a = Array.from_list([1,2,3])
ea = Array.to_erlang_array(a)

assert :array.is_array(ea)
assert 3 == :array.size(ea)
assert 1 == :array.get(0, ea)
Expand Down Expand Up @@ -454,6 +454,14 @@ defmodule ArrayTest do
assert 6 == sum
end

test "Enumerable.slice" do
slice = Enum.slice(Array.from_list([1,2,3,4,5]), 1, 2)
assert [2,3] == slice

slice = Enum.slice(Array.from_list([1,2,3,4,5]), 3, 1)
assert [4] == slice
end

test "Collectable.into" do
a = Enum.into([1,2,3], Array.new())
assert Array.is_array(a)
Expand Down