SlideShare uma empresa Scribd logo
1 de 28
An Introduction to
Functional Programming
With Javascript, the world’s most
popular (functional) language
Outline
•
•
•
•

What is functional programming?
Why should we learn it?
What features are necessary to support it?
What patterns are common?
– Underscore/Lo-Dash

• Examples
• Future topics
What is Functional Programming?

A programming paradigm/style where the
basic unit of abstraction is the function
Motivation for Learning
•
•
•
•
•

Become a better programmer
Be prepared for future technologies
Understand KVDB
Avoid defects
Write clearer code
FP Ciriculum
• Tuples
• Lists
• Handling SideEffects
• Closure
• Nested Functions
• First-Class
Functions
• Higher-Order
Functions
• Anonymous
Functions

• Tail Recursion
• Folding
• Partially-Applied
Functions
• Immutability
• Memoization
• Function
Composition
• Pattern Matching
• Currying
• Generics
• Lazy Evaluation

• Delimited
Continuations
• Lambda Calculus
• Higher Kinds
• Monads
• Monad
Comprehension
• State-Passing
Style
FP Ciriculum
• Tuples
•
• Lists
•
• Handling Side•
Effects
• Lexical Closure •
• Nested Functions •
• First-Class
•
Functions
• Higher-Order
•
Functions
•
• Anonymous/
•
Literal Functions •

Tail Recursion
Folding
Partially-Applied
Functions
Immutability
Memoization
Function
Composition
Pattern Matching
Currying
Generics
Lazy Evaluation

• Delimited
Continuations
• Lambda Calculus
• Higher Kinds
• Monads
• Monad
Comprehension
• State-Passing
Style
Functional Style
Imperative/OO

Functional

Tell the computer what to do

Describe what you want done

Abstraction via objects/
data structures

Abstraction via functions

Re-use via classes/inheritence

Re-use via composition

Iteration/looping

Recursion

function factorial(n) {
var result = 1;
while (n > 1) {
result *= n;
n--;
}
return result;
}

function factorial(n) {
if (n < 2) return 1;
else return
n * factorial(n - 1);
}
Mandatory FP Features
•
•
•
•

Anonymous functions / function literals (λ)
First-class functions
Lexical closure
Higher-order functions
Why Javascript?
•
•
•
•

Fully supports the functional paradigm
Javascript not well understood by users
Benefits of FP align well with JS users’ needs
Guide us down a better path
Anonymous Functions /
Function Literals

x = function(a) {
// ... do something with ‘a’ ...
}
// typeof x === ‘function’
First-Class Functions
var mathFuncs =
function(a)
function(b)
function(c)
];

[
{ return a * 2 },
{ return b * b },
{ return c + 2 }

mathFuncs[1](3); // => 9
Lexical Closure
var complete = false;
function doSomethingOnce() {
if (!complete) {
// ... something
complete = true;
}
}
Higher-Order Functions
function doTwice(action) {
action();
action();
}
doTwice(function() {
console.log('called!');
})
Higher-Order Functions
function multiplyBy(num) {
return function(i) {
return i * num;
}
}
var times10 = multiplyBy(10);
times10(5); // 50
Higher-Order Functions
function map(array, func) {
var result = [];
for (var i = 0; i < array.length; i++) {
result[i] = func(array[i]);
}
return result;
}
function multiplyBy(num) { ... }
var array = map([1,2,3], multiplyBy(10));
Fold/Reduce
var sum = fold(
0,
[1, 2, 3, 4],
function(value, item) {
return value + item;
}
); // sum === 10
Fold/Reduce
Parameters to function
Value
Item
0 (initial) 1
1
2
3
3
6
4

Returns
0+1=1
1+2=3
3+3=6
6 + 4 = 10

Remains
[2, 3, 4]
[3, 4]
[4]
[]
Fold/Reduce
function fold(init, array, folder) {
var result = init;
for (var i = 0; i < array.length; i++) {
var curr = array[i];
result = folder(result, curr);
}
return result;
}
Map as Fold
function map(array, func) {
return foldLeft([], array,
function(a, x) {
a.push(func(x));
return a;
});
}
var a = map([1,2,3], multiplyBy(10));
Map as Fold
Parameters to function
Value
Item
[] (initial)
1
[10]
2
[10, 20]
3

Returns
[10]
[10, 20]
[10, 20, 30]

Remains
[2, 3]
[3]
[]
Data Encapsulation through Closure
function makeCounter() {
var i = 0;
return function() {
i++;
return i;
}
}
var counter = makeCounter();
counter(); // 1
counter(); // 2
Underscore.js
“... the tie to go along with jQuery's tux,
and Backbone.js's suspenders.”
-- http://underscorejs.org
Underscore.js Basics
e.g.:
_.<method>(<array>, <function>)
var array = ["hello", "world"];
_.each(array, function(x) {
console.log(x);
});
Underscore.js Chaining
e.g.:
_.chain(<array>)
.<method1>(<function>)
.<method2>(<function>)
.value();
Underscore.js Chaining
_.chain([1, 2, 3, 4, 5])
.map(function(n) { return n * n })
// [1, 4, 9, 16, 25]
.filter(function(n) { return n % 2 === 0 })
// [4, 16]
.reduce(function(t, n) { return t + n }, 0)
// [20]
.value();
// 20
Underscore.js Chaining
_.chain([1, 2, 3, 4, 5])
.map(function(n) { return n * n })
.filter(function(n) { return n % 2 === 0 })
.reduce(function(t, n) { return t + n }, 0)
.value();
Next Steps
• Functional Javascript:
– http://shop.oreilly.com/product/0636920028857.do

• Literate underscore.js code:
– http://underscorejs.org/docs/underscore.html

• When you get bored with underscore:
– http://lodash.com/

• When something doesn’t make sense:
– JavaScript: The Good Parts
Questions?

Mais conteúdo relacionado

Mais procurados

Functional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekFunctional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekyoavrubin
 
Functional programming
Functional programmingFunctional programming
Functional programmingKibru Demeke
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScripttmont
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioLuis Atencio
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScriptWill Livengood
 
Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Reuven Lerner
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programmingKonrad Szydlo
 
Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Omar Abdelhafith
 
Practical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan HodorogPractical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan Hodorog3Pillar Global
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAdam Getchell
 
Function class in c++
Function class in c++Function class in c++
Function class in c++Kumar
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overridingRajab Ali
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional ProgrammingGeison Goes
 
Functional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smartFunctional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smartChen Fisher
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
 

Mais procurados (20)

Functional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekFunctional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks week
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis Atencio
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScript
 
Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programming
 
Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)Introduction to functional programming (In Arabic)
Introduction to functional programming (In Arabic)
 
