Immaculate Coder CLEAN CODE FUNCTIONAL PROGRAMMING 2024 latest update DIRECT APPLICATION This guide is designed for you to be able to apply it right away SUMMARY In this quick guide, we will explain in simple terms what functional programming paradigm is and how to apply it using Java Streams. 12 mins read FUNCTIONAL PROGRAMMING In simple terms, functional programming is a coding style where you build programs by combining functions that don't change the program's state and always produce the same output for the same input. It is like using building blocks, where each block is a function that does a specific job without affecting the other blocks, and you can combine them in different ways to build your program. Don’t worry this is just the definition you will understand it more once we discuss the examples. So let’s jump right in... JAVA STREAMS Java Streams provide a way to efficiently process large amounts of data by allowing you to define a sequence of operations to be applied on a collection of objects, like filtering, sorting, or summing up values. to use streams you should do the following: 1. convert list to stream by using stream() method 2. apply specific stream methods 3. convert back to list using toList() we’ll be showing the top use cases of Java Streams in the following pages... FILTER Purpose: Filters elements based on a condition. EXAMPLE: In the code above we are creating an array called names. Then we are filtering names based on a condition that the name should start with letter ‘A’ finally, we are converting it back to a list and saving it in method filteredNames MAP Purpose: Transforms each element in the stream. EXAMPLE: map simply changes each item in the array to another according to the lambda method we pass. So, in the code above we are mapping each element to element * element. sqauredNumbers will be 1, 4, 9 SORTED Purpose: Sorts the elements of the stream. EXAMPLE: sorted simple sorts the array in alphabatical order. so sortedNames will be ["Alice", "Bob", "Charlie"] LIMIT Purpose: Limits the number of elements in the stream. EXAMPLE: Limit comes in handy in many places where you want to limit the number of items you want to take from a list. In the code above, the output will be: [1, 2, 3, 4, 5] REDUCE Purpose: Performs a reduction on the elements of the stream to produce a single cumulative result. EXAMPLE: As its name indicates it reduces the list of items to a single result. In the code above, we are reducing the list of items to one number which is its sum. we pass the reduce: 0: This is the initial value. The reduce operation starts with this value. (a, b) -> a + b: This is a lambda expression. Here, it describes how to combine two elements. a is the accumulated result so far, and b is the value of the current element in the stream. For each element in the stream, the lambda expression is applied. It adds the current element (b) to the accumulated sum (a). CONCATENATING METHODS Purpose: do multiple operations on the same list of items. EXAMPLE: you can do multiple operations on the same list. In the code above, we are: 1. sorting the array output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 2. limiting it to first 5 items output: [1, 2, 3, 4, 5] 3. multiplying each element by itself final output: [1, 4, 9, 16, 25] 2023 IMMACULATE CODER FOLLOW FOR MORE