SlideShare a Scribd company logo
Functional Programming
in TypeScript
: @binDebug
binDebug WorkSpace
hello@binDebug.io
Agenda
What is
Functional
Programming?
3 Tenets of
Functional
Programming
Features for
Functional
Programming in
TypeScript
What is Functional Programming?
The Functional Paradigm Buzzwords
 Immutability, or Purity.
 Higher Order Functions, or Functions as First-class citizens.
 Pipelining.
 Map-Reduce
 Expressions.
 Tail-call optimizations (available in ECMA 2015).
 Parallelization.
 Lazy evaluation.
 Determinism, or Predictability.
“
”
OO makes code understandable
by encapsulating moving parts.
FP makes code understandable
by minimizing moving parts.
https://twitter.com/mfeathers/status/29581296216
- Michael Feathers
What exactly are “moving parts” here?
 The disposition of a functional system is to change its state. If a system is
functional, then its state, for the most part, is changing.
 Managing the state of an application while structuring it is one of the more
difficult things in software development. This is especially true when the
application can execute code asynchronously.
 There are two parts to code we write –
 Code that computes results (Business Logic)
 Code that performs actions (Persistence Logic)
 Any and every code that contributes towards the changing of state is a
“moving” part.
And how have we dealt with them?
 In the beginning of time there was the procedural world, where everything
was mixed.
 The Objected oriented paradigm brought attention to the individuality of
entities, and thus focused on encapsulation of the “moving” parts.
 Functional Programming, which is in many ways a form of Declarative
Programming, encourages us to focus on computing results rather than
performing actions, thus minimizing the number of “moving” parts, i.e. the
parts that cause the state of the system to change.
A Guide Rope
 Immutable data, first class functions and tail call optimization are language
features that aid functional programming.
 Mapping, reducing, pipelining, recursing, currying and the use of higher order
functions are programming techniques used to write functional code.
 Parallelization, lazy evaluation and determinism are advantageous properties
of functional programs.
The idea is…
 Absence of side effects, i.e. our code doesn’t rely on data outside the current
function, and it doesn’t change data that exists outside the current function.
 Make our code more readable, and thus more maintainable.
