Scope References in Formulas

Scope Reference in Formulas

Problem

It is not possible to access a value from an outer formula within an inner formula. This is prevalent when you get into some more complex array manipulations that are too dynamic for reference tables to be valuable. (examples in JavaScript using only arrays)

Examples

I believe that both of these can potentially be done in O(n^2) time or worse, but doing that in every row can kill performance pretty quickly.

Array formulas that need to reference themselves

const range = [ 0, 1, 2, 3, 3, 2, 1, 2, 3, 1, 2, 3, 1 ]
const rangeOccurrenceCount = 
  range
    .filter( isFirstOccurrence )
    .map( val => 
      [ 
        val, 
        range.filter( current => current === val )
      ]
    );

“Zipping” non-unique arrays together to form a single array

const a = [ 0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 ];
const b = [ "a", "b", "c", "a", "b", "c" ];
const zipped = (
  a.length >= b.length ?
    a.map( getIndex )
    : b.map (getIndex )
  ).map( index => [ a[index], b[index] ] )