Functional Programming
Concept
By Abdullah Ilhanli
Email: abdullah.ilhanli@gmail.com
Agenda
FP: Funtional Programming
• Prerequisites
• Motivation
• Definition of Function
• Side effect
• Definition of FP
• Lambda Syntax
• Functional Interfaces in Java
• Programming models
• Stream API
• FP Benefits and Drawbacks
• Why we have to use FP
• Do we still need to write imperative?
Prerequisites
• What do you need to better understand?
- Basic Programming(looping, if/else)
- Data structures and algorithms(recursion)
- Design Patterns(strategy, iterator)
Motivation
https://medium.com/@cscalfani/so-you-want-to-be-a-functional-programmer-part-5-c70adc9cf56a
Motivation
Functional Programming vs Object Oriented Program
• Lets watch!
• https://www.youtube.com/watch?v=K_g_xUtydpg
What is a function?
F(x) = x+1
For every x result must be always x+1
Is the result always x+1?
https://dev.to/marpme/basic-functional-programming-with-javascript-2elj
Functional Programming : Side effect
https://www.slideshare.net/EastBancTech/functional-programming-with-c
Side effect
When we are not functional?
All kind of IO operations change the
world. This causes Side Effect.
- File I/O
- Database calls
- Web service calls
What is functional programming
Function as a first class citizen
- You can assign functionality to a
variable
- You can pass functionality to a
procedure
- You can return functionality from
procedure
What is functional programming
https://medium.freecodecamp.org/learning-the-fundamentals-of-functional-programming-425c9fd901c6
Lambda Syntax
https://medium.com/@gelopfalcon/best-practices-when-you-use-lambda-expressions-in-java-f51e96d44b25
Functional Interfaces in Java
https://www.slideshare.net/andreaiacono/java8-39841939
Practice 1: Functional Programming In Java 7
and 8
We will look at basically what kind of problem can we can solve in
functional programming
- interface driven approach
- Anonymous class
- Lambda expression
- https://github.com/ailhanli/functional_programming_by_example/tree/
master/src/main/java/com/ailhanli/practice1
Practice 2: Lazy Execution
In this example we will look at lazy execution using log4j.
https://github.com/ailhanli/functional_programming_by_example/tree/master/s
rc/main/java/com/ailhanli/practice2
Functional Programming: Programming models
https://www.ibm.com/developerworks/library/j-java8idioms1/index.html
Declarative vs Imperative
https://www.slideshare.net/HngHongHi/functional-reactive-programming-74350959
Stream API
https://www.logicbig.com/tutorials/core-java-tutorial/java-util-stream/stream-api-intro.html
Stream API
https://www.exclamationlabs.com/blog/refactoring-for-java-8-streams/
Practice 3: Using Stream API
In this practice we will first look at lazy execution and then using stream api to
solve problem.
https://github.com/ailhanli/functional_programming_by_example/tree/master
/src/main/java/com/ailhanli/practice3
Practice 4: Statistic With Stream API
We will look at grouping account salary sum by their age
https://github.com/ailhanli/functional_programming_by_example/tree/m
aster/src/main/java/com/ailhanli/practice4
Practice 5:
Multithreading by example
In this example we will look at;
-How make stream parallel
-What kind of problem can we face during the parallel execution
https://github.com/ailhanli/functional_programming_by_example/tree/master/src
/main/java/com/ailhanli/practice5
Practice 6:
More examples about Functional
Lets check them
https://github.com/ailhanli/functional_programming_by_example/tree/master/src
/main/java/com/ailhanli/practice6
Functional Programming Benefits and Drawbacks
• Benefits
• Functions have no side effect
• More diffucult to learn than other programming
• Large program can be written with functions tat are relatively easy to test
• Drawbacks
• Less efficient memory usage because of recursion
• Great for programs that are primarily used for analyzing large amounts of
numerical data
Lets watch a video to motivate us more!
• https://www.youtube.com/watch?v=0-wM-bBMYg4
• https://www.youtube.com/watch?v=kJuJU-7_utA
Why we have to use Functional Programming
Why we have to use Functional Programming
- If our project programming language functional, we can write after
now functional
-If our project is not supported, we can make our daily tasks using
java 8 or any other functional
-It is good start to learn Scala for Java developer or F# for Dot net
developer
- It is good start to understand Reactive Programming
Do we still need to write imperative programming?
It depends!
Below link you can see what we can not make functional
https://github.com/ailhanli/functional_programming_by_example/tree/master
/src/main/java/com/ailhanli/practice7
Do you have any question about functional
programming?
Thank you!

Functional Programming Concept

