Intro to
Functional Programming
By - Prakash Balodi
I Am
Software Engineer @ Quintype Inc.
Prakash Balodi
Agenda
- What is Functional programming
- Pure functions & Immutability
- State, Identity and Value
- Higher Order functions
State of the Art!
-
Complex software systems
-
Huge Codebases
-
Distributed systems (Architecture & Infrastructure)
What is FP ?
-
Programming mostly with pure functions
-
Functions take data and return data
-
Higher order functions
Pure Functions
-
Functions that are without side effects
-
Only return values, do not affect world
-
in any other way
-
For same input, output is also same (Referential Transparency)
int x= 10;
Math.sqrt(10); //Pure
new Random.nextInt() //Impure
Advantages of Pure Functions
-
Code is easier to debug,
-
Code can be lazy (or executed multiple times)
-
Function Calls can be Memoized.
-
Context for a function is small => easier to test
Person p = new Person(name: "Prakash", age:27);
println p.name // Prakash
foo(p);
bar(p);
println p.name // maybe Prakash?
Immutability
-
Basically means that value of a variable cant be changed.
-
Enforces Functions to be simple (and testable)
State, Identity and Value
-
Value signifies the meaning or worth of something.
-
Traditional programming assumes little difference between value and identity.
int x = 50;
// 50 is the value
//x is the identity.
Values don't have time associated with them, Identities do
int x = 50; //Value of x at time t1
x = x*2; //Value of x at time t2
x = 80; //Value of x at time t3
Thanks!
- Simplicity Matters - https://www.youtube.com/watch?v=rI8tNMsozo0
- FP and Battle for survival - https://www.youtube.com/watch?v=_6cDgwIHtQw
- Clojure for Java programmers - https://www.youtube.com/watch?v=P76Vbsk_3J0
Why Clojure?
-
REPL is awesome!
-
Java Ecosystem (libs and Interop)
-
Beautiful Sequence API
Introduction to FP
By Prakash Balodi
Introduction to FP
Presentation for Delhi Clojure meetup group
- 1,211