Endre Galaczi
π¦ @galacziendre
βοΈ galacziendre@gmail.com
functional programming style evaluation of mathematical functions avoid changing-state and mutable data
var triple = function(n) {
return n * 3;
}
functions can be passed as arguments
map, filter, reduce
var numbers = [1,2,3].map(triple)
instead of:
input = [1,2,3]
numbers = []
for(var i = 0; i<=numbers.length; i++){
numbers.push(input[i])
}
var a = 5;
a = a + 1
no mutating global state
function π© triple(n){
times_called = times_called + 1;
return n * 3;
}
no mutation -> no loops no while no for
β¨ recursion β¨
function count_to_a_chicken(n){
if(n==0) return "π ";
else return count_to_chicken(n-1);
}
stack overflow
factorial 0 = 1
factorial n = n * factorial n-1
factorial(10) =
10 * factorial(9) =
10 * (9 * factorial(8)) =
10 * (9 * (8 * factorial(7))) =
10 * (9 * (8 * (7 * factorial(6))) =
...
10 * (9 * (8 * (7 * (6 * (5 * (4 * (3 * (2 * 1))))))))
factorial 0 i = i
factorial n i = factorial (n-1) (i*n)
factorial(4,1)=
(factorial(3,4))=
((factorial(2,12)))=
(((factorial(1,24))))=
((((factorial(0,24)))))=
((((24))))=
es6 tco
encourages writing code using expressions rather than statements
int total = 0;
for(int i = 1; i<=10; i++){
total = total + 1;
}
sum [1..10]
hatch = π³ -> π
clone = π -> π π
superhatcher x = clone(clone(hatch(x)))
superhatcher = clone.clone.hatch
superhatcher π³ -> π π π π
var triple = function(n) {
return n * 3;
}
var numbers = [1,2,3].map(triple)
input = [1,2,3]
numbers = []
for(var i = 0; i<=numbers.length; i++){
numbers.push(input[i])
}
"There are only two hard things in Computer Science:
cache invalidation and naming things."
-- Phil Karlton
memory is cheap
cpu is growing in # cores not in GHz
functionally pure code is easier to understand
pure functions are easier to unit test
var mistery_func = function(a, b) {
//lol I don't know why we need this
//but it breaks otherwise
write_to_file(random_filename(), "π© ");
return a * 2 + b * 5;
}
side effects need to be cleaned up or need to be mocked
learn by example or from your own mistakes
tests as communication
Do you use source control? Can you make a build in one step? Do you make daily builds? Do you have a bug database? Do you fix bugs before writing new code? Do you have an up-to-date schedule? Do you have a spec? Do programmers have quiet working conditions? Do you use the best tools money can buy? Do you have testers? Do new candidates write code during their interview? Do you do hallway usability testing?
- do you always write tests?
- do you have mandatory code review?
Islands of functional purity
You will probably never end up programming in a purely functional language
Like in javascript you are not going to start using recursion everywhere
Even if you never work in a functional language professionally,
understanding functional programming will make you a better developer.
It will give you a new perspective on your code and programming in general.
sum [1..12]
factorial 0 = 1
factorial n = n * factorial (n - 1)
let right_triangles = [(x, y, z) |
x**2 + y**2 = z**2,
x <- [1..20],
y <- [1..20],
z <- [1..20] ]
Endre Galaczi
π¦ @galacziendre
βοΈ galacziendre@gmail.com