diff --git a/add.ce b/add.ce index 074cbda1..350fe4c6 100644 --- a/add.ce +++ b/add.ce @@ -14,8 +14,8 @@ var fd = use('fd') var locator = null var alias = null -for (var i = 0; i < length(args); i++) { - if (args[i] == '--help' || args[i] == '-h') { +array(args, function(arg) { + if (arg == '--help' || arg == '-h') { log.console("Usage: cell add [alias]") log.console("") log.console("Add a dependency to the current package.") @@ -25,14 +25,14 @@ for (var i = 0; i < length(args); i++) { log.console(" cell add gitea.pockle.world/john/cell-image image") log.console(" cell add ../local-package") $stop() - } else if (!starts_with(args[i], '-')) { + } else if (!starts_with(arg, '-')) { if (!locator) { - locator = args[i] + locator = arg } else if (!alias) { - alias = args[i] + alias = arg } } -} +}) if (!locator) { log.console("Usage: cell add [alias]") diff --git a/benchmarks/nota.ce b/benchmarks/nota.ce index 5779b0e9..24e2e7a4 100644 --- a/benchmarks/nota.ce +++ b/benchmarks/nota.ce @@ -43,10 +43,11 @@ for (var i = 0; i < 100; i++) { // Calculate statistics function getStats(arr) { - def avg = reduce(arr, (a,b) => a+b, 0) / length(arr) - def min = min(...arr); - def max = max(...arr); - return { avg, min, max }; + return { + avg: reduce(arr, (a,b) => a+b, 0) / length(arr), + min: reduce(arr, min), + max: reduce(arr, max) + }; } // Pretty print results diff --git a/benchmarks/wota.ce b/benchmarks/wota.ce index b4eb7c9a..d4f0b97c 100644 --- a/benchmarks/wota.ce +++ b/benchmarks/wota.ce @@ -79,18 +79,13 @@ log.console("Wota Encode/Decode Benchmark"); log.console("===================\n"); // We'll run each benchmark scenario in turn. -for (var bench of benchmarks) { - // We'll measure how long it takes to do 'iterations' *for each test value* - // in bench.data. The total loop count is `bench.iterations * bench.data)`. - // Then we compute an overall encode+decode throughput (ops/s). - var totalIterations = bench.iterations * bench.data); +arrfor(benchmarks, function(bench) { + var totalIterations = bench.iterations * length(bench.data); // We'll define a function that does a roundTrip for *each* data item in bench.data // to measure in one loop iteration. Then we multiply by bench.iterations. function runAllData() { - for (var val of bench.data) { - roundTripWota(val); - } + arrfor(bench.data, roundTripWota) } var elapsedSec = measureTime(runAllData, bench.iterations); @@ -100,7 +95,7 @@ for (var bench of benchmarks) { log.console(` Iterations: ${bench.iterations} × ${length(bench.data)} data items = ${totalIterations}`); log.console(` Elapsed: ${elapsedSec.toFixed(3)} s`); log.console(` Throughput: ${opsPerSec} encode+decode ops/sec\n`); -} +}) // All done log.console("Benchmark completed.\n"); diff --git a/benchmarks/wota_nota_json.ce b/benchmarks/wota_nota_json.ce index 4564bd56..c474b221 100644 --- a/benchmarks/wota_nota_json.ce +++ b/benchmarks/wota_nota_json.ce @@ -149,11 +149,7 @@ function runBenchmarkForLibrary(lib, bench) { // 2) Measure DECODING var decodeTime = measureTime(() => { for (var i = 0; i < bench.iterations; i++) { - // decode everything we stored during the first iteration - for (var e of encodedList) { - var decoded = lib.decode(e); - // not verifying correctness here, just measuring speed - } + arrfor(encodedList, lib.decode) } }); diff --git a/build.cm b/build.cm index a73812e6..be6df287 100644 --- a/build.cm +++ b/build.cm @@ -88,9 +88,7 @@ function ensure_dir(path) { for (var i = 0; i < length(parts); i++) { if (parts[i] == '') continue current += parts[i] + '/' - if (!fd.stat(current).isDirectory) { - fd.mkdir(current) - } + if (!fd.stat(current).isDirectory) fd.mkdir(current) } } diff --git a/internal/engine.cm b/internal/engine.cm index 9d5b3980..078355a7 100644 --- a/internal/engine.cm +++ b/internal/engine.cm @@ -177,10 +177,10 @@ function disrupt(err) if (underlings) { var unders = array(underlings) - for (var id of unders) { + arrfor(unders, function(id) { log.console(`calling on ${id} to disrupt too`) $_.stop(create_actor({id})) - } + }) } if (err) { @@ -449,16 +449,14 @@ $_.receiver = function receiver(fn) { receive_fn = fn } -$_.start = function start(cb, program, ...args) { +$_.start = function start(cb, program) { if (!program) return var id = guid() - if (length(args) == 1 && is_array(args[0])) args = args[0] var startup = { id, overling: $_.self, root, - arg: args, program, } greeters[id] = cb @@ -582,14 +580,14 @@ var need_stop = false return } - for (var msg of message_queue) { + arrfor(message_queue, function(msg) { if (msg.startup) { // now is the time to actually spin up the actor actor_mod.createactor(msg.startup) } else { actor_send(msg.actor, msg.send) } - } + }) message_queue = [] } @@ -803,7 +801,7 @@ $_.clock(_ => { var pkg = file_info ? file_info.package : null var use_fn = function(path) { return shop.use(path, pkg) } - // Call with signature: setup_module(args, use, ...capabilities) + // Call with signature: setup_module(args, use, ..capabilities) // The script wrapper builds $_ from the injected capabilities for backward compatibility var val = call(locator.symbol, null, _cell.args.arg, use_fn, ...vals) diff --git a/internal/shop.cm b/internal/shop.cm index 9d741f37..0d3af9b6 100644 --- a/internal/shop.cm +++ b/internal/shop.cm @@ -360,11 +360,6 @@ var open_dls = {} // These map to $_ properties in engine.cm var SHOP_DEFAULT_INJECT = ['$self', '$overling', '$clock', '$delay', '$start', '$receiver', '$contact', '$portal', '$time_limit', '$couple', '$stop', '$unneeded', '$connection', '$fd'] -function strip_dollar(name) { - if (name && name[0] == '$') return text(name, 1) - return name -} - // Decide what a given module is allowed to see. // This is the capability gate - tweak as needed. Shop.script_inject_for = function(file_info) { @@ -387,13 +382,11 @@ function inject_params(inject) { } function inject_values(inject) { - var vals = [] - for (var i = 0; i < length(inject); i++) { - var key = strip_dollar(inject[i]) - if (key == 'fd') vals.push(fd) - else vals.push(my$_[key]) - } - return vals + return array(inject, function(inj) { + var key = trim(inj, '$') + if (key == 'fd') return fd + return my$_[key] + }) } // Build the use function for a specific package context @@ -802,7 +795,7 @@ function execute_module(info) var pkg = file_info.package var use_fn = make_use_fn(pkg) - // Call with signature: setup_module(args, use, ...capabilities) + // Call with signature: setup_module(args, use, ..capabilities) // args is null for module loading used = call(mod_resolve.symbol, context, null, use_fn, ...vals) } else if (c_resolve.scope < 900) { @@ -1217,8 +1210,9 @@ Shop.build_package_scripts = function(package) var scripts = get_package_scripts(package) var pkg_dir = get_package_abs_dir(package) - for (var script of scripts) + arrfor(scripts, function(script) { resolve_mod_fn(pkg_dir + '/' + script, package) + }) } Shop.list_packages = function() @@ -1274,12 +1268,12 @@ Shop.audit_packages = function() { var bad = [] - for (var package of packages) { - if (package == 'core') continue - if (fd.is_dir(package)) continue - if (fetch_remote_hash(package)) continue + arrfor(packages, function(package) { + if (package == 'core') return + if (fd.is_dir(package)) return + if (fetch_remote_hash(package)) return bad.push(package) - } + }) return bad } diff --git a/pronto.cm b/pronto.cm index 45160e1e..1c44efa4 100644 --- a/pronto.cm +++ b/pronto.cm @@ -319,45 +319,12 @@ function requestorize(unary) { } } -// objectify(factory_fn) -// Converts a factory that takes arrays to one that takes objects. -function objectify(factory_fn) { - def factory = 'objectify' - if (!is_function(factory_fn)) - throw make_reason(factory, 'Not a factory.', factory_fn) - - return function objectified_factory(object_of_requestors, ...rest) { - if (!is_object(object_of_requestors)) - throw make_reason(factory, 'Expected an object.', object_of_requestors) - - def keys = array(object_of_requestors) - def requestor_array = array(keys, k => object_of_requestors[k]) - - def inner_requestor = factory_fn(requestor_array, ...rest) - - return function(callback, value) { - return inner_requestor(function(results, reason) { - if (results == null) { - callback(null, reason) - } else if (is_array(results)) { - def obj = {} - arrfor(keys, (k, i) => { obj[k] = results[i] }) - callback(obj, reason) - } else { - callback(results, reason) - } - }, value) - } - } -} - return { fallback, parallel, race, sequence, requestorize, - objectify, is_requestor, check_callback } diff --git a/qopconv.ce b/qopconv.ce index d181a98e..b7c6c1d6 100644 --- a/qopconv.ce +++ b/qopconv.ce @@ -3,10 +3,10 @@ var qop = use('qop') function print_usage() { log.console("Usage: qopconv [OPTION...] FILE...") - log.console(" -u ... unpack archive") - log.console(" -l ... list contents of archive") + log.console(" -u .. unpack archive") + log.console(" -l .. list contents of archive") log.console(" -d ....... change read dir when creating archives") - log.console(" ... create archive from sources") + log.console(" .. create archive from sources") } function list(archive_path) { diff --git a/test.ce b/test.ce index 10997763..0e2ac28c 100644 --- a/test.ce +++ b/test.ce @@ -149,9 +149,8 @@ function ensure_dir(path) { for (var i = 0; i < length(parts); i++) { if (parts[i] == '') continue current += parts[i] + '/' - if (!fd.is_dir(current)) { + if (!fd.is_dir(current)) fd.mkdir(current) - } } return true } diff --git a/tests/comments.cm b/tests/comments.cm index c618d52e..a1a61f53 100644 --- a/tests/comments.cm +++ b/tests/comments.cm @@ -30,7 +30,10 @@ return { send(tree, reason) } - send(tree, { ...result.comment, children: result.children, time: time.text() }) + var obj = object(result.comment) + obj.children = result.children + obj.time = time.text() + send(tree, obj) }) }) } diff --git a/tests/comments_actor.ce b/tests/comments_actor.ce index c7b70222..63851f2a 100644 --- a/tests/comments_actor.ce +++ b/tests/comments_actor.ce @@ -25,6 +25,9 @@ $receiver(tree => { send(tree, reason) } - send(tree, { ...result.comment, children: result.children, time: time.text() }) + var obj = object(result.comment) + obj.children = result.children + obj.time = time.text() + send(tree, obj) }) }) diff --git a/tests/hang.cm b/tests/hang.cm index 660af740..a7845da8 100644 --- a/tests/hang.cm +++ b/tests/hang.cm @@ -1,6 +1,6 @@ return { test_hang: function() { - log.console(`Going to start hanging ... (disabled)`) + log.console(`Going to start hanging .. (disabled)`) // while(1) { // // hang! diff --git a/tests/nota.cm b/tests/nota.cm index bb03a0fa..97d3c83f 100644 --- a/tests/nota.cm +++ b/tests/nota.cm @@ -41,7 +41,7 @@ function deepCompare(expected, actual, path) { stone_if_needed(expected); stone_if_needed(actual) if (expected.length != actual.length) return { passed: false, messages: [`blob length mismatch at ${path}: ${length(expected)} vs ${length(actual)}`] } - for (var i = 0; i < expected.length; i++) { + for (var i = 0; i < length(expected); i++) { if (expected.read_logical(i) != actual.read_logical(i)) return { passed: false, messages: [`blob bit mismatch at ${path}[${i}]`] } } @@ -55,12 +55,11 @@ function deepCompare(expected, actual, path) { messages: [`Array length mismatch at ${path}: expected ${length(expected)}, got ${length(actual)}`] }; var messages = []; - for (var i = 0; i < expected.length; i++) { - var result = deepCompare(expected[i], actual[i], `${path}[${i}]`); - if (!result.passed) { - for(var m of result.messages) messages.push(m); - } - } + arrfor(expected, function(val, i) { + var result = deepCompare(val, actual[i], `${path}[${i}]`); + if (!result.passed) + messages = array(messages, result.messages) + }) return { passed: messages.length == 0, messages: messages }; } @@ -73,12 +72,11 @@ function deepCompare(expected, actual, path) { messages: [`Object keys mismatch at ${path}: expected ${expKeys}, got ${actKeys}`] }; var messages = []; - for (var key of expKeys) { + arrfor(expKeys, function(key) { var result = deepCompare(expected[key], actual[key], `${path}.${key}`); - if (!result.passed) { - for(var m of result.messages) messages.push(m); - } - } + if (!result.passed) + messages = array(messages, result.messages) + }) return { passed: messages.length == 0, messages: messages }; } diff --git a/tests/wota.cm b/tests/wota.cm index 8a004f70..b25620b5 100644 --- a/tests/wota.cm +++ b/tests/wota.cm @@ -22,11 +22,11 @@ function deep_compare(expected, actual, path) { if (is_blob(expected) && is_blob(actual)) { stone_if_needed(expected); stone_if_needed(actual) if (length(expected) != length(actual)) - return { passed: false, messages: [`blob length mismatch at ${path}: ${length(expected)} vs ${actual)}`] } - for (var i = 0; i < length(expected); i++) { + return { passed: false, messages: [`blob length mismatch at ${path}: ${length(expected)} vs ${length(actual)}`] } + arrfor(array(expected), function(i) { if (expected.read_logical(i) != actual.read_logical(i)) return { passed: false, messages: [`blob bit mismatch at ${path}[${i}]`] } - } + }) return { passed: true, messages: [] } } @@ -34,12 +34,10 @@ function deep_compare(expected, actual, path) { if (length(expected) != length(actual)) return { passed: false, messages: [`Array length mismatch at ${path}: ${length(expected)} vs ${length(actual)}`] } var msgs = [] - for (var i = 0; i < length(expected); i++) { + arrfor(array(expected), function(i) { var res = deep_compare(expected[i], actual[i], `${path}[${i}]`) - if (!res.passed) { - for(var m of res.messages) msgs.push(m) - } - } + if (!res.passed) array(msgs, res.messages) + }) return { passed: length(msgs) == 0, messages: msgs } } @@ -49,12 +47,10 @@ function deep_compare(expected, actual, path) { if (JSON.stringify(expKeys) != JSON.stringify(actKeys)) return { passed: false, messages: [`Object keys mismatch at ${path}: ${expKeys} vs ${actKeys}`] } var msgs = [] - for (var k of expKeys) { + arrfor(expKeys, function(k) { var res = deep_compare(expected[k], actual[k], `${path}.${k}`) - if (!res.passed) { - for(var m of res.messages) msgs.push(m) - } - } + if (!res.passed) array(msgs, res.messages) + }) return { passed: length(msgs) == 0, messages: msgs } } diff --git a/toml.cm b/toml.cm index 1eb41b8b..099dd12e 100644 --- a/toml.cm +++ b/toml.cm @@ -226,7 +226,7 @@ function encode_toml(obj) { // Direct properties var section_keys = array(value) - for (var j = 0; j < section_length(keys); j++) { + for (var j = 0; j < length(section_keys); j++) { var sk = section_keys[j] var sv = value[sk] if (!is_object(sv)) result.push(quote_key(sk) + ' = ' + encode_value(sv))