SlideShare a Scribd company logo
The new wave of
JavaScript techniques
Practical pairing of generative programming
with functional programming
Eugene Lazutkin, ClubAjax, 6/4/2013
Wednesday, June 5, 13
Generative programming
Wednesday, June 5, 13
What is GP?
Generative programming (GP):
Creates source code automatically to improve
programmer productivity.
Can be considered as a form of code reuse.
Sometimes called “automatic programming”.
Wednesday, June 5, 13
Examples of GP
Macro processors:
C preprocessor.
C++ templates and inline functions.
IDE “wizards” in Eclipse, MS Visual Studio.
Meta-languages and their compilers.
Domain-specific languages (DSL).
Wednesday, June 5, 13
Why do we need GP?
Higher-level programming.
Meta-programming.
Modeling.
DSL.
Wednesday, June 5, 13
Why do we need GP?
Performance improvements.
Program transformations.
Data transformations.
Code optimization and generation.
Applies to both compilers and interpreters.
Wednesday, June 5, 13
Why do I need GP?
Common narrative goes like that:
“Compilers and interpreters are now very
sophisticated – no need to optimize my code.”
“Modern CPUs with their fancy pipelining,
parallel execution, and branch prediction
eliminate code inefficiencies.”
Wednesday, June 5, 13
Don’t write inefficient code
Always estimate the algorithmic complexity of
your code (Big O notation).
Remember simple things:
Linear code usually works faster than code
with branches or loops.
Calling a function is not free. If its body is
small, it may make sense to inline it.
Wednesday, June 5, 13
Don’t write inefficient code
Remember simple things:
Simplify!
Simple shorter code is usually faster.
Eliminate unnecessary variables.
Profile!!!
BTW, this was an advice by Jared Jurkiewicz.
Wednesday, June 5, 13
Performance summary
Do not expect that an inefficient code will be
magically efficient. Always profile!
In a battle of a code clarity with a code
efficiency support the clarity, if possible.
Avoid premature optimization by taking complex
steps, yet do not write obviously slow code.
Wednesday, June 5, 13
JavaScript and GP
People coming from static languages frequently
overlook or discount GP facilities in JS.
JS has four ways to generate code dynamically:
eval(), new Function(), setTimeout(),
setInterval().
Browser allows to inline code.
Wednesday, June 5, 13
JavaScript and GP
Amazing code-generation facilities are a big
differentiator for Javascript.
Yet they are frequently shunned for numerous
reasons, including security, which is not applied
in all scenarios.
Wednesday, June 5, 13
JavaScript and GP
Recently GP crept in JavaScript libraries.
Examples:
Many templating languages are actually
compiled in JavaScript or can be compiled:
Mustache, Handlebars, jQuery templates...
Wednesday, June 5, 13
JavaScript and GP
Examples:
All JavaScript-based templates:
EJS, JST (e.g., Underscore, Lo-Dash)...
All transpilers:
CoffeeScript, GWT, Emscripten...
Numerous code-level tools (minifiers...).
Wednesday, June 5, 13
JavaScript and GP
Examples:
Some libraries generate code for similar
functions from the same template in order to
reduce their downloadable size, and optimize
performance.
Lo-Dash...
Wednesday, June 5, 13
Functional programming
Wednesday, June 5, 13
What is FP?
Functional programming (FP):
Treats computations as the evaluation of
mathematical functions.
Avoids state, mutations, side-effects.
Emphasizes “scenario” over “a list of actors”.
Concerns more with “how” rather than
“who” and “what”.
Wednesday, June 5, 13
JavaScript and FP
“JavaScript is Lisp in C’s clothing.” – said
Douglas Crockford back in 2001.
Functions are first-class objects.
Lambdas are supported (functions can be
inlined, and passed around).
Closures are supported (functions can access
their environment).
Wednesday, June 5, 13
Practical applications of FP
Abstracting control flow details with array extras:
var total = data
.filter(function(value){ return value % 2; })
.map(function(value){ return value * value; })
.reduce(function(acc, value){
return acc + value;
}, 0);
Wednesday, June 5, 13
Practical applications of FP
We can even use “building blocks” now:
var total = data
.filter(isOdd)
.map(square)
.reduce(sum, 0);
Wednesday, June 5, 13
Why I like this code
It is easy to understand.
It is easy to decompose or refactor.
There is no trivial technical noise like names of
index and value variables, and exit conditions.
Wednesday, June 5, 13
Building blocks
In general we are assumed to create our
programs from right-sized building blocks.
Bigger blocks are built from smaller blocks with a
mortar/language.
It applies to any programming paradigms.
Wednesday, June 5, 13
...and reality
How come in real projects we see methods and
functions in 100s and even 1000s lines of code?
Sometimes there are “good” reasons for that.
It can take more time to figure out our
building blocks than to code everything from
scratch.
Wednesday, June 5, 13
...and more reality
Sometimes there are “good” reasons for that.
Calling myriad of small functions can be
detrimental to performance.
Is it really the case? It can be verified with a
profiler, which takes time too.
It is possible to create a different system of
“building blocks”.
Wednesday, June 5, 13
Case for pipes
Pipes are individual components of a pipeline.
Each pipe has an input and an output
(sometimes more than one of those).
Pipeline combines individual pipes by
connecting their inputs and outputs producing a
program.
A pipeline can be used as a pipe.
Wednesday, June 5, 13
Case for pipes
Unix shells are a classic examples of pipes.
Closely related concept: dataflow.
Complex applications frequently can be
expressed in terms of event flow.
Hot topics in JS: event streams, data streams.
Wednesday, June 5, 13
Pipes and chaining
While chaining is a frequent implementation
technique for pipes, it doesn’t mean that all
chaining equates to pipes.
Pipes encapsulate streams of data and
orchestrate processing of individual bits.
Frequently chaining operates in terms of
transferred objects (streams), not data bits.
Wednesday, June 5, 13
Chaining
Examples in JS of chaining without piping:
jQuery
Underscore
JavaScript array extras
All examples above create a container object
before passing it down.
Wednesday, June 5, 13
Theory and practice
Wednesday, June 5, 13
Can we do better?
Let’s use our simple example as a start point and
optimize it by inlining functions:
var total = data
.filter(function(value){ return value % 2; })
.map(function(value){ return value * value; })
.reduce(function(acc, value){
return acc + value;
}, 0);
Wednesday, June 5, 13
Can we do better?
var a1 = [];
for(var i = 0; i < data.length; ++i){
if(data[i] % 2) a1.push(data[i]);
}
var a2 = new Array(a1.length);
for(var i = 0; i < a1.length; ++i){
a2[i] = a1[i] * a1[i];
}
var acc = 0;
for(var i = 0; i < a2.length; ++i){
acc += a2[i];
}
var total = acc;
Wednesday, June 5, 13
Can we do better?
Let’s merge loops and eliminate intermediate
arrays:
var acc = 0;
for(var i = 0; i < data.length; ++i){
var value = data[i];
if(value % 2) acc += value * value;
}
var total = acc;
Wednesday, June 5, 13
Performance results
0 750 1500 2250 3000
ms
Manual (38ms) Extra (2885ms)
Wednesday, June 5, 13
We did better!
And the results are in:
It is faster.
We loop over values manually.
We cannot easily move this snippet around
because our technical variable names may
clash.
Ultimately it was a trade-off.
Wednesday, June 5, 13
Can we have our cake and eat it too?
Yes!
Wednesday, June 5, 13
Can we have our cake and eat it too?
We need a way to describe a result.
Not how exactly get it, but its logical inference.
Then we can apply GP to generate an optimal
code.
Wednesday, June 5, 13
FP and pipes
FP gives us some answers:
Iterations can be described by higher-order
operations:
map, filter, fold/reduce, zipping, unzipping,
partitioning.
Secondary: forEach, every, some, indexOf.
Wednesday, June 5, 13
Heya Pipe
Wednesday, June 5, 13
Heya: ctr and pipe
Heya is a library that provides a modern
foundation for building web applications (both
server and client).
Heya Pipe is a functional toolkit to build efficient
data and event processing pipes using GP.
Heya Ctr provides a constructor to build
generated components.
Wednesday, June 5, 13
The Heya way
Let’s use our simple example and optimize it with
Heya Pipe:
var total = data
.filter(function(value){ return value % 2; })
.map(function(value){ return value * value; })
.reduce(function(acc, value){
return acc + value;
}, 0);
Wednesday, June 5, 13
The Heya way
Here it is:
var f = array(new Pipe()
.filter(“value % 2”)
.map(“value *= value;”)
.fold(“accumulator += value;”, 0)
).compile();
var total = f(data);
Wednesday, June 5, 13
Performance results
0 750 1500 2250 3000
ms
Manual (38ms) Pipe (53ms) Extra (2885ms)
Wednesday, June 5, 13
What do we see?
We are as expressive as array extras.
We are as performant as manual code.
The performance gap can be reduced.
In any case pipes are >40x faster than array
extras.
Wednesday, June 5, 13
Heya Pipe conventions
Heya Pipe is modeled after array extras.
It can accept the same parameters.
If a string is specified instead of a function it is
assumed to be a code snippet.
Code snippets work by conventions.
Wednesday, June 5, 13
Heya Pipe conventions
Snippets can have several lines of code.
“Boolean” snippets assume that the last line
returns a boolean value.
Snippet’s input is a “value” variable. A snippet
can modify it to return a different value.
Snippets for fold-able operations can access and
update an “accumulator” variable.
Wednesday, June 5, 13
Debugging
People make mistakes ⇒ code should be
debugged.
How to debug a generated code?
What is Heya Pipe story here?
Wednesday, June 5, 13
Debugging
The screenshot shows node.js debugging with
node-inspector.
The generated code appears in the file panel.
The code is fully readable.
Wednesday, June 5, 13
Heya Pipe
Provides a rich foundation for pipes:
forEach, transform, map, filter, indexOf, every,
some, fold, scan, skip, take, skipWhile,
takeWhile, voidResult.
Can work with potentially infinite streams.
Wednesday, June 5, 13
Heya Pipe
Can iterate over numerous containers:
Array (sparse and dense, including in reverse),
array slices (including in reverse), Object (keys,
values, own and inherited), iterators,
enumerators, generators, and perform unfold.
Includes a compiler, and an interpreter.
Can use pipes as sub-pipes.
Wednesday, June 5, 13
Heya Ctr
Implements GP helpers.
Includes a simple formatting-aware
templating.
Allows managing closures.
Serves as a foundation for Heya Pipes.
Wednesday, June 5, 13
Closures with Heya Ctr
Allows to inject variables in a closure, which will
be directly accessible from snippets by name.
Does not use “with” statement because it is
incompatible with strict mode.
Fully strict mode compatible.
Allows to access and modify closure-bound
variables.
Wednesday, June 5, 13
!at’s all
Folks!
Wednesday, June 5, 13

