SlideShare a Scribd company logo
@FGRibreau
Implementing pattern-matching in JavaScript
@FGRibreau
Implementing pattern-matching in JavaScript
…or how to play with EcmaScript shortcoming
0
context
@FGRibreau
Une syntaxe de pattern-matching en JS ?
Sortez l'artillerie lourde.
@FGRibreau
_.flatten(links).map(link => {
[{protocol: 'HTTP'}]: => 1,
[{protocol: 'AMQP'}]: => 2
});
It would it be awesome to use some pattern-matching there right?
@FGRibreau
SyntaxError: /Users/FG/www/iadvize-services-
orchestration-tools/src/api/src/repositoryManagers/
github.js: Unexpected token (185:32)
183 |
184 | _.flatten(links).forEach(link => {
> 185 | [{protocol: 'HTTP'}]: => 1,
| ^
186 | [{protocol: 'AMQP'}]: => 2
187 | });
188 |
... and of course it’s not JS valid syntax
1
goal
@FGRibreau
syntactically short
javascript-minded syntax
functionally-way
I wanted a pattern-matching library
2
syntax
@FGRibreau
links.map(link => {
[{protocol: 'HTTP'}]: => 1,
[{protocol: 'AMQP'}]: => 2
});
Ok, so this is not valid,
what precisely is not valid and
how can we make it valid?
@FGRibreau
links.map(link => {
[{protocol: 'HTTP'}]: () => 1,
[{protocol: ‘AMQP'}]: () => 2
});
links.map(link => {
[{protocol: 'HTTP'}]: 1,
[{protocol: ‘AMQP'}]: 2
});
links.map(link => {
[{protocol: 'HTTP'}]: => 1,
[{protocol: 'AMQP'}]: => 2
});
@FGRibreau
{
[{protocol: ‘HTTP'}]: 1,
[{protocol: ‘AMQP’}]: 2
}
The rest is syntactically valid
ES6 "computed property names"
{
[{protocol: ‘HTTP’}]: () => 1,
[{protocol: ‘AMQP’}]: () => 2
}
@FGRibreau
links.map(link => {})
[undefined, undefined]
links.map(link => {1})
[undefined, undefined]
links.map(link => {return 1})
[1,1]
links.map(link => 1)
[1,1]
Syntactically valid, semantically invalid
… but then I won't have my pattern matching.
@FGRibreau
BUT…
@FGRibreau
If I go from there…
_.flatten(links).map(link => {
[{protocol: 'HTTP'}]: => 1,
[{protocol: 'AMQP'}]: => 2
});
@FGRibreau
If I go from there…
_.flatten(links).map(link => {
[{protocol: 'HTTP'}]: => 1,
[{protocol: 'AMQP'}]: => 2
});
_.flatten(links).map(match({
[{protocol: 'HTTP'}]: 1,
[{protocol: 'AMQP'}]: 2
}));
…to there…
@FGRibreau
… then it’s syntactically correct!
_.flatten(links).map(match({
[{protocol: 'HTTP'}]: 1,
[{protocol: 'AMQP'}]: 2
}));
@FGRibreau
… then it’s syntactically correct!
_.flatten(links).map(match({
[{protocol: 'HTTP'}]: 1,
[{protocol: 'AMQP'}]: 2
}));
@FGRibreau
… would be great too !
const linkNumber = match(link,{
[{protocol: 'HTTP'}]: 1,
[{protocol: 'AMQP'}]: 2
});
3
Semantic
@FGRibreau
{
'[object Object]': 2
}
evaluates to
ES6 "computed property names"
{
[{protocol: 'HTTP'}]: 1,
[{protocol: 'AMQP'}]: 2
}
plz fix this
plz fix this
@FGRibreau
evaluates to
ES6 "computed property names"
{
[when({protocol: ‘HTTP’})]: 1,
[when({protocol: ‘AMQP'})]: 2
}
{
'{"protocol":"HTTP"}': 1,
'{"protocol":"AMQP"}': 2
}
@FGRibreau
evaluates to
ES6 "computed property names"
{
[when({protocol: ‘HTTP’})]: 1,
[when({protocol: ‘AMQP'})]: 2
}
{
'{"protocol":"HTTP"}': 1,
'{"protocol":"AMQP"}': 2
}
@FGRibreau
Order is lost by match’s object
.map(match({
[when.range(0, 43)]: 42,
[when(42)]: 72
}))
when.range(0,43) => '["Symbol(match.pattern.RANGE)",0,43]'
when(42) => '[42]'
JS objects are an unordered collection of properties
@FGRibreau
Fixing properties declaration
?
.map(match({
[when.range(0, 43)]: 42,
[when(42)]: 72
}))
.map(match(new Map([
[ when.range(0, 43), 42 ],
[ when(42), 72 ],
[ when(), 'never should be hit' ]
])))
NO!
@FGRibreau
Fixing properties declaration
?
.map(match({
[when.range(0, 43)]: 42,
[when(42)]: 72
}))
.map(match([
[ when.range(0, 43), 42 ],
[ when(42), 72 ],
[ when(), 'never should be hit' ]
]))
NO!
plz fix this
plz fix this
@FGRibreau
Fixing properties declaration
.map(match({
[when.range(0, 43)]: 42,
[when(42)]: 72
}))
- same callsite
- single thread
- sequential evaluation
@FGRibreau
Fixing properties declaration
.map(match({
[when.range(0, 43)]: 42,
[when(42)]: 72
}))
- same callsite
- single thread
- sequential evaluation
@FGRibreau
Fixing properties declaration
when.range(0,43) => '[0, "Symbol(match.pattern.RANGE)",0,43]'
when(42) => '[1, 42]'
… #problemSolved …
.map(match({
[when.range(0, 43)]: 42,
[when(42)]: 72
}))
- same callsite
- single thread
- sequential evaluation
@FGRibreau
How to get matched value?
const fact = match({
[when(0)]: 1,
[when()]: n * fact(n-1)
});
fact(10);
n is not defined
@FGRibreau
How to get matched value?
const fact = match({
[when(0)]: 1,
[when()]: n * fact(n-1)
});
fact(10);
simple, use a function:
const fact = match({
[when(0)]: 1,
[when()]: (n) => n * fact(n-1)
});
fact(10); // 3628800
@FGRibreau
How to get matched value?
const fact = match({
[when(0)]: 1,
[when()]: n * fact(n-1)
});
fact(10);
simple, use a function:
const fact = match({
[when(0)]: 1,
[when()]: (n) => n * fact(n-1)
});
fact(10); // 3628800
4
wrap up
@FGRibreau
@FGRibreau
@FGRibreau
@FGRibreau
François-Guillaume
RIBREAU
@FGRibreau
Tightly crafted developer oriented
online real-time monitoring and
administration service for Redis.
Join us
Frontend Dev - Backend Dev
Fullstack Dev - DevOps
#scala #nodejs #react #docker #xmpp

More Related Content

What's hot (20)

PDF
Asynchronous API in Java8, how to use CompletableFuture
José Paumard
 
PDF
A Modest Introduction To Swift
John Anderson
 
PDF
Rhebok, High Performance Rack Handler / Rubykaigi 2015
Masahiro Nagano
 
PDF
Middy.js - A powerful Node.js middleware framework for your lambdas​
Luciano Mammino
 
PDF
Serverless, The Middy Way - Workshop
Luciano Mammino
 
PDF
What you need to remember when you upload to CPAN
charsbar
 
PPTX
Introduction to Debuggers
Saumil Shah
 
PDF
Adding 1.21 Gigawatts to Applications with RabbitMQ (PHP Oxford June Meetup 2...
James Titcumb
 
PDF
Release with confidence
John Congdon
 
PDF
GraphQL API in Clojure
Kent Ohashi
 
ODP
Java Boilerplate Busters
HamletDRC
 
PDF
Introducing Middy, Node.js middleware engine for AWS Lambda (FrontConf Munich...
Luciano Mammino
 
PPT
RingoJS
Oleg Podsechin
 
PDF
SDPHP - Percona Toolkit (It's Basically Magic)
Robert Swisher
 
PPT
Symfony2 Service Container: Inject me, my friend
Kirill Chebunin
 
PDF
Interceptors: Into the Core of Pedestal
Kent Ohashi
 
PDF
How to send gzipped requests with boto3
Luciano Mammino
 
PDF
Diving into HHVM Extensions (PHPNW Conference 2015)
James Titcumb
 
PDF
Better detection of what modules are used by some Perl 5 code
charsbar
 
PPT
2016年のPerl (Long version)
charsbar
 
Asynchronous API in Java8, how to use CompletableFuture
José Paumard
 
A Modest Introduction To Swift
John Anderson
 
Rhebok, High Performance Rack Handler / Rubykaigi 2015
Masahiro Nagano
 
Middy.js - A powerful Node.js middleware framework for your lambdas​
Luciano Mammino
 
Serverless, The Middy Way - Workshop
Luciano Mammino
 
What you need to remember when you upload to CPAN
charsbar
 
Introduction to Debuggers
Saumil Shah
 
Adding 1.21 Gigawatts to Applications with RabbitMQ (PHP Oxford June Meetup 2...
James Titcumb
 
Release with confidence
John Congdon
 
GraphQL API in Clojure
Kent Ohashi
 
Java Boilerplate Busters
HamletDRC
 
Introducing Middy, Node.js middleware engine for AWS Lambda (FrontConf Munich...
Luciano Mammino
 
SDPHP - Percona Toolkit (It's Basically Magic)
Robert Swisher
 
Symfony2 Service Container: Inject me, my friend
Kirill Chebunin
 
Interceptors: Into the Core of Pedestal
Kent Ohashi
 
How to send gzipped requests with boto3
Luciano Mammino
 
Diving into HHVM Extensions (PHPNW Conference 2015)
James Titcumb
 
Better detection of what modules are used by some Perl 5 code
charsbar
 
2016年のPerl (Long version)
charsbar
 

Similar to Implementing pattern-matching in JavaScript (short version) (12)

PDF
Aligning Ember.js with Web Standards
Matthew Beale
 
PDF
Hands on: The sexy side of JavaScript (feat. node.js)
Francesco Pira
 
PPTX
MiamiJS - The Future of JavaScript
Caridy Patino
 
PDF
Practical JavaScript Programming - Session 7/8
Wilson Su
 
PDF
ECMAScript2015
qmmr
 
PDF
Reason - introduction to language and its ecosystem | Łukasz Strączyński
Grand Parade Poland
 
PDF
ECMAScript 6 new features
GephenSG
 
PDF
Workshop 10: ECMAScript 6
Visual Engineering
 
PDF
Missing objects: ?. and ?? in JavaScript (BrazilJS 2018)
Igalia
 
PDF
Advanced JavaScript Development
Jussi Pohjolainen
 
PDF
Short intro to ECMAScript
Jussi Pohjolainen
 
PPTX
ES6: Features + Rails
Santosh Wadghule
 
Aligning Ember.js with Web Standards
Matthew Beale
 
Hands on: The sexy side of JavaScript (feat. node.js)
Francesco Pira
 
MiamiJS - The Future of JavaScript
Caridy Patino
 
Practical JavaScript Programming - Session 7/8
Wilson Su
 
ECMAScript2015
qmmr
 
Reason - introduction to language and its ecosystem | Łukasz Strączyński
Grand Parade Poland
 
ECMAScript 6 new features
GephenSG
 
Workshop 10: ECMAScript 6
Visual Engineering
 
Missing objects: ?. and ?? in JavaScript (BrazilJS 2018)
Igalia
 
Advanced JavaScript Development
Jussi Pohjolainen
 
Short intro to ECMAScript
Jussi Pohjolainen
 
ES6: Features + Rails
Santosh Wadghule
 
Ad

More from François-Guillaume Ribreau (15)

PDF
REX LEAN- Créer un SaaS et être rentable après 6 mois
François-Guillaume Ribreau
 
PDF
⛳️ Votre API passe-t-elle le contrôle technique ?
François-Guillaume Ribreau
 
PDF
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...
François-Guillaume Ribreau
 
PDF
He stopped using for/while loops, you won't believe what happened next!
François-Guillaume Ribreau
 
PDF
Une plateforme moderne pour le groupe SIPA/Ouest-France 
François-Guillaume Ribreau
 
PDF
[BreizhCamp, format 15min] Construire et automatiser l'ecosystème de son Saa...
François-Guillaume Ribreau
 
PDF
[BreizhCamp, format 15min] Une api rest et GraphQL sans code grâce à PostgR...
François-Guillaume Ribreau
 
PDF
RedisConf 2016 - Redis usage and ecosystem
François-Guillaume Ribreau
 
PDF
Automatic constraints as a team maturity accelerator for startups
François-Guillaume Ribreau
 
PDF
Les enjeux de l'information et de l'algorithmique dans notre société
François-Guillaume Ribreau
 
PDF
How I monitor SaaS products
François-Guillaume Ribreau
 
PDF
Continous Integration of (JS) projects & check-build philosophy
François-Guillaume Ribreau
 
PDF
Introduction to Redis
François-Guillaume Ribreau
 
PDF
Approfondissement CSS3
François-Guillaume Ribreau
 
PDF
Découverte HTML5/CSS3
François-Guillaume Ribreau
 
REX LEAN- Créer un SaaS et être rentable après 6 mois
François-Guillaume Ribreau
 
⛳️ Votre API passe-t-elle le contrôle technique ?
François-Guillaume Ribreau
 
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...
François-Guillaume Ribreau
 
He stopped using for/while loops, you won't believe what happened next!
François-Guillaume Ribreau
 
Une plateforme moderne pour le groupe SIPA/Ouest-France 
François-Guillaume Ribreau
 
[BreizhCamp, format 15min] Construire et automatiser l'ecosystème de son Saa...
François-Guillaume Ribreau
 
[BreizhCamp, format 15min] Une api rest et GraphQL sans code grâce à PostgR...
François-Guillaume Ribreau
 
RedisConf 2016 - Redis usage and ecosystem
François-Guillaume Ribreau
 
Automatic constraints as a team maturity accelerator for startups
François-Guillaume Ribreau
 
Les enjeux de l'information et de l'algorithmique dans notre société
François-Guillaume Ribreau
 
How I monitor SaaS products
François-Guillaume Ribreau
 
Continous Integration of (JS) projects & check-build philosophy
François-Guillaume Ribreau
 
Introduction to Redis
François-Guillaume Ribreau
 
Approfondissement CSS3
François-Guillaume Ribreau
 
Découverte HTML5/CSS3
François-Guillaume Ribreau
 
Ad

Recently uploaded (20)

PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
July Patch Tuesday
Ivanti
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 

Implementing pattern-matching in JavaScript (short version)