Skip to content

Commit 2a4808f

Browse files
committed
Add more lists functions
1 parent e009885 commit 2a4808f

File tree

1 file changed

+80
-0
lines changed
  • src/javascript/lib/core/erlang_compat

1 file changed

+80
-0
lines changed

src/javascript/lib/core/erlang_compat/lists.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,83 @@ function mapfoldl(fun, acc0, list1) {
143143
return new ErlangTypes.Tuple(listResult, accResult);
144144
}
145145

146+
function concat(things) {
147+
return things.map(v => v.toString()).join();
148+
}
149+
146150
function map(fun, list) {
147151
return list.map(value => fun(value));
148152
}
149153

154+
function filter(pred, list1) {
155+
return list1.filter(x => pred(x));
156+
}
157+
158+
function filtermap(fun, list1) {
159+
const list2 = [];
160+
161+
for (const item of list1) {
162+
const value = fun(item);
163+
164+
if (value === true) {
165+
list2.push(item);
166+
} else if (value instanceof ErlangTypes.Tuple && value.get(0) === true) {
167+
list2.push(value.get(1));
168+
}
169+
}
170+
171+
return list2;
172+
}
173+
174+
function member(elem, list) {
175+
for (const item of list) {
176+
if (item === elem) {
177+
return true;
178+
}
179+
}
180+
181+
return false;
182+
}
183+
184+
function all(pred, list) {
185+
for (const item of list) {
186+
if (pred(item) === false) {
187+
return false;
188+
}
189+
}
190+
191+
return true;
192+
}
193+
194+
function any(pred, list) {
195+
for (const item of list) {
196+
if (pred(item) === true) {
197+
return true;
198+
}
199+
}
200+
201+
return false;
202+
}
203+
204+
function splitwith(pred, list) {
205+
let switchToList2 = false;
206+
const list1 = [];
207+
const list2 = [];
208+
209+
for (const item of list) {
210+
if (switchToList2 === true) {
211+
list2.push(item);
212+
} else if (pred(item) === true) {
213+
list1.push(item);
214+
} else {
215+
switchToList2 = true;
216+
list2.push(item);
217+
}
218+
}
219+
220+
return new ErlangTypes.Tuple(list1, list2);
221+
}
222+
150223
export default {
151224
reverse,
152225
foreach,
@@ -162,5 +235,12 @@ export default {
162235
keystore,
163236
keytake,
164237
mapfoldl,
238+
concat,
165239
map,
240+
filter,
241+
filtermap,
242+
member,
243+
all,
244+
any,
245+
splitwith,
166246
};

0 commit comments

Comments
 (0)