More Related Content

What's hot (20)

PDF
Skiron - Experiments in CPU Design in D
Mithun Hunsur
 
PDF
clWrap: Nonsense free control of your GPU
John Colvin
 
PPTX
C++ Coroutines
Sumant Tambe
 
PDF
Best Practices in Qt Quick/QML - Part 3
ICS
 
KEY
Know yourengines velocity2011
Demis Bellot
 
PPTX
Optimizing Communicating Event-Loop Languages with Truffle
Stefan Marr
 
PDF
Best Practices in Qt Quick/QML - Part 2
Janel Heilbrunn
 
PPT
Big-data-analysis-training-in-mumbai
Unmesh Baile
 
DOC
Qtp realtime scripts
Ramu Palanki
 
PDF
A Domain-Specific Embedded Language for Programming Parallel Architectures.
Jason Hearne-McGuiness
 
PPTX
Building High-Performance Language Implementations With Low Effort
Stefan Marr
 
PPTX
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
Stefan Marr
 
PDF
Best Practices in Qt Quick/QML - Part III
ICS
 
PDF
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
PDF
Go Faster With Native Compilation
Rajeev Rastogi (KRR)
 
PDF
Дмитрий Копляров , Потокобезопасные сигналы в C++
Sergey Platonov
 
PPTX
Pune-Cocoa: Blocks and GCD
Prashant Rane
 
