SlideShare a Scribd company logo
Metaprogramming
with JavaScript
Timur Shemsedinov
Research Institute of System Technologies
What is Metaprogramming?
β€’ Templates and macroses to generate source
code in compile-time
β€’ Self-changing programs
β€’ Programs that generate other programs
β€’ other meanings ?
What is Metaprogramming?
1. This is not artificial intelligence.
2. Metaprogramming is not something new,
you've always used it.
3. In programming languages ​​of Von Neumann architecture
nothing happens without metadata (architecture in which
data and instructions are stored in shared memory and
computer needs to recognize numbers, strings, addresses,
executable instruction, etc.)
What is Metaprogramming?
Programming paradigm that implies program
structure and functionality modification
programmatically
How Metaprogramming works?
Programming paradigm that implies program
structure and functionality modification
programmatically
1. When changes occur?
2. What is changing?
3. Whereby changes occur?
How Metaprogramming works?
When changes occurs?
β€’ Design-time
β€’ Compile-time
β€’ Run-time
β€’ Just-in-Time (immediate when executing/processing)
β€’ Lazy (between executing/processing)
β€’ On Timer
β€’ On Pull requests
β€’ On Push notifications
How Metaprogramming works?
What is changing?
β€’ data types and data structures
β€’ identifiers (class names, type names, variable names)
β€’ calls (method/function names, dynamic binding)
β€’ algorithm parameters
β€’ formula syntax, regular expression syntax, etc.
β€’ dynamic code interpretation (eval)
β€’ data serialization/deserialization
How Metaprogramming works?
Whereby changes occurs?
β€’ Parsing and translation of syntactic structures
β€’ Access to objects and functions by identification name
β€’ Full introspection
β€’ First-class objects individuation:
β€’ functions, using closures
β€’ classes, using inheritance (prototypes in js)
β€’ objects, using mixins
The problem definition
Why do we need Metaprogramming?
β€’ To raise functionality, universality and
abstraction level of software solutions
β€’ Dynamic domains when changing functionality
and structure is a normal mode
β€’ Intersystem/inter-module integration,
using introspection and dynamic binding
Example 1: data definition
var names = [
"Marcus Aurelius Antoninus Augustus",
"Darth Vader",
"Victor Michailovich Glushkov",
"Gottfried Wilhelm von Leibniz",
"Mao Zedong",
"Vladimir Sergeevich Soloviov",
"Ibn Arabi",
"Lev Nikolayevich Tolstoy",
"Muammar Muhammad Abu Minyar al-Gaddafi",
"Rene Descartes",
"Fyodor Mikhailovich Dostoyevsky",
"Benedito de Espinosa"
];
Example 1: solution (without metaprogramming)
function filter(names) {
var result = [], name;
for (var i=0; i<names.length; i++) {
name = names[i];
if ( name.length >= 10 && name.length <= 200 &&
name.indexOf("Mich") > -1 &&
name.indexOf("V") === 0 &&
name.slice(-2) == "ov" &&
!( name.length >= 50 && name.length <= 65 &&
name.indexOf("Abu") > -1 &&
name.indexOf("Lev") === 0 &&
name.slice(-3) == "iov")
) result.push(name);
}
return result;
}
Example 1: extracted metadata
var conditions = {
length: [10, 200],
contains: "Mich",
starts: "V",
ends: "ov",
not: {
length: [50, 65],
contains: "Abu",
starts: "Lev",
ends: "iov"
}
};
Example 1: metamodel
function filter(names, conditions) {
var operations = {
length: function(s,v) { return s.length>=v[0] && s.length<=v[1] },
contains: function(s,v) { return s.indexOf(v) > -1 },
starts: function(s,v) { return s.indexOf(v) === 0 },
ends: function(s,v) { return s.slice(-v.length) == v },
not: function(s,v) { return !check(s,v) }
};
function check(s, conditions) {
var valid = true;
for (var key in conditions)
valid &= operations[key](s, conditions[key]);
return valid;
}
return names.filter(function(s) { return check(s, conditions); });
}
Unified model for IS module
Example 2: task definition
var tasks = [
{ interval:5000, get:"http://127.0.0.1/api/method1.json",
expect:"OK", save:"file1.json" },
{ interval:"8s", get:"http://127.0.0.1/api/method2.json",
put:"http://127.0.0.1/api/method4.json", save:"file2.json" },
{ interval:"7s", get:"http://127.0.0.1/api/method3.json",
expect:"Done", post:"http://127.0.0.1/api/method5.json" },
{ interval:"4s", load:"file1.json",
expect:"OK", put:"http://127.0.0.1/api/method6.json" },
{ interval:"9s", load:"file2.json", save:"file1.json",
post:"http://127.0.0.1/api/method7.json" },
{ interval:"3s", load:"file1.json", save:"file3.json" },
];
Example 2: metamodel
function iterate(tasks) {
function closureTask(task) { return function () {
console.dir(task);
var source;
if (task.get) source = request.get(task.get);
if (task.load) source = fs.createReadStream(task.load);
if (task.save) source.pipe(fs.createWriteStream(task.save));
if (task.post) source.pipe(request.post(task.post));
if (task.put) source.pipe(request.put(task.put));
} };
for (var i=0; i<tasks.length; i++)
setInterval(closureTask(tasks[i]), duration(tasks[i].interval));
}
Example 2: metamodel (with internal configuration)
function iterate(tasks) {
var sources = { get: request.get,
load: fs.createReadStream };
var destinations = { save: fs.createWriteStream,
post: request.post,
put: request.put };
function closureTask(task) { return function () {
console.dir(task);
var verb, source, destination;
for (key in sources)
if (task[key]) source = sources[key](task[key]);
for (key in destinations)
if (task[key]) source.pipe(destinations[key](task[key]));
} };
for (var i=0; i<tasks.length; i++)
setInterval(closureTask(tasks[i]), duration(tasks[i].interval));
}
Example 3: interpretation
// Parse duration to seconds, example: duration("1d 10h 7m 13s")
function duration(s) {
var result = 0;
if (typeof(s) == 'string') {
var days = s.match(/(d+)s*d/),
hours = s.match(/(d+)s*h/),
minutes = s.match(/(d+)s*m/),
seconds = s.match(/(d+)s*s/);
if (days) result += parseInt(days[1])*86400;
if (hours) result += parseInt(hours[1])*3600;
if (minutes) result += parseInt(minutes[1])*60;
if (seconds) result += parseInt(seconds[1]);
result = result*1000;
} if (typeof(s) == 'number') result = s;
return result;
}
Example 3: interpretation (configurable)
function duration(s) {
if (typeof(s) == 'number') return s;
var units = {
days: { rx:/(d+)s*d/, mul:86400 },
hours: { rx:/(d+)s*h/, mul:3600 },
minutes: { rx:/(d+)s*m/, mul:60 },
seconds: { rx:/(d+)s*s/, mul:1 }
};
var result = 0, unit, match;
if (typeof(s) == 'string') for (var key in units) {
unit = units[key];
match = s.match(unit.rx);
if (match) result += parseInt(match[1])*unit.mul;
}
return result*1000;
}
Example 4: introspection in metaprogramming
Example 4: introspection
var ds = wcl.AjaxDataSource({
read: { get: "examples/person/read.json" },
insert: { post: "examples/person/insert.json" },
update: { post: "examples/person/update.json" },
delete: { post: "examples/person/delete.json" },
find: { post: "examples/person/find.json" },
metadata: { post: "examples/person/metadata.json" }
});
ds.read({ id:5 }, function(err, data) {
data.phone ="+0123456789";
ds.update(data, function(err) {
console.log('Data saved');
});
});
Example 4: introspection in metaprogramming
var ds = wcl.AjaxDataSource({
introspect: { post: "examples/person/introspect.json" }
});
ds.read({ id:3 }, function(err, data) {
data.phone ="+0123456789";
ds.update(data, function(err) {
console.log('Data saved');
});
});
Example 4: introspection in metaprogramming
var ds = wcl.MemoryDataSource({ data: [
{ id:1, name:"Person 1", phone:"+380501002011",
emails:[ "person1@domain.com" ], age: 25 },
{ id:2, name:"Person 2", phone:"+380501002022",
emails:[ "person2@domain.com", "person2@domain2.com" ],
address: { city: "Kiev", street:"Khreschatit", building: "26" } },
{ id:3, name:"Person 3", phone:"+380501002033",
emails:[ "person3@domain.com" ],
tags: [ {tag:"tag1", color:"red"}, {tag:"tag2", color:"green"} ] },
]});
ds.read({ id:3 }, function(err, data) {
data.phone ="+0123456789";
ds.update(data, function(err) {
console.log('Data saved');
});
});
Metaprogramming techniques
β€’ Task definition style: declarative, using metadata,
imperative and functional elements
β€’ Hashes (associative arrays)
beforehand unknown key: var a = {}; a[key] = value;
β€’ String interpretation, inventing domain-specific syntactic
structures or using universal ones (json, js, regexp ...)
β€’ Mixins: beforehand unknown targer object/class
function mixin(a) { a.fn=function(){ ... } }
β€’ Closures: function individuations
fn = (function(a) { return function() { return a*2 } })(value)
The findings
β€’ Code size: generally decrease a lot but may increases code size
in rare cases (compensated by code readability)
β€’ Speed: slightly decreases but good implementation
may remain approximately the same speed
β€’ Flexibility: solution becomes more abstract/universal
software application scope expands
β€’ Integration: usually much simplified integration
and requires less code changes
β€’ Working pleasure: metaprogramming is interesting
so we have more pleasure and motivation
β€’ Working speed: increasing development time but modification and
support tasks take less time, so total time is less
Metaprogramming
with JavaScript
Github: https://github.com/tshemsedinov/metaprogramming
Article: http://habrahabr.ru/post/227753/
Metaprogramming
with JavaScript
Thank you!
Questions please
Timur Shemsedinov
Research Institute of System Technologies

