Meta-statements could allow a programmer to define novell entities and data structures
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 } }