










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
it about software technolohies and
Typology: Summaries
1 / 18
This page cannot be seen from the preview
Don't miss anything!
c
● Having knowledge of functional programming. ● Learning the details of streams, lambda expressions, optionals. ● Practising with real life examples.
● Functional programming is becoming popular. ● Allows you to think from a different point of view. ● In Trendyol, we use depending on the requirements.
Keywords
Functional Programming
● Programming languages: ○ Haskell, Scala, ○ Erlang, JavaScript, ○ Python, Java ○ Many other languages
● Functions can be: ○ Variable ○ Parameter ○ Return type ○ Expressions
● Benefits: ○ Performance ○ Effective code ○ Less code ○ Improved debugging
● Some terms ○ Pure Functions ○ High Order Functions ○ Referential Transparency
Functional Programming
It is a declarative programming paradigm.
Contains multiple pure functions. Function chaning is core flow.
Can not change state of any object or value. Immutability is important. Avoids mutable data. (^) Programming is done with expressions.
Functions return the same output for a given set of inputs.
Has no side effect. There is no impact of external parameter on the output of function.
Functional programs are inherently thread-safe.
Its main focus is on “what to solve”
Inherent parallelism allows for better utilization of hardware resources, leading to improved performance and scalability.
Encourages the use of lazy evaluation, where computations are only performed when their results are actually needed
Functional Programming in Java
Functional interfaces are also called Single Abstract Method Interfaces.
As the name suggests, they are interfaces that permit exactly one unimplemented method inside them.
Functional interfaces can be implemented, and the implemented class can be used to create an object that represents a function
● Starting with Java 8 , functional programming has been gaining real improvements in Java, with lambdas, Streams API etc. ● Before Java 8, we need to create separate interfaces and their implementations for each purpose. ● With this new version of Java, we have built-in interfaces, streams, optionals, lambda expressions - which make coding easier. ● The Java API contains a set of functional interfaces designed for commonly occuring use cases. ● We don’t have to create our own functional interfaces for every little use case. ● They have located in the java.util.function package
Lambda Expressions
● A lambda expression is a short block of code used as a replacement for a method declaration. ● It takes in parameters and returns a value. ● It doesn’t need a name and it can be implemented right inside the body of another method. ● It can be used to implement a single method directly from a Functional Interface. ● Simplifies the usage of Function, Predicate etc interfaces in Java. ● Arrow -> helps us to define lambda expressions.
A simple lambda function looks like:
Streams
● Streams are the structure of processing a collection in functional style. ● A stream represents a sequence of elements and supports different kind of operations to perform computations upon those elements. ● Original collection is not modified. ● Stream operations are either intermediate or terminal.
Imperative Functional
Operetion
Parallelizing Operations
Parallel Operator^ Result
● By default, any stream operation in Java is processed sequentially, unless explicitly specified as parallel. ● Sequential streams use a single thread to process the pipeline. ● A sequential stream can be converted to a parallel one when we have actual performance requirements. ● We can achieve this by adding the parallel method to a sequential stream or by creating a stream using the parallelStream method of a collection.
● Sometimes parallelizing is not a good choice. ● It is not performance effective every time. ● A large amount of data and many computations done per element indicate that parallelism could be a good option. ● A small amount of data, unevenly splitting sources, expensive merge operations and poor memory locality indicate a potential problem for parallel execution.
● Parallel streams enable us to execute code in parallel on separate cores. ● The final result is the combination of each individual outcome.
Optional Usage
● We can chain functions in FP, having null input is not desired and throw NPE. ● Between chains we should check if value present or not ● Optionals inform you about possibility of not exists. ○ ifPresent ○ ifPresentOrElse ○ isPresent ○ orElseThrow ● We can use above and more built-in methods to take action.
Null References
● Null is a value that is not actually a value. ● Hard to determine whether the value not present or just has no value. ● Called as “A billion dollar mistake”. ● Functional programming avoids null.
● Hard to read.
● Causes to Null Pointer Exception.
Quiz
Q&A