More Related Content

What's hot (20)

NodeJS
NodeJS
LinkMe Srl
Β 
Shared memory and multithreading in Node.js - Timur Shemsedinov - JSFest'19
Shared memory and multithreading in Node.js - Timur Shemsedinov - JSFest'19
Timur Shemsedinov
Β 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scale
Tom Croucher
Β 
Node.js
Node.js
Jan Dillmann
Β 
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Tom Croucher
Β 
DEF CON 23 - amit ashbel and maty siman - game of hacks
DEF CON 23 - amit ashbel and maty siman - game of hacks
Felipe Prado
Β 
Node js presentation
Node js presentation
martincabrera
Β 
Node.js - Best practices
Node.js - Best practices
Felix GeisendΓΆrfer
Β 
introduction to node.js
introduction to node.js
orkaplan
Β 
Node ppt
Node ppt
Tamil Selvan R S
Β 
The Art of JVM Profiling
The Art of JVM Profiling
Andrei Pangin
Β 
Π˜Π½Π΄Π΅ΠΊΡΠΈΡ€ΡƒΠ΅ΠΌ Π±Π°Π·Ρƒ: ΠΊΠ°ΠΊ Π΄Π΅Π»Π°Ρ‚ΡŒ Ρ…ΠΎΡ€ΠΎΡˆΠΎ ΠΈ Π½Π΅ Π΄Π΅Π»Π°Ρ‚ΡŒ ΠΏΠ»ΠΎΡ…ΠΎ Winter saint p 2021 m...
Π˜Π½Π΄Π΅ΠΊΡΠΈΡ€ΡƒΠ΅ΠΌ Π±Π°Π·Ρƒ: ΠΊΠ°ΠΊ Π΄Π΅Π»Π°Ρ‚ΡŒ Ρ…ΠΎΡ€ΠΎΡˆΠΎ ΠΈ Π½Π΅ Π΄Π΅Π»Π°Ρ‚ΡŒ ΠΏΠ»ΠΎΡ…ΠΎ Winter saint p 2021 m...
АндрСй Новиков
Β 
Node.js - A Quick Tour
Node.js - A Quick Tour
Felix GeisendΓΆrfer
Β 
03 standard class library
03 standard class library
eleksdev
Β 
Building a real life application in node js
Building a real life application in node js
fakedarren
Β 
Multithreading in Node.js and JavaScript
Multithreading in Node.js and JavaScript
Timur Shemsedinov
Β 
MongoDB + node.js γ§δ½œγ‚‹γ‚½γƒΌγ‚·γƒ£γƒ«γ‚²γƒΌγƒ 
MongoDB + node.js γ§δ½œγ‚‹γ‚½γƒΌγ‚·γƒ£γƒ«γ‚²γƒΌγƒ 
Suguru Namura
Β 
MongoDB Performance Debugging
MongoDB Performance Debugging
MongoDB
Β 
Everything you wanted to know about Stack Traces and Heap Dumps
Everything you wanted to know about Stack Traces and Heap Dumps
Andrei Pangin
Β 
Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?
Dinh Pham
Β 
Shared memory and multithreading in Node.js - Timur Shemsedinov - JSFest'19
Shared memory and multithreading in Node.js - Timur Shemsedinov - JSFest'19
Timur Shemsedinov
Β 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scale
Tom Croucher
Β 
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Tom Croucher
Β 
DEF CON 23 - amit ashbel and maty siman - game of hacks
DEF CON 23 - amit ashbel and maty siman - game of hacks
Felipe Prado
Β 
Node js presentation
Node js presentation
martincabrera
Β 
introduction to node.js
introduction to node.js
orkaplan
Β 
The Art of JVM Profiling
The Art of JVM Profiling
Andrei Pangin
Β 
Π˜Π½Π΄Π΅ΠΊΡΠΈΡ€ΡƒΠ΅ΠΌ Π±Π°Π·Ρƒ: ΠΊΠ°ΠΊ Π΄Π΅Π»Π°Ρ‚ΡŒ Ρ…ΠΎΡ€ΠΎΡˆΠΎ ΠΈ Π½Π΅ Π΄Π΅Π»Π°Ρ‚ΡŒ ΠΏΠ»ΠΎΡ…ΠΎ Winter saint p 2021 m...
Π˜Π½Π΄Π΅ΠΊΡΠΈΡ€ΡƒΠ΅ΠΌ Π±Π°Π·Ρƒ: ΠΊΠ°ΠΊ Π΄Π΅Π»Π°Ρ‚ΡŒ Ρ…ΠΎΡ€ΠΎΡˆΠΎ ΠΈ Π½Π΅ Π΄Π΅Π»Π°Ρ‚ΡŒ ΠΏΠ»ΠΎΡ…ΠΎ Winter saint p 2021 m...
АндрСй Новиков
Β 
03 standard class library
03 standard class library
eleksdev
Β 
Building a real life application in node js
Building a real life application in node js
fakedarren
Β 
Multithreading in Node.js and JavaScript
Multithreading in Node.js and JavaScript
Timur Shemsedinov
Β 
MongoDB + node.js γ§δ½œγ‚‹γ‚½γƒΌγ‚·γƒ£γƒ«γ‚²γƒΌγƒ 
MongoDB + node.js γ§δ½œγ‚‹γ‚½γƒΌγ‚·γƒ£γƒ«γ‚²γƒΌγƒ 
Suguru Namura
Β 
MongoDB Performance Debugging
MongoDB Performance Debugging
MongoDB
Β 
Everything you wanted to know about Stack Traces and Heap Dumps
Everything you wanted to know about Stack Traces and Heap Dumps
Andrei Pangin
Β 
Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?
Dinh Pham
Β 