PDF
Comparing different concurrency models on the JVM
Mario Fusco
 
PPTX
System verilog assertions
HARINATH REDDY
 
PDF
How and why I turned my old Java projects into a first-class serverless compo...
Mario Fusco
 
Skiron - Experiments in CPU Design in D
Mithun Hunsur
 
clWrap: Nonsense free control of your GPU
John Colvin
 
C++ Coroutines
Sumant Tambe
 
Best Practices in Qt Quick/QML - Part 3
ICS
 
Know yourengines velocity2011
Demis Bellot
 
Optimizing Communicating Event-Loop Languages with Truffle
Stefan Marr
 
Best Practices in Qt Quick/QML - Part 2
Janel Heilbrunn
 
Big-data-analysis-training-in-mumbai
Unmesh Baile
 
Qtp realtime scripts
Ramu Palanki
 
A Domain-Specific Embedded Language for Programming Parallel Architectures.
Jason Hearne-McGuiness
 
Building High-Performance Language Implementations With Low Effort
Stefan Marr
 
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
Stefan Marr
 
Best Practices in Qt Quick/QML - Part III
ICS
 
Reactive Programming for a demanding world: building event-driven and respons...
Mario Fusco
 
Go Faster With Native Compilation
Rajeev Rastogi (KRR)
 
