Skip to content

Commit b452a65

Browse files
committed
Add more enum functions. Add a way to turn a js iterator to a reducer
1 parent 2c90dea commit b452a65

3 files changed

Lines changed: 50 additions & 16 deletions

File tree

priv/std_lib/enum.ex

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ defmodule ElixirScript.Enum do
3838
end)
3939

4040
case result do
41-
{:cont, _} ->
41+
{:done, _} ->
4242
default
43-
{:halt, item} ->
43+
{:halted, item} ->
4444
item
4545
end
4646
end
@@ -125,9 +125,9 @@ defmodule ElixirScript.Enum do
125125
end)
126126

127127
case result do
128-
{:cont, _} ->
128+
{:done, _} ->
129129
:error
130-
{:halt, item} ->
130+
{:halted, item} ->
131131
item
132132
end
133133
end
@@ -173,6 +173,20 @@ defmodule ElixirScript.Enum do
173173
result
174174
end
175175

176+
def into(enumerable, collectable) do
177+
{init, fun} = Collectable.into(collectable)
178+
reduce(enumerable, init, fn x, acc ->
179+
fun.(acc, {:cont, x})
180+
end)
181+
end
182+
183+
def into(enumerable, collectable, transform) do
184+
{init, fun} = Collectable.into(collectable)
185+
reduce(enumerable, init, fn x, acc ->
186+
fun.(acc, {:cont, transform.(x)})
187+
end)
188+
end
189+
176190
def member?(enumerable, value) do
177191
{_, result} = Enumerable.reduce(enumerable, {:cont, false}, fn(item, acc) ->
178192
if item == value do

priv/std_lib/enumerable.ex

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,33 @@ end
77

88
defimpl ElixirScript.Enumerable, for: List do
99
def count(list),
10-
do: {:ok, length(list) }
10+
do: {:ok, list.length }
1111

1212
def member?(list, value),
1313
do: {:ok, value in list }
1414

15-
def reduce(_, {:halt, acc}, _fun), do: {:halted, acc}
16-
def reduce(list, {:suspend, acc}, fun), do: {:suspended, acc, &reduce(list, &1, fun)}
17-
def reduce([], {:cont, acc}, _fun), do: {:done, acc}
18-
def reduce([h | t], {:cont, acc}, fun), do: reduce(t, fun.(h, acc), fun)
15+
def reduce(list, acc, fun), do
16+
Bootstrap.Core.Functions.iterator_to_reducer(list, acc, fun)
17+
end
1918
end
2019

2120
defimpl ElixirScript.Enumerable, for: Map do
2221
def count(map) do
23-
{:ok, map_size(map)}
22+
{:ok, map.length}
2423
end
2524

2625
def member?(map, {key, value}) do
27-
{:ok, match?(^value, Map.get(map, key))}
26+
{:ok, Map.get(map, key) == value }
2827
end
2928

3029
def member?(_, _) do
3130
{:ok, false}
3231
end
3332

3433
def reduce(map, acc, fun) do
34+
map
35+
|> Map.to_list
36+
|> Bootstrap.Core.Functions.iterator_to_reducer(acc, fun)
3537
do_reduce(Map.to_list(map), acc, fun)
3638
end
37-
38-
defp do_reduce(_, {:halt, acc}, _fun), do: {:halted, acc}
39-
defp do_reduce(list, {:suspend, acc}, fun), do: {:suspended, acc, &do_reduce(list, &1, fun)}
40-
defp do_reduce([], {:cont, acc}, _fun), do: {:done, acc}
41-
defp do_reduce([h | t], {:cont, acc}, fun), do: do_reduce(t, fun.(h, acc), fun)
4239
end

src/javascript/lib/core/functions.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11
import Protocol from './protocol';
2+
import Core from '../core';
3+
4+
function iterator_to_reducer(iterable, acc, fun) {
5+
const iterator = iterable[Symbol.iterator]();
6+
let x = iterator.next();
7+
let _acc = acc;
8+
9+
while (x.done === false) {
10+
_acc = fun(x.value, _acc.get(1));
11+
if (_acc.get(0) === Symbol.for('halt')) {
12+
return new Core.Tuple(Symbol.for('halted'), _acc.get(1));
13+
} else if (_acc.get(0) === Symbol.for('suspend')) {
14+
return new Core.Tuple(Symbol.for('suspended'), _acc.get(1), new_acc => {
15+
return iterator_to_reducer(iterator, new_acc, fun);
16+
});
17+
}
18+
19+
x = iterator.next();
20+
}
21+
22+
return new Core.Tuple(Symbol.for('done'), _acc.get(1));
23+
}
224

325
function call_property(item, property) {
426
let prop = null;
@@ -117,4 +139,5 @@ export default {
117139
defprotocol,
118140
defimpl,
119141
build_namespace,
142+
iterator_to_reducer,
120143
};

0 commit comments

Comments
 (0)