Similar to Metaprogramming with JavaScript (20)

"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
Fwdays
Β 
Metaprogramming, Metaobject Protocols, Gradual Type Checks: Optimizing the "U...
Metaprogramming, Metaobject Protocols, Gradual Type Checks: Optimizing the "U...
Stefan Marr
Β 
Metaprogramming by brandon
Metaprogramming by brandon
MaslowB
Β 
TripCase Unit Testing with Jasmine
TripCase Unit Testing with Jasmine
Stephen Pond
Β 
Metaprogramming in ES6
Metaprogramming in ES6
HΓ©ctor Pablos LΓ³pez
Β 
Metarhia KievJS 22-Feb-2018
Metarhia KievJS 22-Feb-2018
Timur Shemsedinov
Β 
Rediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The Libraries
Simon Willison
Β 
Exploring metaprogramming using Ruby language
Exploring metaprogramming using Ruby language
Harshal Hayatnagarkar
Β 
Refactoring
Refactoring
Amir Barylko
Β 
10 Ways To Improve Your Code( Neal Ford)
10 Ways To Improve Your Code( Neal Ford)
guestebde
Β 
A Systematic Language Engineering Approach for Prototyping Domain Specific Mo...
A Systematic Language Engineering Approach for Prototyping Domain Specific Mo...
Luis Pedro
Β 
ECMAScript 6 new features
ECMAScript 6 new features
GephenSG
Β 
Refactoring
Refactoring
Amir Barylko
Β 
Testing Model Transformations
Testing Model Transformations
miso_uam
Β 
What’s new in ECMAScript 6.0
What’s new in ECMAScript 6.0
Eyal Vardi
Β 
Rapid software evolution
Rapid software evolution
borislav
Β 
JavaScript Growing Up
JavaScript Growing Up
David Padbury
Β 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy Dyagilev
JavaDayUA
Β 
Semantic code transformations in MetaJS
Semantic code transformations in MetaJS
Dmytro Dogadailo
Β 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almost
Quinton Sheppard
Β 
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
Fwdays
Β 
Metaprogramming, Metaobject Protocols, Gradual Type Checks: Optimizing the "U...
Metaprogramming, Metaobject Protocols, Gradual Type Checks: Optimizing the "U...
Stefan Marr
Β 
Metaprogramming by brandon
Metaprogramming by brandon
MaslowB
Β 
TripCase Unit Testing with Jasmine
TripCase Unit Testing with Jasmine
Stephen Pond
Β 
Metarhia KievJS 22-Feb-2018
Metarhia KievJS 22-Feb-2018
Timur Shemsedinov
Β 
Rediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The Libraries
Simon Willison
Β 
Exploring metaprogramming using Ruby language
Exploring metaprogramming using Ruby language
Harshal Hayatnagarkar
Β 
10 Ways To Improve Your Code( Neal Ford)
10 Ways To Improve Your Code( Neal Ford)
guestebde
Β 
A Systematic Language Engineering Approach for Prototyping Domain Specific Mo...
A Systematic Language Engineering Approach for Prototyping Domain Specific Mo...
Luis Pedro
Β 
ECMAScript 6 new features
ECMAScript 6 new features
GephenSG
Β 
Testing Model Transformations
Testing Model Transformations
miso_uam
Β 
What’s new in ECMAScript 6.0
What’s new in ECMAScript 6.0
Eyal Vardi
Β 
Rapid software evolution
Rapid software evolution
borislav
Β 
JavaScript Growing Up
JavaScript Growing Up
David Padbury
Β 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy Dyagilev
JavaDayUA
Β 
Semantic code transformations in MetaJS
Semantic code transformations in MetaJS
Dmytro Dogadailo
Β 
Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almost
Quinton Sheppard
Β 
Ad

