Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
A Humane Introduction to Functional Programming
Search
John S.
May 09, 2014
Programming
0
68
A Humane Introduction to Functional Programming
A gentle look at what functional programming is.
John S.
May 09, 2014
Tweet
Share
More Decks by John S.
See All by John S.
Iteration, iteration, iteration.
sneeu
0
130
Do Something Else
sneeu
0
30
Data at FanDuel
sneeu
1
100
As Little As Possible
sneeu
0
81
Other Decks in Programming
See All in Programming
Deep Dive into ~/.claude/projects
hiragram
14
2.6k
Modern Angular with Signals and Signal Store:New Rules for Your Architecture @enterJS Advanced Angular Day 2025
manfredsteyer
PRO
0
230
What's new in AppKit on macOS 26
1024jp
0
110
ruby.wasmで多人数リアルタイム通信ゲームを作ろう
lnit
3
490
Quand Symfony, ApiPlatform, OpenAI et LangChain s'allient pour exploiter vos PDF : de la théorie à la production…
ahmedbhs123
0
210
なんとなくわかった気になるブロックテーマ入門/contents.nagoya 2025 6.28
chiilog
1
280
MCPを使ってイベントソーシングのAIコーディングを効率化する / Streamlining Event Sourcing AI Coding with MCP
tomohisa
0
110
『自分のデータだけ見せたい!』を叶える──Laravel × Casbin で複雑権限をスッキリ解きほぐす 25 分
akitotsukahara
2
640
明示と暗黙 ー PHPとGoの インターフェイスの違いを知る
shimabox
2
530
ニーリーにおけるプロダクトエンジニア
nealle
0
870
型で語るカタ
irof
0
200
10 Costly Database Performance Mistakes (And How To Fix Them)
andyatkinson
0
440
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Practical Orchestrator
shlominoach
189
11k
How to Think Like a Performance Engineer
csswizardry
25
1.7k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
50
5.5k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
The Art of Programming - Codeland 2020
erikaheidi
54
13k
Raft: Consensus for Rubyists
vanstee
140
7k
A Tale of Four Properties
chriscoyier
160
23k
A designer walks into a library…
pauljervisheath
207
24k
The Straight Up "How To Draw Better" Workshop
denniskardys
235
140k
Transcript
A Humane Introduction to Functional Programming 9th May, 2014-ScotlandJS #sjsJohn
A Humane Introduction to Functional Programming 9th May, 2014-ScotlandJS #sjsJohn
John Sutherland sneeu.com #sjsJohn
A Humane Introduction to Functional Programming 9th May, 2014-ScotlandJS #sjsJohn
Functional Programming “… treats computation as the evaluation of mathematical
functions and avoids state and mutable data #sjsJohn
Humane “Characterised by tenderness, compassion, and sympathy for people and
animals, especially for the suffering or distressed #sjsJohn
“We’ll need to be strict with the 20 minute duration
#sjsJohn -Peter Aitken
Functional Programming Matters #sjsJohn Why
Functional Programming Offers #sjsJohn What
Warning! Contrived examples ahead #sjsJohn
Higher-Order functions #sjsJohn Functions which take a function as an
argument, return a function, or both.
#sjsJohn var makeAdder = function (x) { return function (n)
{ return x + n; }; }; ! _.map([2, 3, 5], makeAdder(7)); // [9, 10, 12] 1 2 3 4 5 6 7
#sjsJohn var map = function (list, func) { var i,
result = []; ! for (i = 0; i < list.length; i++) { result.push(func(list[i])); } ! return result; }; 1 2 3 4 5 6 7 8 9
Purity #sjsJohn A pure function always evaluates to the same
result given the same arguments.
#sjsJohn var x = 3; ! var impure = function
(y) { return x + y; }; ! expect(impure(4)).toBe(7); 1 2 3 4 5 6 7
Immutability #sjsJohn An immutable object is an object whose value
cannot be changed after it is created.
#sjsJohn var map = function (list, func) { var i,
result = []; ! for (i = 0; i < list.length; i++) { result.push(func(list[i])); } ! return result; }; 1 2 3 4 5 6 7 8 9
#sjsJohn var map = function (list, func) { if (list.length
== 0) { return []; } else { return [func(list[0])].concat( map(list.slice(1), func) ); } }; 1 2 3 4 5 6 7 8 9
“If a pure function mutates some local data in order
to produce an immutable return value, is that ok? #sjsJohn -Rich Hickey
Application #sjsJohn
Project Euler Problem No. 2 #sjsJohn By considering the terms
in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Fibonacci sequence #sjsJohn Fn = Fn−1 + Fn−2
1, 1, 2, 3, 5, 8, 13 … #sjsJohn
var euler2 = function () { var a = 1,
b = 1, result = 0, next = a + b; ! while (next <= 4000000) { if (next % 2 == 0) result += next; a = b; b = next; next = a + b; } ! return result; }; 1 2 3 4 5 6 7 8 9 10 11 12 13 #sjsJohn
var euler2 = function () { var a = 1,
b = 1, result = 0, next = a + b; ! while (next <= 4000000) { if (next % 2 == 0) result += next; a = b; b = next; next = a + b; } ! return result; }; 1 2 3 4 5 6 7 8 9 10 11 12 13 #sjsJohn
var fib = function (n) { if (n < 2)
return 1; return fib(n - 1) + fib(n - 2); }; 1 2 3 4 #sjsJohn
var fibSequence = function (max) { var i, seq =
[]; ! for (i = 0; fib(i) <= max; i++) { seq.push(fib(i)); } ! return seq; }; 1 2 3 4 5 6 7 8 9 #sjsJohn
var fibSequence = function (pred) { var i, seq =
[]; ! for (i = 0; pred(fib(i), i); i++) { seq.push(fib(i)); } ! return seq; }; 1 2 3 4 5 6 7 8 9 #sjsJohn
var twentyTerms = function (f, i) { return i <
20; }; ! var maxTerm = function (max) { return function (f, i) { return f <= max; }; }; 1 2 3 4 5 6 7 8 9 #sjsJohn
var fibs = fibSequence(maxTerm(4000000)); 1 #sjsJohn
var isEven = function (n) { return n % 2
== 0; }; ! var fibs = _.filter( fibSequence(maxTerm(4000000)), isEven ); 1 2 3 4 5 6 7 8 #sjsJohn
The Sum of a List #sjsJohn … is the first
item plus the sum of the rest.
var add = function (a, b) { return a +
b; }; ! var sum = function (list) { return _.reduce(list, add, 0); }; 1 2 3 4 5 6 7 #sjsJohn
_.reduce([1, 2, 3, 4, 5], add, 0); ! add(0, 1)
// 1 add(1, 2) // 3 add(3, 3) // 6 add(6, 4) // 10 add(10, 5) // 15 #sjsJohn 1 2 3 4 5 6 7
Drum-roll please #sjsJohn
sum( _.filter( fibSequence( maxTerm(4000000) ), function (n) { return n
% 2 == 0; } ) ); 1 2 3 4 5 6 7 8 #sjsJohn
var fib = function (n) { if (n < 2)
return 1; return fib(n - 1) + fib(n - 2); }; 1 2 3 4 #sjsJohn
var fib = _.memoize(function (n) { if (n < 2)
return 1; return fib(n - 1) + fib(n - 2); }); 1 2 3 4 #sjsJohn
var memoize = function (func) { var cache = {};
! return function () { if (!_.has(cache, arguments)) { cache[arguments] = func.apply(this, arguments); } return cache[arguments]; }; }; 1 2 3 4 5 6 7 8 9 10 11 #sjsJohn
Review & an Improvement #sjsJohn
var fibs = function* () { var i = 0;
! while (true) { yield fib(i); i++; } }; 1 2 3 4 5 6 7 8 #sjsJohn
var fibGenerator = fibs(); ! fibGenerator.next(); // 1 fibGenerator.next(); //
1 fibGenerator.next(); // 2 fibGenerator.next(); // 3 fibGenerator.next(); // 5 fibGenerator.next(); // 8 1 2 3 4 5 6 7 8 #sjsJohn
Further … #sjsJohn Adventures in Functional Programming - Jim Weirich
Enemy of the State - Phil Roberts Functional JavaScript - Michael Fogus
None
Thank you!