SlideShare a Scribd company logo
1 of 30
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

TypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painTypeScript: coding JavaScript without the pain
TypeScript: coding JavaScript without the painSander Mak (@Sander_Mak)
 
TypeScript Best Practices
TypeScript Best PracticesTypeScript Best Practices
TypeScript Best Practicesfelixbillon
 
Typescript Fundamentals
Typescript FundamentalsTypescript Fundamentals
Typescript FundamentalsSunny Sharma
 
TypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponTypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponLaurent 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 developmentJoost de Vries
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practicesIwan 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 GuideNascenia IT
 
Getting Started with TypeScript
Getting Started with TypeScriptGetting Started with TypeScript
Getting Started with TypeScriptGil Fink
 

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
 
Getting started with typescript
Getting started with typescriptGetting started with typescript
Getting started with typescript
 
TypeScript Overview
TypeScript OverviewTypeScript Overview
TypeScript Overview
 
Introducing TypeScript
Introducing TypeScriptIntroducing TypeScript
Introducing TypeScript
 
TypeScript Best Practices
TypeScript Best PracticesTypeScript Best Practices
TypeScript Best Practices
 
Typescript Fundamentals
Typescript FundamentalsTypescript Fundamentals
Typescript Fundamentals
 
Typescript ppt
Typescript pptTypescript ppt
Typescript ppt
 
TypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret WeaponTypeScript: Angular's Secret Weapon
TypeScript: Angular's Secret Weapon
 
TypeScript Presentation
TypeScript PresentationTypeScript Presentation
TypeScript Presentation
 
Learning typescript
Learning typescriptLearning typescript
Learning typescript
 
TypeScript
TypeScriptTypeScript
TypeScript
 
Typescript in 30mins
Typescript in 30mins Typescript in 30mins
Typescript in 30mins
 
TypeScript intro
TypeScript introTypeScript intro
TypeScript intro
 
Typescript Basics
Typescript BasicsTypescript Basics
Typescript Basics
 
TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!TypeScript . the JavaScript developer best friend!
TypeScript . the JavaScript developer best friend!
 
Typescript: enjoying large scale browser development
Typescript: enjoying large scale browser developmentTypescript: enjoying large scale browser development
Typescript: enjoying large scale browser development
 
Type script - advanced usage and practices
Type script  - advanced usage and practicesType script  - advanced usage and practices
Type script - advanced usage and practices
 
TypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation GuideTypeScript: Basic Features and Compilation Guide
TypeScript: Basic Features and Compilation Guide
 
AngularConf2015
AngularConf2015AngularConf2015
AngularConf2015
 
Getting Started with TypeScript
Getting Started with TypeScriptGetting Started with TypeScript
Getting Started with TypeScript
 

Viewers also liked

The CNCF on Serverless
The CNCF on ServerlessThe CNCF on Serverless
The CNCF on ServerlessDaniel Krook
 
Microservices and Redis #redisconf Keynote
Microservices and Redis #redisconf KeynoteMicroservices and Redis #redisconf Keynote
Microservices and Redis #redisconf KeynoteChris 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
 

Viewers also liked (6)

The CNCF on Serverless
The CNCF on ServerlessThe CNCF on Serverless
The CNCF on Serverless
 
Microservices and Redis #redisconf Keynote
Microservices and Redis #redisconf KeynoteMicroservices and Redis #redisconf Keynote
Microservices and Redis #redisconf Keynote
 
#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...
 
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)
 
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)
 
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...
 

Similar to Functional programming in TypeScript

Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#Riccardo Terrell
 
Book management system
Book management systemBook management system
Book management systemSHARDA SHARAN
 
AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1guest38bf
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diarySHARDA SHARAN
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsRuth Marvin
 
Contact management system
Contact management systemContact management system
Contact management systemSHARDA SHARAN
 
over all view programming to computer
over all view programming to computer over all view programming to computer
over all view programming to computer muniryaseen
 
379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogrammingLuis Atencio
 
Language design and translation issues
Language design and translation issuesLanguage design and translation issues
Language design and translation issuesSURBHI SAROHA
 
Vendredi Tech_ la programmation fonctionnelle.pptx
Vendredi Tech_ la programmation fonctionnelle.pptxVendredi Tech_ la programmation fonctionnelle.pptx
Vendredi Tech_ la programmation fonctionnelle.pptxGuillaume Saint Etienne
 
The Design, Evolution and Use of KernelF
The Design, Evolution and Use of KernelFThe Design, Evolution and Use of KernelF
The Design, Evolution and Use of KernelFMarkus Voelter
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving TechniquesAshesh R
 
S D D Program Development Tools
S D D  Program  Development  ToolsS D D  Program  Development  Tools
S D D Program Development Toolsgavhays
 
4 coding from algorithms
4 coding from algorithms4 coding from algorithms
4 coding from algorithmshccit
 

Similar to Functional programming in TypeScript (20)

Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#
 
C question
C questionC question
C question
 
Training 8051Report
Training 8051ReportTraining 8051Report
Training 8051Report
 
Sdlc
SdlcSdlc
Sdlc
 
Sdlc
SdlcSdlc
Sdlc
 
Book management system
Book management systemBook management system
Book management system
 
AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1
 
Password protected diary
Password protected diaryPassword protected diary
Password protected diary
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 
Contact management system
Contact management systemContact management system
Contact management system
 
over all view programming to computer
over all view programming to computer over all view programming to computer
over all view programming to computer
 
379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogramming
 
Language design and translation issues
Language design and translation issuesLanguage design and translation issues
Language design and translation issues
 
Vendredi Tech_ la programmation fonctionnelle.pptx
Vendredi Tech_ la programmation fonctionnelle.pptxVendredi Tech_ la programmation fonctionnelle.pptx
Vendredi Tech_ la programmation fonctionnelle.pptx
 
The Design, Evolution and Use of KernelF
The Design, Evolution and Use of KernelFThe Design, Evolution and Use of KernelF
The Design, Evolution and Use of KernelF
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
 
S D D Program Development Tools
S D D  Program  Development  ToolsS D D  Program  Development  Tools
S D D Program Development Tools
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
C interview questions
C interview  questionsC interview  questions
C interview questions
 
4 coding from algorithms
4 coding from algorithms4 coding from algorithms
4 coding from algorithms
 

Recently uploaded

WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2
 
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2
 
WSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfAzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfryanfarris8
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 

Recently uploaded (20)

WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
 
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million PeopleWSO2Con2024 - Unleashing the Financial Potential of 13 Million People
WSO2Con2024 - Unleashing the Financial Potential of 13 Million People
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
WSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in UgandaWSO2CON 2024 - Building a Digital Government in Uganda
WSO2CON 2024 - Building a Digital Government in Uganda
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
WSO2CON 2024 - Not Just Microservices: Rightsize Your Services!
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
WSO2CON 2024 - Unlocking the Identity: Embracing CIAM 2.0 for a Competitive A...
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdfAzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
AzureNativeQumulo_HPC_Cloud_Native_Benchmarks.pdf
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
WSO2Con2024 - Simplified Integration: Unveiling the Latest Features in WSO2 L...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 

Functional programming in TypeScript

  • 1. Functional Programming in TypeScript : @binDebug binDebug WorkSpace hello@binDebug.io
  • 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