# The inclusion of Meta-statements ?

**URL:** <https://es.discourse.group/t/the-inclusion-of-meta-statements/388>\
**Category:** 💡 Ideas\
**Created:** [July 6, 2020, 9:17am UTC](https://es.discourse.group/t/the-inclusion-of-meta-statements/388 "2020-07-06T09:17:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seagat2011](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/seagat2011/32/367_2.png) [@Seagat2011](https://es.discourse.group/u/Seagat2011)\
**Post date:** [July 6, 2020, 9:17am UTC](https://es.discourse.group/t/the-inclusion-of-meta-statements/388/1 "2020-07-06T09:17:02Z")

</div>

Meta-statements could allow a programmer to define novell entities and data structures

```javascript
let val = MetaStatement{ 'az', [pattern => callback [, ...] ] }

FLAGS

'a' - prefix `++`a
'z' - postfix (default) a`[]`
'ez' - infix (or any) `++`a`[]`

EXAMPLE

class DS() extends Array {
    static get [Symbol.species] () {
        return Array
    }
    // Methods
    add( n ) { // n == Array of values
        while ( n.length ) {
            this.push( n.shift() )
        }
    }
    addAt( n ) {
        while ( n.length ) {
            this[n[ 0][0] ] = n[0][1]
            n.shift()
        }
    }
    sub( n ) {
        return this.pop( n )
    }
    show {
        return `[${ this.join(', ') }]`
    }
    showAt( n ) {
        return this[n]
    }
    spread() {
        return ...this
    }
}

let ds = new DS()

let `[]` = MetaStatement {
    `z`,
    `[$]` => ds.showAt,
    `[$0] = $1` => ds.addAt,
    `.push( $,... )` => ds.add,
    `.pop()` => ds.sub,
    `.length` => ds.length,
    `` => ds.show,
    `[]...` => ds.spread,
}

let a`` = `[]`
a`[0] = 2` // [2]
a`.push( 4 )` // [2,4]
a`[0]` // 2
a`` // [2,4]
a`[]...` // 2,4
a`.length` // 2
a`.pop()` // 4
a`[]...` // 2
a`.length` // 1

// let b`` = `{{ }}`, where {{ }} is a JSON-like object which allows cycles
let b`` = `{{ a:0, b:1, c:2, d:2 }}` // { a:0, b:1, c:2, d:2 }
b`[]: = {{ e:3, f:4, g:5 }} ` // { a, b, c, d } : { e:3, f:4, g:5 } == { a:{ e:3, f:4, g:5 }, b:{ e:3, f:4, g:5 }, c:{ e:3, f:4, g:5 }, d:{ e:3, f:4, g:5 } }
let mykey = 'a'
let myval = 'g'
b`[${ mykey }][g] = ${ myval }` // { a:{ e:3, f:4, g:g }, b:{ e:3, f:4, g:5 }, c:{ e:3, f:4, g:5 }, d:{ e:3, f:4, g:5 } }

```

---

<div class="post-metadata">

**Author:** ![Seagat2011](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/seagat2011/32/367_2.png) [@Seagat2011](https://es.discourse.group/u/Seagat2011)\
**Post date:** [July 6, 2020, 12:42pm UTC](https://es.discourse.group/t/the-inclusion-of-meta-statements/388/2 "2020-07-06T12:42:42Z")

</div>

Perhaps this is a cleaner notation

```javascript
let {`a`} = `[]`
{`a[0] = 2`} // [2]
{`a.push( 4 )`} // [2,4]
{`a[0]`} // 2
{`a`} // [2,4]
{`a[]...`} // 2,4
{`a.length`} // 2
{`a.pop()`} // 4
{`a[]...`} // 2
{`a.length`} // 1

// let {`b`} = `{{ }}`, where {{ }} is a JSON-like object which allows cycles
let {`b`} = `{{ a:0, b:1, c:2, d:2 }}` // { a:0, b:1, c:2, d:2 }
{`b[]: = {{ e:3, f:4, g:5 }}`} // { a, b, c, d } : { e:3, f:4, g:5 } == { a:{ e:3, f:4, g:5 }, b:{ e:3, f:4, g:5 }, c:{ e:3, f:4, g:5 }, d:{ e:3, f:4, g:5 } }
let mykey = 'a'
let myval = 'g'
{`b[${ mykey }][g] = ${ myval }`} // { a:{ e:3, f:4, g:g }, b:{ e:3, f:4, g:5 }, c:{ e:3, f:4, g:5 }, d:{ e:3, f:4, g:5 } }

```

---

<div class="post-metadata">

**Author:** ![Seagat2011](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/seagat2011/32/367_2.png) [@Seagat2011](https://es.discourse.group/u/Seagat2011)\
**Post date:** [July 6, 2020, 1:48pm UTC](https://es.discourse.group/t/the-inclusion-of-meta-statements/388/3 "2020-07-06T13:48:06Z")

</div>

EXAMPLES

A small virtual-machine to execute python-like statements

```javascript
val = 128
let {`py`} = `#python`
{`py.print('Hello, world!')`}
{`py.scope {`}
{`n = ${val}`}
{`fact = 1`}
{`i = 2`}
{`while i <= n:`}
{` fact *= i`}
{` i += 1`}
{`} py.scope`}
{`py.print(fact)`}

```

And quick test for set-matching by mapping set entries to primes

```javascript
class DS extends Array {
    static get [Symbol.species] () {
        return Array
    }
    // Methods
    #primes = [2,3,5,7]
    #primeIDX = 0
    setCompare( n,m ) {
        let lhs = 1
        let rhs = 1
        let cache = {}
        while ( n.length || m.length ) {
            if( n.length ) {
                let c = 1
                if(n[0] in cache) {
                    c = cache[n[0] ]
                } else {
                    c = this.#primes[this.#primeIDX++]
                    cache[n[0] ] = c
                }
                lhs *= c
                n.shift()
            }
            if( m.length ) {
                let d = 1
                if(m[0] in cache) {
                    d = cache[m[0] ]
                } else {
                    d = this.#primes[this.#primeIDX++]
                    cache[m[0] ] = c
                }
                rhs *= d
                m.shift()
            }
        }
        return (lhs > 1) && (rhs > 1) && (rhs%lhs == 0)
    }
}

let ds = new DS()

let `()` = MetaStatement {
    `z`,
    `( $,... ) in ( $,... )` => ds.setCompare,
}
let {`r`} = `()`
let {`s`} = `()`
{`r( 2,7,9 ) in s( 2,7,12,9 )`} // true

```