Practical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan HodorogPractical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan Hodorog
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Scala functions
Scala functionsScala functions
Scala functions
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Function class in c++
Function class in c++Function class in c++
Function class in c++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional Programming
 
Functional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smartFunctional programming with Ruby - can make you look smart
Functional programming with Ruby - can make you look smart
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 

Semelhante a An Introduction to Functional Programming with Javascript

Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almostQuinton Sheppard
 
Functional programming and ruby in functional style
Functional programming and ruby in functional styleFunctional programming and ruby in functional style
Functional programming and ruby in functional styleNiranjan Sarade
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programmingDhaval Dalal
 
Fp for the oo programmer
Fp for the oo programmerFp for the oo programmer
Fp for the oo programmerShawn Button
 
Functional converter project
Functional converter projectFunctional converter project
Functional converter projectAmir Barylko
 
Functional programming in clojure
Functional programming in clojureFunctional programming in clojure
Functional programming in clojureJuan-Manuel Gimeno
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in rubyKoen Handekyn
 
The joy of functional programming
The joy of functional programmingThe joy of functional programming
The joy of functional programmingSteve Zhang
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScriptTroy Miles
 
Functional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersFunctional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersDiego Freniche Brito
 
Parallel and Async Programming With C#
Parallel and Async Programming With C#Parallel and Async Programming With C#
Parallel and Async Programming With C#Rainer Stropek
 
TechDaysNL 2015 - F# for C# Developers
TechDaysNL 2015 - F# for C# DevelopersTechDaysNL 2015 - F# for C# Developers
TechDaysNL 2015 - F# for C# DevelopersRonald Harmsen
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdflailoesakhan
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring ClojurescriptLuke Donnet
 
JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)Eduard Tomàs
 
JavaScript in 2016
JavaScript in 2016JavaScript in 2016
JavaScript in 2016Codemotion
 
Functional Programming - Past, Present and Future
Functional Programming - Past, Present and FutureFunctional Programming - Past, Present and Future
Functional Programming - Past, Present and FuturePushkar Kulkarni
 

Semelhante a An Introduction to Functional Programming with Javascript (20)

Awesomeness of JavaScript…almost
Awesomeness of JavaScript…almostAwesomeness of JavaScript…almost
Awesomeness of JavaScript…almost
 
Functional programming and ruby in functional style
Functional programming and ruby in functional styleFunctional programming and ruby in functional style
Functional programming and ruby in functional style
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programming
 
Fp for the oo programmer
Fp for the oo programmerFp for the oo programmer
Fp for the oo programmer
 
Functional converter project
Functional converter projectFunctional converter project
Functional converter project
 
Functional programming in clojure
Functional programming in clojureFunctional programming in clojure
Functional programming in clojure
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
 
The joy of functional programming
The joy of functional programmingThe joy of functional programming
The joy of functional programming
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScript
 
Functional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersFunctional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented Programmers
 
JS Essence
JS EssenceJS Essence
JS Essence
 
Parallel and Async Programming With C#
Parallel and Async Programming With C#Parallel and Async Programming With C#
Parallel and Async Programming With C#
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
 
TechDaysNL 2015 - F# for C# Developers
TechDaysNL 2015 - F# for C# DevelopersTechDaysNL 2015 - F# for C# Developers
TechDaysNL 2015 - F# for C# Developers
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring Clojurescript
 
JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)
 
JavaScript in 2016
JavaScript in 2016JavaScript in 2016
JavaScript in 2016
 
Functional Programming - Past, Present and Future
Functional Programming - Past, Present and FutureFunctional Programming - Past, Present and Future
Functional Programming - Past, Present and Future
 

Último

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 

Último (20)

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 

An Introduction to Functional Programming with Javascript

Notas do Editor

  1. associated with esoteric languages like Haskell, Scheme, Erlang, etc.Those are functional languages, but they aren’t the only road to functional programming.FP is a fundamental style (or paradigm) enabled by language features.Just as oo-languages use objects as the basic unit of abstraction, usually through classes.Just as we can encapsulate behaviour and data behind an object’s interface, using a functional language we can encapsulate behaviour and data in functions. Functions can also be treated as data.
  2. So what do we get by thinking in functions?Better: Functional paradigm can be applied to almost any language. E.g. CollectionUtil at MBC. LINQ in C# comes up a lot. Libraries available for non-functional languages.If you are working on a non-trivial web application, you should be using a functional programming. Learning FP will make you a better Javascript programmerFuture: Some people think that functional programming is going to be the next big paradigm, and the jump is ever bigger than the one from imperative to OO. So learning FP now will prepare you for that scenario.Kvdb: KVDB, the technology behind Protunity, uses Scala, a functional language.Defects: because functional programs tend to be composed of small functions, may of them built in, you leave fewer places for bugs to hide.
  3. L&amp;L part of a series of FP topics. Sorted by strength of headache you might get by looking at them.
  4. We’re starting out with these introductary topics, and skipping a few because Javascript either doesn’t directly support them, such as in the case oftuples and lists (JS only has arrays), and we only have an hour anyway.
  5. Aside from features, what do programs written in this style look like?
  6. What features must a language have to support programming in a functional style?Function literals: shorthand for creating anonymous or named function values.First-class: functions are just regular values. Anywhere regular values or expressions are allowed, functions can appear.Lexical closure: functions have access to surrounding scopes, even after it has finished executing.Higher-order: can be passed into and returned from other functionsWhen all of these features combine, you can do some really powerful stuff with functions.
  7. Functions are one of the things JS gets mostly right- history of JS: management said “make it look like Java or C” and Brendan Eich did so, but thankfully smuggled in FPJS is a functional programming language that almost all of us are forced to use.Not well understood: most of us learned from fragments. Find code snippets on stackoverflow that do what we wanted. - lots of frustration comes from broken JS features, but lots more comes from cross-browser issues with the DOM and CSS. Functions work consistently across browsers.Lots of Javascript out there is crap. Better to learn the principles than go looking online for examples.JS users’ needs: - maintainability: FP helps to abstract behaviour out into reusable chunks, which form libraries like underscore/lo-dashFunctional programming (especially via common libraries) helps you write understandable codeGuide: give programmers a task and a language and they generally say “okay, I know how I would implement this.” Not so with JS. There are no self-professed “Javascript programmers”, which is a shame.
  8. Lets you package up some code into a function and use it anywhere an expression might appear.
  9. Builds on the idea of a function literal: functions can be treated like data, stuffed into arrays, etc. and consistently invoked at any time.Functions are treated like any other values in the language. Especially true in Javascript, the language is based heavily on functions. Get sick of typing function all the time…Useful in building things like lists of event handlers (in case you wanted to sidestep the existing event model),
  10. Parens in red denote the scope of the function, but it can access ‘complete’ outside of it.The function is packaged up along with its environment, even after those variables have gone out of scope.
  11. Here’s where everything comes together and we get to build what are called higher-order functions:Defn: A function that either takes a function as a parameter or returns a function.Start with the first situation: function as parameter
  12. Example of returning a function from a function. We also make use of a closure in the inner function (num).multiplyBy returns a new function that multiplies its argument by a specific number.
  13. Let’s put these two together to create a new function: map. Map is common to many functional languages.Result is powerful, abstractions like map or multiplyBy that are easier to rid of bugs.
  14. Something a bit more powerful: fold, also known as reduce, takes an array and reduces it to a single value.Start with an initial value (e.g. 0), then apply the function to everything in the array and pass in the last returned value, then return the result.Comes in two flavours: left and right. Determines the order (left is forward, right is backward) in which the items are visited.
  15. Steps during the fold.
  16. Another way to look at fold.
  17. Another usage of fold.
  18. Steps during the fold.
  19. This might make experienced functional programmers cringe a bit, but here’s a neat trick that turns out to be really useful inJavascript.
  20. Just like the methodsjQuery provides, you wouldn’t want to write all of these utilities yourself. Adds functional programming support to Javascript without breaking anything else.
  21. _ : a global object
  22. Because it doesn’t modify the base array object, you can use “chain” to wrap an array and invoke multiple underscore functions on it.
  23. Chain: wraps the arrayMap: squaresFilter: predicate filters out oddsReduce: Alias for fold, sums up the
  24. Here’s how it would appear normally. There’s a lot going on but it’s still compact and readable.