From 29b6806b2f3ba7ff1e9d26e8a1c3fff94f483a13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20M=C3=B6nig?= Date: Wed, 12 Jul 2023 16:02:48 +0200 Subject: [PATCH] made "distribution" list selector case-sensitivity setting aware --- HISTORY.md | 2 ++ src/threads.js | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 44e35efb27..dd560a776f 100755 --- a/HISTORY.md +++ b/HISTORY.md @@ -37,6 +37,7 @@ * changed the semantics of empty list-type slots to return a new empty list instead of "nothing" * improved browser resource management for graphics and sounds * changed long-form input dialog setting's behavior to automatically expand / collapse slot type view + * made "distribution" list selector case-sensitivity setting aware and optimized for atomic data * slightly optimized pen drawing performance when WARPed * removed multibranched conditional library (cases.xml), because IF is now variadic * updated audio comp library with a faster (hyper) version of the "Hz for secs" reporter @@ -82,6 +83,7 @@ * v9-rc7 * blocks: replaced the variadic black "ladder" symbol with a white vertical ellipsis label * blocks: changed the semantics of empty list-type slots to return a new empty list instead of "nothing" +* threads: made "distribution" list selector case-sensitivity setting aware ### 2023-07-11 * blocks, symbols, gui: design overhaul of variadic input slots diff --git a/src/threads.js b/src/threads.js index f738b9b5df..98b88101ee 100644 --- a/src/threads.js +++ b/src/threads.js @@ -2280,7 +2280,18 @@ Process.prototype.reportListAttribute = function (choice, list) { return this.reportUniqueValues(list); case 'distribution': this.assertType(list, 'list'); - return list.distribution(); + if (list.canBeCSV()) { + if (Process.prototype.isCaseInsensitive) { + return list.map(row => row instanceof List ? + row.map(cell => + cell.toLowerCase() + ) + : row.toLowerCase() + ).distribution(); + } + return list.distribution(); + } + return this.reportDistribution(list); case 'sorted': this.assertType(list, 'list'); return this.reportSorted(list); @@ -2374,6 +2385,39 @@ Process.prototype.reportUniqueValues = function (list) { this.pushContext(); }; +Process.prototype.reportDistribution = function (list) { + // answer a new list with an entry for each unique value and the + // number of its occurrences in the source list, + // interpolated so it can be interrupted by the user + // because snapEquals() can be a lot slower than identity comparison + var next, record; + if (this.context.accumulator === null) { + this.assertType(list, 'list'); + this.context.accumulator = { + idx : 0, + target : [] + }; + } + if (this.context.accumulator.idx === list.length()) { + this.returnValueToParentContext( + new List(this.context.accumulator.target.map(row => new List(row))) + ); + return; + } + this.context.accumulator.idx += 1; + next = list.at(this.context.accumulator.idx); + + record = this.context.accumulator.target.find(row => + snapEquals(row[0], next) + ); + if (record !== undefined) { + record[1] += 1; + } else { + this.context.accumulator.target.push([next, 1]); + } + this.pushContext(); +}; + Process.prototype.reportSorted = function (data) { return new List(data.itemsArray().slice().sort((a, b) => this.reportIsBefore(a, b) ? - 1 : 1