Дмитрий Копляров , Потокобезопасные сигналы в C++
Sergey Platonov
 
Pune-Cocoa: Blocks and GCD
Prashant Rane
 
Comparing different concurrency models on the JVM
Mario Fusco
 
System verilog assertions
HARINATH REDDY
 
How and why I turned my old Java projects into a first-class serverless compo...
Mario Fusco
 

Viewers also liked (16)

KEY
Dojo GFX: SVG in the real world
Eugene Lazutkin
 
KEY
Exciting JavaScript - Part II
Eugene Lazutkin
 
KEY
Exciting JavaScript - Part I
Eugene Lazutkin
 
PDF
Seeking Enligtenment - A journey of purpose rather tan instruction
Schalk Cronjé
 
PDF
Generative programming (mostly parser generation)
Ralf Laemmel
 
PPT
Practical Meta Programming
Reggie Meisler
 
PDF
Generative Software Development. Overview and Examples
Eelco Visser
 
PDF
A Generative Programming Approach to Developing Pervasive Computing Systems
Damien Cassou
 
PDF
Generative and Meta-Programming - Modern C++ Design for Parallel Computing
Joel Falcou
 
PDF
Japanese Open and Generative Design
Yuichi Yazaki
 
PDF
Seri Belajar Mandiri - Pemrograman C# Untuk Pemula
Agus Kurniawan
 
ODP
Probabilistic programming
Eli Gottlieb
 
PPTX
Summary - Transformational-Generative Theory
Marielis VI
 
PPTX
Transformational-Generative Grammar
Ruth Ann Llego
 
PPTX
Deep structure and surface structure
Asif Ali Raza
 
PPT
Ubiquitous Computing
u065932
 
Dojo GFX: SVG in the real world
Eugene Lazutkin
 
Exciting JavaScript - Part II
Eugene Lazutkin
 
Exciting JavaScript - Part I
Eugene Lazutkin
 
Seeking Enligtenment - A journey of purpose rather tan instruction
Schalk Cronjé
 
Generative programming (mostly parser generation)
Ralf Laemmel
 
Practical Meta Programming
Reggie Meisler
 
Generative Software Development. Overview and Examples
Eelco Visser
 
A Generative Programming Approach to Developing Pervasive Computing Systems
Damien Cassou
 
Generative and Meta-Programming - Modern C++ Design for Parallel Computing
Joel Falcou
 
Japanese Open and Generative Design
Yuichi Yazaki
 
Seri Belajar Mandiri - Pemrograman C# Untuk Pemula
Agus Kurniawan
 
Probabilistic programming
Eli Gottlieb
 
Summary - Transformational-Generative Theory
Marielis VI
 
Transformational-Generative Grammar
Ruth Ann Llego
 