More from Timur Shemsedinov (20)

How to use Chat GPT in JavaScript optimizations for Node.js
How to use Chat GPT in JavaScript optimizations for Node.js
Timur Shemsedinov
Β 
IT Revolution in 2023-2024: AI, GPT, business transformation, future professi...
IT Revolution in 2023-2024: AI, GPT, business transformation, future professi...
Timur Shemsedinov
Β 
Node.js threads for I/O-bound tasks
Node.js threads for I/O-bound tasks
Timur Shemsedinov
Β 
Node.js МСньшС слоТности, большС надСТности Holy.js 2021
Node.js МСньшС слоТности, большС надСТности Holy.js 2021
Timur Shemsedinov
Β 
Rethinking low-code
Rethinking low-code
Timur Shemsedinov
Β 
Hat full of developers
Hat full of developers
Timur Shemsedinov
Β 
FwDays 2021: Metarhia Technology Stack for Node.js
FwDays 2021: Metarhia Technology Stack for Node.js
Timur Shemsedinov
Β 
Node.js for enterprise 2021 - JavaScript Fwdays 3
Node.js for enterprise 2021 - JavaScript Fwdays 3
Timur Shemsedinov
Β 
Node.js in 2021
Node.js in 2021
Timur Shemsedinov
Β 
Node.js middleware: Never again!
Node.js middleware: Never again!
Timur Shemsedinov
Β 
Patterns and antipatterns
Patterns and antipatterns
Timur Shemsedinov
Β 
Race-conditions-web-locks-and-shared-memory
Race-conditions-web-locks-and-shared-memory
Timur Shemsedinov
Β 
Asynchronous programming and mutlithreading
Asynchronous programming and mutlithreading
Timur Shemsedinov
Β 
Node.js in 2020 - part 3
Node.js in 2020 - part 3
Timur Shemsedinov
Β 
Node.js in 2020 - part 2
Node.js in 2020 - part 2
Timur Shemsedinov
Β 
Information system structure and architecture
Information system structure and architecture
Timur Shemsedinov
Β 
Node.js in 2020 - part 1
Node.js in 2020 - part 1
Timur Shemsedinov
Β 
Web Locks API
Web Locks API
Timur Shemsedinov
Β 
Node.js in 2020
Node.js in 2020
Timur Shemsedinov
Β 
Π’Π²Π΅Π΄Π΅Π½ΠΈΠ΅ Π² SQL
Π’Π²Π΅Π΄Π΅Π½ΠΈΠ΅ Π² SQL
Timur Shemsedinov
Β 
How to use Chat GPT in JavaScript optimizations for Node.js
How to use Chat GPT in JavaScript optimizations for Node.js
Timur Shemsedinov
Β 
IT Revolution in 2023-2024: AI, GPT, business transformation, future professi...
IT Revolution in 2023-2024: AI, GPT, business transformation, future professi...
Timur Shemsedinov
Β 
Node.js threads for I/O-bound tasks
Node.js threads for I/O-bound tasks
Timur Shemsedinov
Β 
Node.js МСньшС слоТности, большС надСТности Holy.js 2021
Node.js МСньшС слоТности, большС надСТности Holy.js 2021
Timur Shemsedinov
Β 
Hat full of developers
Hat full of developers
Timur Shemsedinov
Β 
FwDays 2021: Metarhia Technology Stack for Node.js
FwDays 2021: Metarhia Technology Stack for Node.js
Timur Shemsedinov
Β 
Node.js for enterprise 2021 - JavaScript Fwdays 3
Node.js for enterprise 2021 - JavaScript Fwdays 3
Timur Shemsedinov
Β 
Node.js middleware: Never again!
Node.js middleware: Never again!
Timur Shemsedinov
Β 
Patterns and antipatterns
Patterns and antipatterns
Timur Shemsedinov
Β 
Race-conditions-web-locks-and-shared-memory
Race-conditions-web-locks-and-shared-memory
Timur Shemsedinov
Β 
Asynchronous programming and mutlithreading
Asynchronous programming and mutlithreading
Timur Shemsedinov
Β 
Node.js in 2020 - part 3
Node.js in 2020 - part 3
Timur Shemsedinov
Β 
Node.js in 2020 - part 2
Node.js in 2020 - part 2
Timur Shemsedinov
Β 
Information system structure and architecture
Information system structure and architecture
Timur Shemsedinov
Β 
Node.js in 2020 - part 1
Node.js in 2020 - part 1
Timur Shemsedinov
Β 
Π’Π²Π΅Π΄Π΅Π½ΠΈΠ΅ Π² SQL
Π’Π²Π΅Π΄Π΅Π½ΠΈΠ΅ Π² SQL
Timur Shemsedinov
Β 
Ad