Editor's Notes

  • #2 Hello everyone, welcome to functional programming training. before I started, I want to inform that I wont talk details in deep that’s because we don’t have enough time. I will try to explain concept behind the scene. Actually, you dont need to know java. Basic programming knowledge is enough.
  • #3 I will first talk about requirement; what we need to understand, And then motivation; what make FP different than others and why we need And then we will define Function in terms of Math and then programming And then we will talk side effect which changes the world And then we will define functional programming And then we will talk about lambda syntax which comes with java 8 And then we will check already defined functional interfaces in java And then we will focus on programming models And then we will define what Stream API does In Last three slayts are about motivation in more..
  • #4 To understand better, you only need basic programming constructs these are looping, if/else Data structures and algorithms especially recursion And Some of Design patterns strategy and iterator i will explain a bit what they are.
  • #5 Our tiny monkey is our PC. NO actually it is operating system which is a system our programs run on. Assembly is a programming language for microprocessor and devices. It is simply machine language. It is very low level we have to write lots of instructions for simple program Procedural language is good for simple business applications. We have procedures and we call them in order to do our business. Advantage is simple to understand for simple program. Disadvantage is difficult to maintain and extend for real business applications. Object Oriented is good for real business applications. We model our business in terms of objects. SOLID is more important principle in OOP. If you are not using SOLID you are actually writing Object Based Programming. It is now easy to main and extend. Main big disadvantage is writing multithreaded program is really difficult. Functional is our big monkey. It is more readable, easy to extendable and maintain than OOP. Multithreading is also easy. We write less code than Procedural and it uses OOP patterns behind scene. Does it mean we will not use OOP when we program functional? Not really, using OOP and functional together is best programming model. It is actually new way of thinking and not easy to understand. I will explain concept behind the scene in this training! As a result procedural languages uses assembly, OOP uses procedural languages and Functional uses OOP. As a result all of them converted to assembly by the Programming language. So lets get started!
  • #7 In math function is f(x)= x+1 What it means. for every x, result should be x+1 If result is different than x+1, it is not pure function and we can say function has side effect Mathematical functions are pure functions. Programming functions depend.
  • #8 In this example, on the left hand side, for every x and y, result is always x^2+y^2. So it is pure! On the right hand side result is usually different, because date is externally changed and our function will return different result time by time so it has side effect. When do we have side effect in our program?
  • #9 All kind of IO operations including reading/writing file and databases , calling web service etc.. When you do IO during the function execution, function result can be different that because we use operation system disk For instance if file is deleted your program can throw an exception so we can say result is not x+1! Or if database is changed by other programs, our program can return different result.
  • #10 Normally, we define our variable in a program uses simple data types and classes Functional programming means we write our program in terms of functions. What I mean that we can assign our functionality to a variable We can pass our funtion to a procedure We can our function from a procedure
  • #11 What I mean that, a and b can be function.
  • #12  Lambda expression is fundamental of functional of programming Lambda is a implementation of functional interface Functional interface is an interface which has single method only. What is an interface? interface is simply a contract between caller and callee Lambda is shorthand syntax and easy to read than anonymous inner class implementation
  • #13 Java has several built-in functional interfaces. - The main concern should be when reading an interface what this interface takes and returns simply input and output For example Consumer interface take any type of parameter and return nothing. so it consumes. or Function interface take any type of parameter return different types.
  • #14 In demo first we will look at how to change a simple algorithm using strategy pattern and then we will make our algorithm lambda based and see what kind of problem is solved using lambda and what it for.
  • #15 In this example we will look at lazy execution versus eager execution in sample logging
  • #16 In imperative programming model we tell what and tell how to do. We do imperative programming everyday. For instance looping through list, filtering elements calculating sum of filtered elements. In declarative programming model we tell what not how. Programming language will care about the business.
  • #17 In imperative programming you have to write all kind of instructions in order to do your business In declarative programming you have only say what you want like a boss Declarative programming is new way of thinking you only concern your business rather than details about how to do
  • #18 Almost in our all programs we use mostly collections. We iterate collection via iterator pattern and do some calculation. iterator pattern is a pattern which traverse to collection. What Stream API does? We do functional composition with this API. It is an API uses iterator and recursion. with stream API, we write our program uses SQL like operators. all operators take lambda expression as a parameter. It has three components: source, intermediate operators and terminators. Operators take function as an argument. We have focus on what kind of functional interface it needs. **source can be collection, array or file **intermediate operator can be filter, map, sort, distinct **terminator can be collect, toArray, count, reduce If chain hasnt terminator, it wont be execute. so it is lazy lets do more example and understand how can we program functional uses stream API.
  • #19 What a nice picture to understand function composition. Thanks to Toptal In this picture you see that there are many animals goes inside pipe, some them filters based on filter algorithm, then we box filtered ones. finally collect all boxed items. filter actually takes an conditional statement and pass items who satisfy condition. in picture, filter algorithm is our conditional statement. map is take one type of item and create new type of item. in picture, take animal box it. collect is a collectors who carry our boxed animals.
  • #20 In this example we will look at first lazy execution. if a stream hasnt terminator it wont execute. So it is lazy. Then we will develop our previous example uses stream api.
  • #21 Stream API is good for analying data. For example grouping data by age and sum of salary
  • #22 We will see that multithreading is really easy. it takes a second!
  • #23 I will show my daily examples using functional programming.
  • #24 Benefits: because it doesnt have side affect, concurrency is not diffucult. We dont have syncronization problems. We can write a lot of lines of code in a single line and it is more readable and testable We can use functional programming to analyze our data. Drawbacks: It is diffucult to learn and because it uses recursion behind the scene, it uses lots of memory
  • #26 For the old projects which has been developed in earlier version of java for example java 7 or before. we can't use it. if our project in java 8, we can write some parts especially, doing business with collection using java 8. or at least we can write our daily external things in java 8. It is also a first step for learning reactive which will me presented in next week.
  • #27 For collection related jobs, functional programming is good but performance is not better than classic looping structure sometimes. or other things, which are mostly has many details we cannot write functional. example: show one example.