Deep structure and surface structure
Asif Ali Raza
 
Ubiquitous Computing
u065932
 
Ad

Similar to Practical pairing of generative programming with functional programming. (20)

PDF
JavaScript for impatient programmers.pdf
JoaqunFerrariIlusus
 
PDF
Scalable JavaScript
Ynon Perek
 
PDF
A New Baseline for Front-End Devs
Rebecca Murphey
 
KEY
JavaScript Growing Up
David Padbury
 
PDF
Defensive programming in Javascript and Node.js
Ruben Tan
 
PPTX
Week 6 java script loops
brianjihoonlee
 
KEY
LISP: How I Learned To Stop Worrying And Love Parantheses
Dominic Graefen
 
PPTX
Functional Programming in Javascript - IL Tech Talks week
yoavrubin
 
PDF
Melbourne, Australia Global Day of Code Retreat 2018 gdcr18 - Event Slides
Victoria Schiffer
 
PPTX
All of javascript
Togakangaroo
 
PDF
CoffeeScript
Scott Leberknight
 
PDF
Fewd week5 slides
William Myers
 
PPTX
Why learn new programming languages
Jonas Follesø
 
PDF
Js in-ten-minutes
Phong Vân
 
PPTX
Advanced JavaScript
Zsolt Mészárovics
 
PPTX
How to build a JavaScript toolkit
Michael Nelson
 
PPTX
Awesomeness of JavaScript…almost
Quinton Sheppard
 
PDF
Functional Programming in JavaScript
Troy Miles
 
PPTX
Maintainable JavaScript 2012
Nicholas Zakas
 
PDF
Go courseday1
Khammari Elaiila
 
JavaScript for impatient programmers.pdf
JoaqunFerrariIlusus
 
Scalable JavaScript
Ynon Perek
 
A New Baseline for Front-End Devs
Rebecca Murphey
 
JavaScript Growing Up
David Padbury
 
Defensive programming in Javascript and Node.js
Ruben Tan
 
Week 6 java script loops
brianjihoonlee
 
LISP: How I Learned To Stop Worrying And Love Parantheses
Dominic Graefen
 
Functional Programming in Javascript - IL Tech Talks week
yoavrubin
 
Melbourne, Australia Global Day of Code Retreat 2018 gdcr18 - Event Slides
Victoria Schiffer
 
All of javascript
Togakangaroo
 
CoffeeScript
Scott Leberknight
 
Fewd week5 slides
William Myers
 
Why learn new programming languages
Jonas Follesø
 
Js in-ten-minutes
Phong Vân
 
Advanced JavaScript
Zsolt Mészárovics
 
How to build a JavaScript toolkit
Michael Nelson
 
Awesomeness of JavaScript…almost
Quinton Sheppard
 
Functional Programming in JavaScript
Troy Miles
 
Maintainable JavaScript 2012
Nicholas Zakas
 
Go courseday1
Khammari Elaiila
 
Ad

More from Eugene Lazutkin (16)

PDF
Service workers
Eugene Lazutkin
 
PDF
Advanced I/O in browser
Eugene Lazutkin
 
PDF
Functional practices in JavaScript
Eugene Lazutkin
 
PDF
Express: the web server for node.js
Eugene Lazutkin
 
PDF
TXJS 2013 in 10 minutes
Eugene Lazutkin
 
KEY
Optimization of modern web applications
Eugene Lazutkin
 
KEY
OOP in JS
Eugene Lazutkin
 
KEY
Pulsar
Eugene Lazutkin
 
KEY
SSJS, NoSQL, GAE and AppengineJS
Eugene Lazutkin
 
KEY
Dojo for programmers (TXJS 2010)
Eugene Lazutkin
 
KEY
RAD CRUD
Eugene Lazutkin
 
PPT
CRUD with Dojo
Eugene Lazutkin
 
KEY
Dojo GFX workshop slides
Eugene Lazutkin
 
PDF
Dojo (QCon 2007 Slides)
Eugene Lazutkin
 
PDF
DojoX GFX Session Eugene Lazutkin SVG Open 2007
Eugene Lazutkin
 