Further reading:
Mary Rose Cook: A practical introduction to Functional Programming.
http://bit.ly/2hDmuEs
Three Tenets
Tame Side Effects
In Software parlance a side effect is anything that happens to the state of the
system when invoking a function.
The state of a system is shared. So be careful.
Side effects imply dependency, thus difficult to test. That is why mocks exist.
Purely Functional are those functions not allowed to alter state.
System thus becomes predictable. Unit testing is all that much easier.
A natural corollary of pure functions is that since state isn’t being changed, they
are conducive for parallel processing.
Expressions and Statements
Anything that evaluates to a value is referred to as an expression.
2+2 is an expression.
10 > 4 is an expression.
1 === 1 is an expression.
All expressions in JavaScript will return a value and that value will have a type. A
value’s type tells us what kind of thing it is.
A statement is any set of expressions that has an impact on the program in such
that it alters the state. For e.g. assigning a value to a variable, updating the
database, writing to a log file.
Statements are executed to cause some effect. Expressions are executed for
their result. Statements usually update the state. Expressions return a value.
Higher Order Function
Function that does at least one of the following:
accept another function(s) as parameter(s),
and return a function as a result.
Higher order functions enable composition of functionality not through complex
inheritance structures, but through substituting behaviour according to a strict
definition – the function signature.
Suppose we want to double the numbers in an array, and throw away the even
numbers. (Code source: http://bit.ly/2ptSnaV)
The non-functional way:
The functional way:
Purity of Function
https://staltz.com/is-your-javascript-function-actually-pure.html
Is this pure or impure?
Understanding ES5, ES2015 and
TypeScript
 ES5 is what most of us have used for years.
 The new version of JavaScript is ES2015, also known as “ES6”, and offers a
multitude of new features around scoping, modules, arrow functions etc. ES6
is not widely supported in today's browsers, so it needs to be transpiled to
ES5.
 TypeScript is a superset of ES6, which means all ES6 features are part of
TypeScript, but not all TypeScript features are part of ES6.
Evolution of TypeScript
 In the 4 years since it went from an internal to a public open source, open
design project, the combination of static type checking, transpiling and
tooling in TypeScript has become something of a behind-the-scenes success,
especially on large projects.
 Like with C# and the .NET framework, one would expect Microsoft projects
like Visual Studio Code (and the Office Online apps) to be written in
TypeScript. But now, even frameworks like Angular and Ionic have been
written in TypeScript.
Features for Functional Programming in
TypeScript
The Functional TypeScript
TypeScript itself is written entirely in functional style code with no classes at all.
 Spread/Rest Operators.
 Lambda expressions (also introduced in ES2015).
 Tagged Union Types.
 Type Safety (duh!).
 Type Inference.
Spread/Rest Operator
A Spread syntax allows in-place expansion of an expression for the following
cases:
1. Array
2. Function call
3. Multiple variable destructuring
Rest parameters works in the opposite direction of the spread syntax, it collects
an indefinite number of comma separated expressions into an array.
Spread/Rest Operator: Syntax
function add(...numbers) {
return numbers[0] + numbers[1];
}
add(3, 2);
Angular 2 Pipes: PipeTransform Interface
Angular 2 Pipes: A Pipe Implementation
Lambda expressions
Or the fat arrow. “=>”
JavaScript does offer anonymous functions, but then we do have to write the
function keyword a lot.
Lambda concentrates on the expressions at the soul of a function, thus making
the code more readable.
So what was
Is now
Union Type
A union type specifies several valid types for a value. If we have a value that has
a union type, we can only access members that are common to all types in the
union.
For example, Car | Plane | Tank can only be a car or a plane or a tank.
This offers a typesafe alternative to every time we use the any keyword when we
do not know the type of a variable at design time.
We use the vertical bar (|) to separate each type.
Essentially,
function MyCar(): Car | Plane | Tank {
//Can be a car or a Plane or a Tank.
//Who am I kidding, it can only be a car.
}
Tagged Union Types
 Tagged Union Types, which in other functional programming languages are
referred to as Discriminant Union Types, is a Union Type whose member types
all define a discriminant property of a literal type.
Type Inference
 TypeScript does not need you to explicitly describe types at every possible
opportunity.
 TypeScript has a rich type inference system that will "fill in the blanks" for us.
let numbers = [2, 3, 5, 7, 11];
numbers = ['this will generate a type error'];
 Type inference can also work through context, which is handy with callbacks.
You, the developer!
Last, but not the least, we are the biggest feature as far as Functional aspects
go.
Thinking of in terms of what to do, of composability, of computing, and
abstracting out persistence is really an approach. Languages merely offer the
tools.
And that is a wrap!
Q&A

More Related Content

What's hot (20)

TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
Sander Mak (@Sander_Mak)
 
Getting started with typescript
Getting started with typescriptGetting started with typescript
Getting started with typescript
C...L, NESPRESSO, WAFAASSURANCE, SOFRECOM ORANGE
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
Aniruddha Chakrabarti
 
Introducing TypeScript
Introducing TypeScriptIntroducing TypeScript
Introducing TypeScript
Microsoft Developer Network (MSDN) - Belgium and Luxembourg
 
TypeScript Best Practices
TypeScript Best PracticesTypeScript Best Practices
TypeScript Best Practices
felixbillon
 
Typescript Fundamentals
Typescript FundamentalsTypescript Fundamentals
Typescript Fundamentals
Sunny Sharma
 
Typescript ppt
Typescript pptTypescript ppt
Typescript ppt
akhilsreyas
 
TypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponTypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret Weapon
Laurent Duveau
 
TypeScript Presentation
TypeScript PresentationTypeScript Presentation
TypeScript Presentation
Patrick John Pacaña
 
Learning typescript
Learning typescriptLearning typescript
Learning typescript
Alexandre Marreiros
 
TypeScript
TypeScriptTypeScript
TypeScript
Oswald Campesato
 
Typescript in 30mins
Typescript in 30mins Typescript in 30mins
Typescript in 30mins
Udaya Kumar
 
TypeScript intro
TypeScript introTypeScript intro
TypeScript intro
Ats Uiboupin
 
Typescript Basics
Typescript BasicsTypescript Basics
Typescript Basics
Manikandan [M M K]
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!
Alessandro Giorgetti
 
Typescript: enjoying large scale browser development
Typescript: enjoying large scale browser developmentTypescript: enjoying large scale browser development
Typescript: enjoying large scale browser development
Joost de Vries
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practices
Iwan van der Kleijn
 
TypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideTypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation Guide
Nascenia IT
 
AngularConf2015
AngularConf2015AngularConf2015
AngularConf2015
Alessandro Giorgetti
 
Getting Started with TypeScript
Getting Started with TypeScriptGetting Started with TypeScript
Getting Started with TypeScript
Gil Fink
 
TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the pain
Sander Mak (@Sander_Mak)
 
TypeScript Best Practices
TypeScript Best PracticesTypeScript Best Practices
TypeScript Best Practices
felixbillon
 
Typescript Fundamentals
Typescript FundamentalsTypescript Fundamentals
Typescript Fundamentals
Sunny Sharma
 
TypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponTypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret Weapon
Laurent Duveau
 
Typescript in 30mins
Typescript in 30mins Typescript in 30mins
Typescript in 30mins
Udaya Kumar
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!
Alessandro Giorgetti
 
Typescript: enjoying large scale browser development
Typescript: enjoying large scale browser developmentTypescript: enjoying large scale browser development
Typescript: enjoying large scale browser development
Joost de Vries
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practices
Iwan van der Kleijn
 
TypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideTypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation Guide
Nascenia IT
 
Getting Started with TypeScript
Getting Started with TypeScriptGetting Started with TypeScript
Getting Started with TypeScript
Gil Fink
 

Viewers also liked (6)

The CNCF on Serverless
The CNCF on ServerlessThe CNCF on Serverless
The CNCF on Serverless
Daniel Krook
 
Microservices and Redis #redisconf Keynote
Microservices and Redis #redisconf KeynoteMicroservices and Redis #redisconf Keynote
Microservices and Redis #redisconf Keynote
Chris Richardson
 
#hacksummit 2016 - event-driven microservices – Events on the outside, on the...
#hacksummit 2016 - event-driven microservices – Events on the outside, on the...#hacksummit 2016 - event-driven microservices – Events on the outside, on the...
#hacksummit 2016 - event-driven microservices – Events on the outside, on the...
Chris Richardson
 
Microservices + Events + Docker = A Perfect Trio (dockercon)
Microservices + Events + Docker = A Perfect Trio (dockercon)Microservices + Events + Docker = A Perfect Trio (dockercon)
Microservices + Events + Docker = A Perfect Trio (dockercon)
Chris Richardson
 
Developing microservices with aggregates (SpringOne platform, #s1p)
Developing microservices with aggregates (SpringOne platform, #s1p)Developing microservices with aggregates (SpringOne platform, #s1p)
Developing microservices with aggregates (SpringOne platform, #s1p)
Chris Richardson
 
Handling Eventual Consistency in JVM Microservices with Event Sourcing (javao...
Handling Eventual Consistency in JVM Microservices with Event Sourcing (javao...Handling Eventual Consistency in JVM Microservices with Event Sourcing (javao...
Handling Eventual Consistency in JVM Microservices with Event Sourcing (javao...
Chris Richardson
 
The CNCF on Serverless
The CNCF on ServerlessThe CNCF on Serverless
The CNCF on Serverless
Daniel Krook
 
Microservices and Redis #redisconf Keynote
Microservices and Redis #redisconf KeynoteMicroservices and Redis #redisconf Keynote
Microservices and Redis #redisconf Keynote
Chris Richardson
 
#hacksummit 2016 - event-driven microservices – Events on the outside, on the...
#hacksummit 2016 - event-driven microservices – Events on the outside, on the...#hacksummit 2016 - event-driven microservices – Events on the outside, on the...
#hacksummit 2016 - event-driven microservices – Events on the outside, on the...
Chris Richardson
 
Microservices + Events + Docker = A Perfect Trio (dockercon)
Microservices + Events + Docker = A Perfect Trio (dockercon)Microservices + Events + Docker = A Perfect Trio (dockercon)
Microservices + Events + Docker = A Perfect Trio (dockercon)
Chris Richardson
 
Developing microservices with aggregates (SpringOne platform, #s1p)
Developing microservices with aggregates (SpringOne platform, #s1p)Developing microservices with aggregates (SpringOne platform, #s1p)
Developing microservices with aggregates (SpringOne platform, #s1p)
Chris Richardson
 
Handling Eventual Consistency in JVM Microservices with Event Sourcing (javao...
Handling Eventual Consistency in JVM Microservices with Event Sourcing (javao...Handling Eventual Consistency in JVM Microservices with Event Sourcing (javao...
Handling Eventual Consistency in JVM Microservices with Event Sourcing (javao...
Chris Richardson
 
Ad

Similar to Functional programming in TypeScript (20)

Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
Dave Fancher
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNext
Unfold UI
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
Back to the Future with TypeScript
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScript
Aleš Najmann
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScript
ChengHui Weng
 
Introduction to Functional Programming (w/ JS)
Introduction to Functional Programming (w/ JS)Introduction to Functional Programming (w/ JS)
Introduction to Functional Programming (w/ JS)
Allan Marques Baptista
 
TYPESCRIPT.pdfgshshhsjajajsjsjjsjajajjajjj
TYPESCRIPT.pdfgshshhsjajajsjsjjsjajajjajjjTYPESCRIPT.pdfgshshhsjajajsjsjjsjajajjajjj
TYPESCRIPT.pdfgshshhsjajajsjsjjsjajajjajjj
sonidsxyz02
 
Complete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptComplete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScript
EPAM Systems
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
Alfonso Peletier
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
Kamel Ouzouigh
 
Typescript: Beginner to Advanced
Typescript: Beginner to AdvancedTypescript: Beginner to Advanced
Typescript: Beginner to Advanced
Talentica Software
 
379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogramming
Luis Atencio
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
Daniele Pozzobon
 
Functional programming
Functional programmingFunctional programming
Functional programming
S M Asaduzzaman
 
Dev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingDev Concepts: Functional Programming
Dev Concepts: Functional Programming
Svetlin Nakov
 
Gabriele Petronella - FP for front-end development: should you care? - Codemo...
Gabriele Petronella - FP for front-end development: should you care? - Codemo...Gabriele Petronella - FP for front-end development: should you care? - Codemo...
Gabriele Petronella - FP for front-end development: should you care? - Codemo...
Codemotion
 
Functional Programming with Javascript
Functional Programming with JavascriptFunctional Programming with Javascript
Functional Programming with Javascript
Deepankar Chopra
 
TypeScript Interview Questions PDF By ScholarHat
TypeScript Interview Questions PDF By ScholarHatTypeScript Interview Questions PDF By ScholarHat
TypeScript Interview Questions PDF By ScholarHat
Scholarhat
 
Grant Rogerson SDEC2015
Grant Rogerson SDEC2015Grant Rogerson SDEC2015
Grant Rogerson SDEC2015
Grant Rogerson
 
Typescript language extension of java script
Typescript language extension of java scriptTypescript language extension of java script
Typescript language extension of java script
michaelaaron25322
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
Dave Fancher
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNext
Unfold UI
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
Back to the Future with TypeScript
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScript
Aleš Najmann
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScript
ChengHui Weng
 
Introduction to Functional Programming (w/ JS)
Introduction to Functional Programming (w/ JS)Introduction to Functional Programming (w/ JS)
Introduction to Functional Programming (w/ JS)
Allan Marques Baptista
 
TYPESCRIPT.pdfgshshhsjajajsjsjjsjajajjajjj
TYPESCRIPT.pdfgshshhsjajajsjsjjsjajajjajjjTYPESCRIPT.pdfgshshhsjajajsjsjjsjajajjajjj
TYPESCRIPT.pdfgshshhsjajajsjsjjsjajajjajjj
sonidsxyz02
 
Complete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScriptComplete Notes on Angular 2 and TypeScript
Complete Notes on Angular 2 and TypeScript
EPAM Systems
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
Alfonso Peletier
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
Kamel Ouzouigh
 
Typescript: Beginner to Advanced
Typescript: Beginner to AdvancedTypescript: Beginner to Advanced
Typescript: Beginner to Advanced
Talentica Software
 
379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogramming
Luis Atencio
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
Daniele Pozzobon
 
Dev Concepts: Functional Programming
Dev Concepts: Functional ProgrammingDev Concepts: Functional Programming
Dev Concepts: Functional Programming
Svetlin Nakov
 
Gabriele Petronella - FP for front-end development: should you care? - Codemo...
Gabriele Petronella - FP for front-end development: should you care? - Codemo...Gabriele Petronella - FP for front-end development: should you care? - Codemo...
Gabriele Petronella - FP for front-end development: should you care? - Codemo...
Codemotion
 
Functional Programming with Javascript
Functional Programming with JavascriptFunctional Programming with Javascript
Functional Programming with Javascript
Deepankar Chopra
 
TypeScript Interview Questions PDF By ScholarHat
TypeScript Interview Questions PDF By ScholarHatTypeScript Interview Questions PDF By ScholarHat
TypeScript Interview Questions PDF By ScholarHat
Scholarhat
 
Grant Rogerson SDEC2015
Grant Rogerson SDEC2015Grant Rogerson SDEC2015
Grant Rogerson SDEC2015
Grant Rogerson
 
Typescript language extension of java script
Typescript language extension of java scriptTypescript language extension of java script
Typescript language extension of java script
michaelaaron25322
 
Ad

Recently uploaded (20)

FME for Climate Data: Turning Big Data into Actionable Insights
FME for Climate Data: Turning Big Data into Actionable InsightsFME for Climate Data: Turning Big Data into Actionable Insights
FME for Climate Data: Turning Big Data into Actionable Insights
Safe Software
 
Plooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your wayPlooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your way
Plooma
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptxIMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
BradBedford3
 
dp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdfdp-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 OverlookCode and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
Artificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across IndustriesArtificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across Industries
SandeepKS52
 
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
How Insurance Policy Management Software Streamlines Operations
How Insurance Policy Management Software Streamlines OperationsHow Insurance Policy Management Software Streamlines Operations
How Insurance Policy Management Software Streamlines Operations
Insurance Tech Services
 
Maximizing Business Value with AWS Consulting Services.pdf
Maximizing Business Value with AWS Consulting Services.pdfMaximizing Business Value with AWS Consulting Services.pdf
Maximizing Business Value with AWS Consulting Services.pdf
Elena Mia
 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FMEIntegrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
 
FME as an Orchestration Tool - Peak of Data & AI 2025
FME as an Orchestration Tool - Peak of Data & AI 2025FME as an Orchestration Tool - Peak of Data & AI 2025
FME as an Orchestration Tool - Peak of Data & AI 2025
Safe Software
 
Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3
Gaurav Sharma
 
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdfThe Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
Varsha Nayak
 
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Natan Silnitsky
 
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free DownloadWondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
 
AI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA TechnologiesAI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA Technologies
SandeepKS52
 
Migrating to Azure Cosmos DB the Right Way
Migrating to Azure Cosmos DB the Right WayMigrating to Azure Cosmos DB the Right Way
Migrating to Azure Cosmos DB the Right Way
Alexander (Alex) Komyagin
 
Revolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management SoftwareRevolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management Software
Insurance Tech Services
 
FME for Climate Data: Turning Big Data into Actionable Insights
FME for Climate Data: Turning Big Data into Actionable InsightsFME for Climate Data: Turning Big Data into Actionable Insights
FME for Climate Data: Turning Big Data into Actionable Insights
Safe Software
 
Plooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your wayPlooma is a writing platform to plan, write, and shape books your way
Plooma is a writing platform to plan, write, and shape books your way
Plooma
 
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptxIMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
IMAGE CLASSIFICATION USING CONVOLUTIONAL NEURAL NETWORK.P.pptx
usmanch7829
 
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
Maintaining + Optimizing Database Health: Vendors, Orchestrations, Enrichment...
BradBedford3
 
dp-700 exam questions sample docume .pdf
dp-700 exam questions sample docume .pdfdp-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 OverlookCode and No-Code Journeys: The Coverage Overlook
Code and No-Code Journeys: The Coverage Overlook
Applitools
 
How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0How the US Navy Approaches DevSecOps with Raise 2.0
How the US Navy Approaches DevSecOps with Raise 2.0
Anchore
 
Artificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across IndustriesArtificial Intelligence Applications Across Industries
Artificial Intelligence Applications Across Industries
SandeepKS52
 
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Meet You in the Middle: 1000x Performance for Parquet Queries on PB-Scale Dat...
Alluxio, Inc.
 
How Insurance Policy Management Software Streamlines Operations
How Insurance Policy Management Software Streamlines OperationsHow Insurance Policy Management Software Streamlines Operations
How Insurance Policy Management Software Streamlines Operations
Insurance Tech Services
 
Maximizing Business Value with AWS Consulting Services.pdf
Maximizing Business Value with AWS Consulting Services.pdfMaximizing Business Value with AWS Consulting Services.pdf
Maximizing Business Value with AWS Consulting Services.pdf
Elena Mia
 
Integrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FMEIntegrating Survey123 and R&H Data Using FME
Integrating Survey123 and R&H Data Using FME
Safe Software
 
FME as an Orchestration Tool - Peak of Data & AI 2025
FME as an Orchestration Tool - Peak of Data & AI 2025FME as an Orchestration Tool - Peak of Data & AI 2025
FME as an Orchestration Tool - Peak of Data & AI 2025
Safe Software
 
Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3Software Engineering Process, Notation & Tools Introduction - Part 3
Software Engineering Process, Notation & Tools Introduction - Part 3
Gaurav Sharma
 
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdfThe Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
The Future of Open Source Reporting Best Alternatives to Jaspersoft.pdf
Varsha Nayak
 
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Async-ronizing Success at Wix - Patterns for Seamless Microservices - Devoxx ...
Natan Silnitsky
 
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free DownloadWondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Wondershare PDFelement Pro 11.4.20.3548 Crack Free Download
Puppy jhon
 
AI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA TechnologiesAI and Deep Learning with NVIDIA Technologies
AI and Deep Learning with NVIDIA Technologies
SandeepKS52
 
Revolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management SoftwareRevolutionize Your Insurance Workflow with Claims Management Software
Revolutionize Your Insurance Workflow with Claims Management Software
Insurance Tech Services
 

Functional programming in TypeScript

  • 2. Agenda What is Functional Programming? 3 Tenets of Functional Programming Features for Functional Programming in TypeScript
  • 3. What is Functional Programming?
  • 4. The Functional Paradigm Buzzwords  Immutability, or Purity.  Higher Order Functions, or Functions as First-class citizens.  Pipelining.  Map-Reduce  Expressions.  Tail-call optimizations (available in ECMA 2015).  Parallelization.  Lazy evaluation.  Determinism, or Predictability.
  • 5. “ ” OO makes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts. https://twitter.com/mfeathers/status/29581296216 - Michael Feathers
  • 6. What exactly are “moving parts” here?  The disposition of a functional system is to change its state. If a system is functional, then its state, for the most part, is changing.  Managing the state of an application while structuring it is one of the more difficult things in software development. This is especially true when the application can execute code asynchronously.  There are two parts to code we write –  Code that computes results (Business Logic)  Code that performs actions (Persistence Logic)  Any and every code that contributes towards the changing of state is a “moving” part.
  • 7. And how have we dealt with them?  In the beginning of time there was the procedural world, where everything was mixed.  The Objected oriented paradigm brought attention to the individuality of entities, and thus focused on encapsulation of the “moving” parts.  Functional Programming, which is in many ways a form of Declarative Programming, encourages us to focus on computing results rather than performing actions, thus minimizing the number of “moving” parts, i.e. the parts that cause the state of the system to change.
  • 8. A Guide Rope  Immutable data, first class functions and tail call optimization are language features that aid functional programming.  Mapping, reducing, pipelining, recursing, currying and the use of higher order functions are programming techniques used to write functional code.  Parallelization, lazy evaluation and determinism are advantageous properties of functional programs.
  • 9. The idea is…  Absence of side effects, i.e. our code doesn’t rely on data outside the current function, and it doesn’t change data that exists outside the current function.  Make our code more readable, and thus more maintainable. Further reading: Mary Rose Cook: A practical introduction to Functional Programming. http://bit.ly/2hDmuEs
  • 11. Tame Side Effects In Software parlance a side effect is anything that happens to the state of the system when invoking a function. The state of a system is shared. So be careful. Side effects imply dependency, thus difficult to test. That is why mocks exist. Purely Functional are those functions not allowed to alter state. System thus becomes predictable. Unit testing is all that much easier. A natural corollary of pure functions is that since state isn’t being changed, they are conducive for parallel processing.
  • 12. Expressions and Statements Anything that evaluates to a value is referred to as an expression. 2+2 is an expression. 10 > 4 is an expression. 1 === 1 is an expression. All expressions in JavaScript will return a value and that value will have a type. A value’s type tells us what kind of thing it is. A statement is any set of expressions that has an impact on the program in such that it alters the state. For e.g. assigning a value to a variable, updating the database, writing to a log file. Statements are executed to cause some effect. Expressions are executed for their result. Statements usually update the state. Expressions return a value.
  • 13. Higher Order Function Function that does at least one of the following: accept another function(s) as parameter(s), and return a function as a result. Higher order functions enable composition of functionality not through complex inheritance structures, but through substituting behaviour according to a strict definition – the function signature.
  • 14. Suppose we want to double the numbers in an array, and throw away the even numbers. (Code source: http://bit.ly/2ptSnaV) The non-functional way: The functional way:
  • 16. Is this pure or impure?
  • 17. Understanding ES5, ES2015 and TypeScript  ES5 is what most of us have used for years.  The new version of JavaScript is ES2015, also known as “ES6”, and offers a multitude of new features around scoping, modules, arrow functions etc. ES6 is not widely supported in today's browsers, so it needs to be transpiled to ES5.  TypeScript is a superset of ES6, which means all ES6 features are part of TypeScript, but not all TypeScript features are part of ES6.
  • 18. Evolution of TypeScript  In the 4 years since it went from an internal to a public open source, open design project, the combination of static type checking, transpiling and tooling in TypeScript has become something of a behind-the-scenes success, especially on large projects.  Like with C# and the .NET framework, one would expect Microsoft projects like Visual Studio Code (and the Office Online apps) to be written in TypeScript. But now, even frameworks like Angular and Ionic have been written in TypeScript.
  • 19. Features for Functional Programming in TypeScript
  • 20. The Functional TypeScript TypeScript itself is written entirely in functional style code with no classes at all.  Spread/Rest Operators.  Lambda expressions (also introduced in ES2015).  Tagged Union Types.  Type Safety (duh!).  Type Inference.
  • 21. Spread/Rest Operator A Spread syntax allows in-place expansion of an expression for the following cases: 1. Array 2. Function call 3. Multiple variable destructuring Rest parameters works in the opposite direction of the spread syntax, it collects an indefinite number of comma separated expressions into an array.
  • 22. Spread/Rest Operator: Syntax function add(...numbers) { return numbers[0] + numbers[1]; } add(3, 2);
  • 23. Angular 2 Pipes: PipeTransform Interface
  • 24. Angular 2 Pipes: A Pipe Implementation
  • 25. Lambda expressions Or the fat arrow. “=>” JavaScript does offer anonymous functions, but then we do have to write the function keyword a lot. Lambda concentrates on the expressions at the soul of a function, thus making the code more readable. So what was Is now
  • 26. Union Type A union type specifies several valid types for a value. If we have a value that has a union type, we can only access members that are common to all types in the union. For example, Car | Plane | Tank can only be a car or a plane or a tank. This offers a typesafe alternative to every time we use the any keyword when we do not know the type of a variable at design time. We use the vertical bar (|) to separate each type. Essentially, function MyCar(): Car | Plane | Tank { //Can be a car or a Plane or a Tank. //Who am I kidding, it can only be a car. }
  • 27. Tagged Union Types  Tagged Union Types, which in other functional programming languages are referred to as Discriminant Union Types, is a Union Type whose member types all define a discriminant property of a literal type.
  • 28. Type Inference  TypeScript does not need you to explicitly describe types at every possible opportunity.  TypeScript has a rich type inference system that will "fill in the blanks" for us. let numbers = [2, 3, 5, 7, 11]; numbers = ['this will generate a type error'];  Type inference can also work through context, which is handy with callbacks.
  • 29. You, the developer! Last, but not the least, we are the biggest feature as far as Functional aspects go. Thinking of in terms of what to do, of composability, of computing, and abstracting out persistence is really an approach. Languages merely offer the tools.
  • 30. And that is a wrap! Q&A