From e3d6d1660ac0505b0649d1dbf854083f07b5f2f8 Mon Sep 17 00:00:00 2001 From: David Ellis Date: Tue, 10 Dec 2024 12:22:58 -0600 Subject: [PATCH] Guarantee that map executes in order in JS (#1002) --- alan/src/std/root.ln | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/alan/src/std/root.ln b/alan/src/std/root.ln index 6a880d308..1314a05d4 100644 --- a/alan/src/std/root.ln +++ b/alan/src/std/root.ln @@ -1217,9 +1217,9 @@ fn push{T} (a: Mut{T[]}, v: T) = {Method{"push"} :: (Mut{T[]}, Own{T})}(a, v); fn{Rs} pop{T} (a: Mut{T[]}) -> T? = {Method{"pop"} :: Mut{T[]} -> T?}(a); fn{Js} pop{T} (a: Mut{T[]}) -> T? = {"((a) => a || null)" :: T? -> T?}({Method{"pop"} :: Mut{T[]} -> T?}(a)); fn{Rs} map{T, U} "alan_std::map_onearg" <- RootBacking :: (T[], T -> U) -> U[]; -fn{Js} map{T, U} (a: T[], f: T -> U) = {"Promise.all" :: U[] -> U[]}({Method{"map"} :: (T[], T -> U) -> U[]}(a, f)); +fn{Js} map{T, U} "(async (a, f) => { let out = []; for (let v of a) { out.push(await f(v)); } return out; })" :: (T[], f: T -> U) -> U[]; fn{Rs} map{T, U} "alan_std::map_twoarg" <- RootBacking :: (T[], (T, i64) -> U) -> U[]; -fn{Js} map{T, U} (a: T[], f: (T, i64) -> U) = {"Promise.all" :: U[] -> U[]}({Method{"map"} :: (T[], (T, i32) -> U) -> U[]}(a, fn (v: T, i: i32) = f(v, i.i64))); +fn{Js} map{T, U} "(async (a, f) => { let out = []; for (let i = 0; i < a.length; i++) { out.push(await f(a[i], new alan_std.I64(i))); } return out; })" <- RootBacking :: (T[], f: (T, i64) -> U) -> U[]; fn{Rs} parmap{T, U} "alan_std::parmap_onearg" <- RootBacking :: (T[], T -> U) -> U[]; fn{Rs} filter{T} "alan_std::filter_onearg" <- RootBacking :: (T[], T -> bool) -> T[]; fn{Js} filter{T} (a: T[], f: T -> bool) = {"(async (a, f) => { let out = []; for (let v of a) { if ((await f(v)).val) { out.push(v); } } return out; })" :: (T[], T -> bool) -> T[]}(a, f);