PDF
DojoX GFX Keynote Eugene Lazutkin SVG Open 2007
Eugene Lazutkin
 
Service workers
Eugene Lazutkin
 
Advanced I/O in browser
Eugene Lazutkin
 
Functional practices in JavaScript
Eugene Lazutkin
 
Express: the web server for node.js
Eugene Lazutkin
 
TXJS 2013 in 10 minutes
Eugene Lazutkin
 
Optimization of modern web applications
Eugene Lazutkin
 
OOP in JS
Eugene Lazutkin
 
SSJS, NoSQL, GAE and AppengineJS
Eugene Lazutkin
 
Dojo for programmers (TXJS 2010)
Eugene Lazutkin
 
RAD CRUD
Eugene Lazutkin
 
CRUD with Dojo
Eugene Lazutkin
 
Dojo GFX workshop slides
Eugene Lazutkin
 
Dojo (QCon 2007 Slides)
Eugene Lazutkin
 
DojoX GFX Session Eugene Lazutkin SVG Open 2007
Eugene Lazutkin
 
DojoX GFX Keynote Eugene Lazutkin SVG Open 2007
Eugene Lazutkin
 

Recently uploaded (20)

PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
PPTX
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
PPTX
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
The Growing Value and Application of FME & GenAI
Safe Software
 
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PDF
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PPSX
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
PDF
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
PDF
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
PDF
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
PDF
Python Conference Singapore - 19 Jun 2025
ninefyi
 
PDF
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
PDF
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
PDF
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
The Growing Value and Application of FME & GenAI
Safe Software
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
Python Conference Singapore - 19 Jun 2025
ninefyi
 
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 