Recently uploaded (20)

Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Maxim Salnikov
Β 
GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?
felipeceotto
Β 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
Β 
Agile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
Β 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
Β 
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
Β 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
Β 
Looking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdf
Looking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdf
Varsha Nayak
Β 
dp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdf
pravkumarbiz
Β 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
Β 
What is data visualization and how data visualization tool can help.pdf
What is data visualization and how data visualization tool can help.pdf
Varsha Nayak
Β 
Who will create the languages of the future?
Who will create the languages of the future?
Jordi Cabot
Β 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
Β 
Transmission Media. (Computer Networks)
Transmission Media. (Computer Networks)
S Pranav (Deepu)
Β 
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
alexandernoetzold
Β 
Step by step guide to install Flutter and Dart
Step by step guide to install Flutter and Dart
S Pranav (Deepu)
Β 
Open Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
Β 
AI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA Technologies
SandeepKS52
Β 
wAIred_RabobankIgniteSession_12062025.pptx
wAIred_RabobankIgniteSession_12062025.pptx
SimonedeGijt
Β 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
Β 
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Agentic Techniques in Retrieval-Augmented Generation with Azure AI Search
Maxim Salnikov
Β 
GDG Douglas - Google AI Agents: Your Next Intern?
GDG Douglas - Google AI Agents: Your Next Intern?
felipeceotto
Β 
OpenTelemetry 101 Cloud Native Barcelona
OpenTelemetry 101 Cloud Native Barcelona
Imma Valls Bernaus
Β 
Agile Software Engineering Methodologies
Agile Software Engineering Methodologies
Gaurav Sharma
Β 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
Β 
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
Β 
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
MOVIE RECOMMENDATION SYSTEM, UDUMULA GOPI REDDY, Y24MC13085.pptx
Maharshi Mallela
Β 
Looking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdf
Looking for a BIRT Report Alternative Here’s Why Helical Insight Stands Out.pdf
Varsha Nayak
Β 
dp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdf
pravkumarbiz
Β 
Code and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
Β 
What is data visualization and how data visualization tool can help.pdf
What is data visualization and how data visualization tool can help.pdf
Varsha Nayak
Β 
Who will create the languages of the future?
Who will create the languages of the future?
Jordi Cabot
Β 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
Β 
Transmission Media. (Computer Networks)
Transmission Media. (Computer Networks)
S Pranav (Deepu)
Β 
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
Neuralink Templateeeeeeeeeeeeeeeeeeeeeeeeee
alexandernoetzold
Β 
Step by step guide to install Flutter and Dart
Step by step guide to install Flutter and Dart
S Pranav (Deepu)
Β 
Open Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
Β 
AI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA Technologies
SandeepKS52
Β 
wAIred_RabobankIgniteSession_12062025.pptx
wAIred_RabobankIgniteSession_12062025.pptx
SimonedeGijt
Β 
Porting Qt 5 QML Modules to Qt 6 Webinar
Porting Qt 5 QML Modules to Qt 6 Webinar
ICS
Β 

