SlideShare a Scribd company logo
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?

More Related Content

What's hot

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
 

What's hot (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++)
 

Similar to 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
JavaScript in 2016JavaScript in 2016
JavaScript in 2016Codemotion
 
JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)Eduard Tomàs
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present FutureIndicThreads
 

Similar to 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
JavaScript in 2016JavaScript in 2016
JavaScript in 2016
 
JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)JavaScript in 2016 (Codemotion Rome)
JavaScript in 2016 (Codemotion Rome)
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present Future
 

Recently uploaded

AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekCzechDreamin
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutesconfluent
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...Product School
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlPeter Udo Diehl
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka DoktorováCzechDreamin
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfCheryl Hung
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsPaul Groth
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor TurskyiFwdays
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...Product School
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Thierry Lestable
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Julian Hyde
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...CzechDreamin
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualityInflectra
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCzechDreamin
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...CzechDreamin
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoTAnalytics
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Tobias Schneck
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxAbida Shariff
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...Product School
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonDianaGray10
 

Recently uploaded (20)

AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya HalderCustom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
Custom Approval Process: A New Perspective, Pavel Hrbacek & Anindya Halder
 
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
Behind the Scenes From the Manager's Chair: Decoding the Secrets of Successfu...
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 

An Introduction to Functional Programming with Javascript

Editor's Notes

  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.