Bidirectional transformations with lenses

Volker Mische

Bidirectional transformations with lenses

Volker Mische (@vmische)

LXJS 2012 Lisbon

eye

Example

This is not rocket science!

Launching rocket

Bidirectional Transformations

Atomic Lenses: Copy Lens

	    var lens = new CopyLens(/\d/);
	     
	     
	     
	  

Atomic Lenses: Copy Lens

	    var lens = new CopyLens(/\d/);
	    lens.get('2')
	     
	     
	  

Atomic Lenses: Copy Lens

	    var lens = new CopyLens(/\d/);
	    lens.get('2').result === '2';
	     
	     
	  

Atomic Lenses: Copy Lens

	    var lens = new CopyLens(/\d/);
	    lens.get('2').result === '2';
	    lens.create('5')
	     
	  

Atomic Lenses: Copy Lens

	    var lens = new CopyLens(/\d/);
	    lens.get('2').result === '2';
	    lens.create('5').result === '5';
	     
	  

Atomic Lenses: Copy Lens

	    var lens = new CopyLens(/\d/);
	    lens.get('2').result === '2';
	    lens.create('5').result === '5';
	    lens.put('5',   )
	  

Atomic Lenses: Copy Lens

	    var lens = new CopyLens(/\d/);
	    lens.get('2').result === '2';
	    lens.create('5').result === '5';
	    lens.put('5', '2')
	  

Atomic Lenses: Copy Lens

	    var lens = new CopyLens(/\d/);
	    lens.get('2').result === '2';
	    lens.create('5').result === '5';
	    lens.put('5', '2').result === '5';
	  

Atomic Lenses: Default Lens

	    var lens = new DefaultLens(/\d/);
	     
	     
	     
	  

Atomic Lenses: Default Lens

	    var lens = new DefaultLens(/\d/, 'js');
	     
	     
	     
	  

Atomic Lenses: Default Lens

	    var lens = new DefaultLens(/\d/, 'js');
	    lens.get('2')
	     
	     
	  

Atomic Lenses: Default Lens

	    var lens = new DefaultLens(/\d/, 'js');
	    lens.get('2').result === 'js';
	     
	     
	  

Atomic Lenses: Default Lens

	    var lens = new DefaultLens(/\d/, 'js');
	    lens.get('2').result === 'js';
	     
	    lens.put('js', '2')
	  

Atomic Lenses: Default Lens

	    var lens = new DefaultLens(/\d/, 'js');
	    lens.get('2').result === 'js';
	     
	    lens.put('js', '2').result === '2';
	  

Atomic Lenses: Default Lens

	    var lens = new DefaultLens(/\d/, 'js');
	    lens.get('2').result === 'js';
	    lens.create('js')
	    lens.put('js', '2').result === '2';
	  

Atomic Lenses: Default Lens

	    var lens = new DefaultLens(/\d/, 'js', '0');
	    lens.get('2').result === 'js';
	    lens.create('js')
	    lens.put('js', '2').result === '2';
	  

Atomic Lenses: Default Lens

	    var lens = new DefaultLens(/\d/, 'js', '0');
	    lens.get('2').result === 'js';
	    lens.create('js').result === '0';
	    lens.put('js', '2').result === '2';
	  

Atomic Lenses: Del and Ins Lens

	    new DelLens(/\d/);
	     
	  

Atomic Lenses: Del and Ins Lens

	    new DelLens(/\d/, '0');
	     
	  

Atomic Lenses: Del and Ins Lens

	    new DelLens(/\d/, '0');
	    new InsLens('abc');
	  

Regular Operators

            // Concatenation
	    new ConcatLens([any-number-of-lenses])
             
             
             
             
          

Regular Operators

            // Concatenation
	    new ConcatLens([any-number-of-lenses])
	    // Logical or
	    new UnionLens([any-number-of-lenses])
             
             
          

Regular Operators

            // Concatenation
	    new ConcatLens([any-number-of-lenses])
	    // Logical or
	    new UnionLens([any-number-of-lenses])
	    // One or more occurences
	    new KleeneLens(single-lens)
          

{"type": "Point", "coordinates": [10, 20]} POINT(10 20)

Example: JSON lens

            // We want to match: "some-key": some-value
         

Example: JSON lens

            // We want to match: "some-key": some-value
            var json_kv = function(key, lens) {
             
             
             
             
             
            };
         

Example: JSON lens

            // We want to match: "some-key": some-value
            var json_kv = function(key, lens) {
                var key = '"' + key + '"';
             
             
             
             
            };
         

Example: JSON lens

            // We want to match: "some-key": some-value
            var json_kv = function(key, lens) {
                var key = '"' + key + '"';
             
                    new DelLens()
             
                ]);
            };
         

Example: JSON lens

            // We want to match: "some-key": some-value
            var json_kv = function(key, lens) {
                var key = '"' + key + '"';
             
                    new DelLens(new RegExp(key + ': '))
             
                ]);
            };
         

Example: JSON lens

            // We want to match: "some-key": some-value
            var json_kv = function(key, lens) {
                var key = '"' + key + '"';
             
                    new DelLens(new RegExp(key + ': '), key + ': ')
             
             
            };
         

Example: JSON lens

            // We want to match: "some-key": some-value
            var json_kv = function(key, lens) {
                var key = '"' + key + '"';
             
                    new DelLens(new RegExp(key + ': '), key + ': ')
                    lens
             
            };
         

Example: JSON lens

            // We want to match: "some-key": some-value
            var json_kv = function(key, lens) {
                var key = '"' + key + '"';
                       new ConcatLens([
                    new DelLens(new RegExp(key + ': '), key + ': '),
                    lens
                ]);
            };
         

Example: JSON lens

            // We want to match: "some-key": some-value
            var json_kv = function(key, lens) {
                var key = '"' + key + '"';
                return new ConcatLens([
                    new DelLens(new RegExp(key + ': '), key + ': '),
                    lens
                ]);
            };
         

{"type": "Point", "coordinates": [10, 20]} POINT(10 20)

XML JSON

Apache CouchDB logo

XSLT for JSON

https://github.com/
vmx/jslens/

Thanks!

Get in touch with me