Metaprogramming with JavaScript

  • 2. What is Metaprogramming? β€’ Templates and macroses to generate source code in compile-time β€’ Self-changing programs β€’ Programs that generate other programs β€’ other meanings ?
  • 3. What is Metaprogramming? 1. This is not artificial intelligence. 2. Metaprogramming is not something new, you've always used it. 3. In programming languages ​​of Von Neumann architecture nothing happens without metadata (architecture in which data and instructions are stored in shared memory and computer needs to recognize numbers, strings, addresses, executable instruction, etc.)
  • 4. What is Metaprogramming? Programming paradigm that implies program structure and functionality modification programmatically
  • 5. How Metaprogramming works? Programming paradigm that implies program structure and functionality modification programmatically 1. When changes occur? 2. What is changing? 3. Whereby changes occur?
  • 6. How Metaprogramming works? When changes occurs? β€’ Design-time β€’ Compile-time β€’ Run-time β€’ Just-in-Time (immediate when executing/processing) β€’ Lazy (between executing/processing) β€’ On Timer β€’ On Pull requests β€’ On Push notifications
  • 7. How Metaprogramming works? What is changing? β€’ data types and data structures β€’ identifiers (class names, type names, variable names) β€’ calls (method/function names, dynamic binding) β€’ algorithm parameters β€’ formula syntax, regular expression syntax, etc. β€’ dynamic code interpretation (eval) β€’ data serialization/deserialization
  • 8. How Metaprogramming works? Whereby changes occurs? β€’ Parsing and translation of syntactic structures β€’ Access to objects and functions by identification name β€’ Full introspection β€’ First-class objects individuation: β€’ functions, using closures β€’ classes, using inheritance (prototypes in js) β€’ objects, using mixins
  • 9. The problem definition Why do we need Metaprogramming? β€’ To raise functionality, universality and abstraction level of software solutions β€’ Dynamic domains when changing functionality and structure is a normal mode β€’ Intersystem/inter-module integration, using introspection and dynamic binding
  • 10. Example 1: data definition var names = [ "Marcus Aurelius Antoninus Augustus", "Darth Vader", "Victor Michailovich Glushkov", "Gottfried Wilhelm von Leibniz", "Mao Zedong", "Vladimir Sergeevich Soloviov", "Ibn Arabi", "Lev Nikolayevich Tolstoy", "Muammar Muhammad Abu Minyar al-Gaddafi", "Rene Descartes", "Fyodor Mikhailovich Dostoyevsky", "Benedito de Espinosa" ];
  • 11. Example 1: solution (without metaprogramming) function filter(names) { var result = [], name; for (var i=0; i<names.length; i++) { name = names[i]; if ( name.length >= 10 && name.length <= 200 && name.indexOf("Mich") > -1 && name.indexOf("V") === 0 && name.slice(-2) == "ov" && !( name.length >= 50 && name.length <= 65 && name.indexOf("Abu") > -1 && name.indexOf("Lev") === 0 && name.slice(-3) == "iov") ) result.push(name); } return result; }
  • 12. Example 1: extracted metadata var conditions = { length: [10, 200], contains: "Mich", starts: "V", ends: "ov", not: { length: [50, 65], contains: "Abu", starts: "Lev", ends: "iov" } };
  • 13. Example 1: metamodel function filter(names, conditions) { var operations = { length: function(s,v) { return s.length>=v[0] && s.length<=v[1] }, contains: function(s,v) { return s.indexOf(v) > -1 }, starts: function(s,v) { return s.indexOf(v) === 0 }, ends: function(s,v) { return s.slice(-v.length) == v }, not: function(s,v) { return !check(s,v) } }; function check(s, conditions) { var valid = true; for (var key in conditions) valid &= operations[key](s, conditions[key]); return valid; } return names.filter(function(s) { return check(s, conditions); }); }
  • 14. Unified model for IS module
  • 15. Example 2: task definition var tasks = [ { interval:5000, get:"http://127.0.0.1/api/method1.json", expect:"OK", save:"file1.json" }, { interval:"8s", get:"http://127.0.0.1/api/method2.json", put:"http://127.0.0.1/api/method4.json", save:"file2.json" }, { interval:"7s", get:"http://127.0.0.1/api/method3.json", expect:"Done", post:"http://127.0.0.1/api/method5.json" }, { interval:"4s", load:"file1.json", expect:"OK", put:"http://127.0.0.1/api/method6.json" }, { interval:"9s", load:"file2.json", save:"file1.json", post:"http://127.0.0.1/api/method7.json" }, { interval:"3s", load:"file1.json", save:"file3.json" }, ];
  • 16. Example 2: metamodel function iterate(tasks) { function closureTask(task) { return function () { console.dir(task); var source; if (task.get) source = request.get(task.get); if (task.load) source = fs.createReadStream(task.load); if (task.save) source.pipe(fs.createWriteStream(task.save)); if (task.post) source.pipe(request.post(task.post)); if (task.put) source.pipe(request.put(task.put)); } }; for (var i=0; i<tasks.length; i++) setInterval(closureTask(tasks[i]), duration(tasks[i].interval)); }
  • 17. Example 2: metamodel (with internal configuration) function iterate(tasks) { var sources = { get: request.get, load: fs.createReadStream }; var destinations = { save: fs.createWriteStream, post: request.post, put: request.put }; function closureTask(task) { return function () { console.dir(task); var verb, source, destination; for (key in sources) if (task[key]) source = sources[key](task[key]); for (key in destinations) if (task[key]) source.pipe(destinations[key](task[key])); } }; for (var i=0; i<tasks.length; i++) setInterval(closureTask(tasks[i]), duration(tasks[i].interval)); }
  • 18. Example 3: interpretation // Parse duration to seconds, example: duration("1d 10h 7m 13s") function duration(s) { var result = 0; if (typeof(s) == 'string') { var days = s.match(/(d+)s*d/), hours = s.match(/(d+)s*h/), minutes = s.match(/(d+)s*m/), seconds = s.match(/(d+)s*s/); if (days) result += parseInt(days[1])*86400; if (hours) result += parseInt(hours[1])*3600; if (minutes) result += parseInt(minutes[1])*60; if (seconds) result += parseInt(seconds[1]); result = result*1000; } if (typeof(s) == 'number') result = s; return result; }
  • 19. Example 3: interpretation (configurable) function duration(s) { if (typeof(s) == 'number') return s; var units = { days: { rx:/(d+)s*d/, mul:86400 }, hours: { rx:/(d+)s*h/, mul:3600 }, minutes: { rx:/(d+)s*m/, mul:60 }, seconds: { rx:/(d+)s*s/, mul:1 } }; var result = 0, unit, match; if (typeof(s) == 'string') for (var key in units) { unit = units[key]; match = s.match(unit.rx); if (match) result += parseInt(match[1])*unit.mul; } return result*1000; }
  • 20. Example 4: introspection in metaprogramming
  • 21. Example 4: introspection var ds = wcl.AjaxDataSource({ read: { get: "examples/person/read.json" }, insert: { post: "examples/person/insert.json" }, update: { post: "examples/person/update.json" }, delete: { post: "examples/person/delete.json" }, find: { post: "examples/person/find.json" }, metadata: { post: "examples/person/metadata.json" } }); ds.read({ id:5 }, function(err, data) { data.phone ="+0123456789"; ds.update(data, function(err) { console.log('Data saved'); }); });
  • 22. Example 4: introspection in metaprogramming var ds = wcl.AjaxDataSource({ introspect: { post: "examples/person/introspect.json" } }); ds.read({ id:3 }, function(err, data) { data.phone ="+0123456789"; ds.update(data, function(err) { console.log('Data saved'); }); });
  • 23. Example 4: introspection in metaprogramming var ds = wcl.MemoryDataSource({ data: [ { id:1, name:"Person 1", phone:"+380501002011", emails:[ "[email protected]" ], age: 25 }, { id:2, name:"Person 2", phone:"+380501002022", emails:[ "[email protected]", "[email protected]" ], address: { city: "Kiev", street:"Khreschatit", building: "26" } }, { id:3, name:"Person 3", phone:"+380501002033", emails:[ "[email protected]" ], tags: [ {tag:"tag1", color:"red"}, {tag:"tag2", color:"green"} ] }, ]}); ds.read({ id:3 }, function(err, data) { data.phone ="+0123456789"; ds.update(data, function(err) { console.log('Data saved'); }); });
  • 24. Metaprogramming techniques β€’ Task definition style: declarative, using metadata, imperative and functional elements β€’ Hashes (associative arrays) beforehand unknown key: var a = {}; a[key] = value; β€’ String interpretation, inventing domain-specific syntactic structures or using universal ones (json, js, regexp ...) β€’ Mixins: beforehand unknown targer object/class function mixin(a) { a.fn=function(){ ... } } β€’ Closures: function individuations fn = (function(a) { return function() { return a*2 } })(value)
  • 25. The findings β€’ Code size: generally decrease a lot but may increases code size in rare cases (compensated by code readability) β€’ Speed: slightly decreases but good implementation may remain approximately the same speed β€’ Flexibility: solution becomes more abstract/universal software application scope expands β€’ Integration: usually much simplified integration and requires less code changes β€’ Working pleasure: metaprogramming is interesting so we have more pleasure and motivation β€’ Working speed: increasing development time but modification and support tasks take less time, so total time is less
  • 27. Metaprogramming with JavaScript Thank you! Questions please Timur Shemsedinov Research Institute of System Technologies