Practical pairing of generative programming with functional programming.

  • 1. The new wave of JavaScript techniques Practical pairing of generative programming with functional programming Eugene Lazutkin, ClubAjax, 6/4/2013 Wednesday, June 5, 13
  • 3. What is GP? Generative programming (GP): Creates source code automatically to improve programmer productivity. Can be considered as a form of code reuse. Sometimes called “automatic programming”. Wednesday, June 5, 13
  • 4. Examples of GP Macro processors: C preprocessor. C++ templates and inline functions. IDE “wizards” in Eclipse, MS Visual Studio. Meta-languages and their compilers. Domain-specific languages (DSL). Wednesday, June 5, 13
  • 5. Why do we need GP? Higher-level programming. Meta-programming. Modeling. DSL. Wednesday, June 5, 13
  • 6. Why do we need GP? Performance improvements. Program transformations. Data transformations. Code optimization and generation. Applies to both compilers and interpreters. Wednesday, June 5, 13
  • 7. Why do I need GP? Common narrative goes like that: “Compilers and interpreters are now very sophisticated – no need to optimize my code.” “Modern CPUs with their fancy pipelining, parallel execution, and branch prediction eliminate code inefficiencies.” Wednesday, June 5, 13
  • 8. Don’t write inefficient code Always estimate the algorithmic complexity of your code (Big O notation). Remember simple things: Linear code usually works faster than code with branches or loops. Calling a function is not free. If its body is small, it may make sense to inline it. Wednesday, June 5, 13
  • 9. Don’t write inefficient code Remember simple things: Simplify! Simple shorter code is usually faster. Eliminate unnecessary variables. Profile!!! BTW, this was an advice by Jared Jurkiewicz. Wednesday, June 5, 13
  • 10. Performance summary Do not expect that an inefficient code will be magically efficient. Always profile! In a battle of a code clarity with a code efficiency support the clarity, if possible. Avoid premature optimization by taking complex steps, yet do not write obviously slow code. Wednesday, June 5, 13
  • 11. JavaScript and GP People coming from static languages frequently overlook or discount GP facilities in JS. JS has four ways to generate code dynamically: eval(), new Function(), setTimeout(), setInterval(). Browser allows to inline code. Wednesday, June 5, 13
  • 12. JavaScript and GP Amazing code-generation facilities are a big differentiator for Javascript. Yet they are frequently shunned for numerous reasons, including security, which is not applied in all scenarios. Wednesday, June 5, 13
  • 13. JavaScript and GP Recently GP crept in JavaScript libraries. Examples: Many templating languages are actually compiled in JavaScript or can be compiled: Mustache, Handlebars, jQuery templates... Wednesday, June 5, 13
  • 14. JavaScript and GP Examples: All JavaScript-based templates: EJS, JST (e.g., Underscore, Lo-Dash)... All transpilers: CoffeeScript, GWT, Emscripten... Numerous code-level tools (minifiers...). Wednesday, June 5, 13
  • 15. JavaScript and GP Examples: Some libraries generate code for similar functions from the same template in order to reduce their downloadable size, and optimize performance. Lo-Dash... Wednesday, June 5, 13
  • 17. What is FP? Functional programming (FP): Treats computations as the evaluation of mathematical functions. Avoids state, mutations, side-effects. Emphasizes “scenario” over “a list of actors”. Concerns more with “how” rather than “who” and “what”. Wednesday, June 5, 13
  • 18. JavaScript and FP “JavaScript is Lisp in C’s clothing.” – said Douglas Crockford back in 2001. Functions are first-class objects. Lambdas are supported (functions can be inlined, and passed around). Closures are supported (functions can access their environment). Wednesday, June 5, 13
  • 19. Practical applications of FP Abstracting control flow details with array extras: var total = data .filter(function(value){ return value % 2; }) .map(function(value){ return value * value; }) .reduce(function(acc, value){ return acc + value; }, 0); Wednesday, June 5, 13
  • 20. Practical applications of FP We can even use “building blocks” now: var total = data .filter(isOdd) .map(square) .reduce(sum, 0); Wednesday, June 5, 13
  • 21. Why I like this code It is easy to understand. It is easy to decompose or refactor. There is no trivial technical noise like names of index and value variables, and exit conditions. Wednesday, June 5, 13
  • 22. Building blocks In general we are assumed to create our programs from right-sized building blocks. Bigger blocks are built from smaller blocks with a mortar/language. It applies to any programming paradigms. Wednesday, June 5, 13
  • 23. ...and reality How come in real projects we see methods and functions in 100s and even 1000s lines of code? Sometimes there are “good” reasons for that. It can take more time to figure out our building blocks than to code everything from scratch. Wednesday, June 5, 13
  • 24. ...and more reality Sometimes there are “good” reasons for that. Calling myriad of small functions can be detrimental to performance. Is it really the case? It can be verified with a profiler, which takes time too. It is possible to create a different system of “building blocks”. Wednesday, June 5, 13
  • 25. Case for pipes Pipes are individual components of a pipeline. Each pipe has an input and an output (sometimes more than one of those). Pipeline combines individual pipes by connecting their inputs and outputs producing a program. A pipeline can be used as a pipe. Wednesday, June 5, 13
  • 26. Case for pipes Unix shells are a classic examples of pipes. Closely related concept: dataflow. Complex applications frequently can be expressed in terms of event flow. Hot topics in JS: event streams, data streams. Wednesday, June 5, 13
  • 27. Pipes and chaining While chaining is a frequent implementation technique for pipes, it doesn’t mean that all chaining equates to pipes. Pipes encapsulate streams of data and orchestrate processing of individual bits. Frequently chaining operates in terms of transferred objects (streams), not data bits. Wednesday, June 5, 13
  • 28. Chaining Examples in JS of chaining without piping: jQuery Underscore JavaScript array extras All examples above create a container object before passing it down. Wednesday, June 5, 13
  • 30. Can we do better? Let’s use our simple example as a start point and optimize it by inlining functions: var total = data .filter(function(value){ return value % 2; }) .map(function(value){ return value * value; }) .reduce(function(acc, value){ return acc + value; }, 0); Wednesday, June 5, 13
  • 31. Can we do better? var a1 = []; for(var i = 0; i < data.length; ++i){ if(data[i] % 2) a1.push(data[i]); } var a2 = new Array(a1.length); for(var i = 0; i < a1.length; ++i){ a2[i] = a1[i] * a1[i]; } var acc = 0; for(var i = 0; i < a2.length; ++i){ acc += a2[i]; } var total = acc; Wednesday, June 5, 13
  • 32. Can we do better? Let’s merge loops and eliminate intermediate arrays: var acc = 0; for(var i = 0; i < data.length; ++i){ var value = data[i]; if(value % 2) acc += value * value; } var total = acc; Wednesday, June 5, 13
  • 33. Performance results 0 750 1500 2250 3000 ms Manual (38ms) Extra (2885ms) Wednesday, June 5, 13
  • 34. We did better! And the results are in: It is faster. We loop over values manually. We cannot easily move this snippet around because our technical variable names may clash. Ultimately it was a trade-off. Wednesday, June 5, 13
  • 35. Can we have our cake and eat it too? Yes! Wednesday, June 5, 13
  • 36. Can we have our cake and eat it too? We need a way to describe a result. Not how exactly get it, but its logical inference. Then we can apply GP to generate an optimal code. Wednesday, June 5, 13
  • 37. FP and pipes FP gives us some answers: Iterations can be described by higher-order operations: map, filter, fold/reduce, zipping, unzipping, partitioning. Secondary: forEach, every, some, indexOf. Wednesday, June 5, 13
  • 39. Heya: ctr and pipe Heya is a library that provides a modern foundation for building web applications (both server and client). Heya Pipe is a functional toolkit to build efficient data and event processing pipes using GP. Heya Ctr provides a constructor to build generated components. Wednesday, June 5, 13
  • 40. The Heya way Let’s use our simple example and optimize it with Heya Pipe: var total = data .filter(function(value){ return value % 2; }) .map(function(value){ return value * value; }) .reduce(function(acc, value){ return acc + value; }, 0); Wednesday, June 5, 13
  • 41. The Heya way Here it is: var f = array(new Pipe() .filter(“value % 2”) .map(“value *= value;”) .fold(“accumulator += value;”, 0) ).compile(); var total = f(data); Wednesday, June 5, 13
  • 42. Performance results 0 750 1500 2250 3000 ms Manual (38ms) Pipe (53ms) Extra (2885ms) Wednesday, June 5, 13
  • 43. What do we see? We are as expressive as array extras. We are as performant as manual code. The performance gap can be reduced. In any case pipes are >40x faster than array extras. Wednesday, June 5, 13
  • 44. Heya Pipe conventions Heya Pipe is modeled after array extras. It can accept the same parameters. If a string is specified instead of a function it is assumed to be a code snippet. Code snippets work by conventions. Wednesday, June 5, 13
  • 45. Heya Pipe conventions Snippets can have several lines of code. “Boolean” snippets assume that the last line returns a boolean value. Snippet’s input is a “value” variable. A snippet can modify it to return a different value. Snippets for fold-able operations can access and update an “accumulator” variable. Wednesday, June 5, 13
  • 46. Debugging People make mistakes ⇒ code should be debugged. How to debug a generated code? What is Heya Pipe story here? Wednesday, June 5, 13
  • 47. Debugging The screenshot shows node.js debugging with node-inspector. The generated code appears in the file panel. The code is fully readable. Wednesday, June 5, 13
  • 48. Heya Pipe Provides a rich foundation for pipes: forEach, transform, map, filter, indexOf, every, some, fold, scan, skip, take, skipWhile, takeWhile, voidResult. Can work with potentially infinite streams. Wednesday, June 5, 13
  • 49. Heya Pipe Can iterate over numerous containers: Array (sparse and dense, including in reverse), array slices (including in reverse), Object (keys, values, own and inherited), iterators, enumerators, generators, and perform unfold. Includes a compiler, and an interpreter. Can use pipes as sub-pipes. Wednesday, June 5, 13
  • 50. Heya Ctr Implements GP helpers. Includes a simple formatting-aware templating. Allows managing closures. Serves as a foundation for Heya Pipes. Wednesday, June 5, 13
  • 51. Closures with Heya Ctr Allows to inject variables in a closure, which will be directly accessible from snippets by name. Does not use “with” statement because it is incompatible with strict mode. Fully strict mode compatible. Allows to access and modify closure-bound variables. Wednesday, June 5, 13