Skip to content

Commit 8d3e771

Browse files
authored
Merge pull request #1 from jfacorro/missing-slice-implementation-for-Enumerable
Implement missing slice function for Enumerable protocol
2 parents f248f9d + 5547ab6 commit 8d3e771

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

lib/array.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,15 @@ defimpl Enumerable, for: Array do
371371
def reduce(%Array{content: c}, acc, fun) do
372372
Enumerable.reduce(:array.to_list(c), acc, fun)
373373
end
374+
375+
def slice(arr) do
376+
slice_fun = fn (start, length) ->
377+
for i <- start..(start + length - 1) do
378+
Array.get(arr, i)
379+
end
380+
end
381+
{:ok, Array.size(arr), slice_fun}
382+
end
374383
end
375384

376385
defimpl Collectable, for: Array do

mix.exs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,14 @@ defmodule Array.Mixfile do
66
version: "1.0.1",
77
elixir: ">= 1.0.0",
88
description: "An elixir wrapper library for Erlang's array.",
9-
package: package,
10-
deps: deps]
9+
package: package(),
10+
deps: deps()]
1111
end
1212

13-
# Configuration for the OTP application
14-
#
15-
# Type `mix help compile.app` for more information
1613
def application do
1714
[applications: [:logger]]
1815
end
1916

20-
# Dependencies can be Hex packages:
21-
#
22-
# {:mydep, "~> 0.3.0"}
23-
#
24-
# Or git/path repositories:
25-
#
26-
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
27-
#
28-
# Type `mix help deps` for more examples and options
2917
defp deps do
3018
[{:earmark, ">= 0.0.0", only: :dev},
3119
{:ex_doc, "~> 0.6", only: :dev}]

test/array_test.exs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ defmodule ArrayTest do
8787
test "fix" do
8888
a = Array.new()
8989
a = Array.set(a, 100, 0)
90-
90+
9191
a = Array.fix(a)
9292
assert_raise ArgumentError, fn ->
9393
Array.set(a, 101, 0)
@@ -405,7 +405,7 @@ defmodule ArrayTest do
405405
test "to_erlang_array" do
406406
a = Array.from_list([1,2,3])
407407
ea = Array.to_erlang_array(a)
408-
408+
409409
assert :array.is_array(ea)
410410
assert 3 == :array.size(ea)
411411
assert 1 == :array.get(0, ea)
@@ -454,6 +454,14 @@ defmodule ArrayTest do
454454
assert 6 == sum
455455
end
456456

457+
test "Enumerable.slice" do
458+
slice = Enum.slice(Array.from_list([1,2,3,4,5]), 1, 2)
459+
assert [2,3] == slice
460+
461+
slice = Enum.slice(Array.from_list([1,2,3,4,5]), 3, 1)
462+
assert [4] == slice
463+
end
464+
457465
test "Collectable.into" do
458466
a = Enum.into([1,2,3], Array.new())
459467
assert Array.is_array(a)

0 commit comments

Comments
 (0)