Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Functional Programming In Java

Table of Contents

Streams

Concepts

  • Immutability means once a data structure is created, its value cannot be changed. Instead of updating the state, we create new instances of the data structure
  • Higher order functions can take other functions as parameters or return functions as results

Java Streams

  • sequence of elements that can be processed parallelly or sequentially
  • stream will not change the original data structure
  • Stream has:
    • Source: data source
    • Aggregate Operations: Using the map, filter, reduce, etc operations complex manipulations can be written in a functional way
    • Pipeline: Pipelines can be formed when operations on a stream are chained together
    • Intermediate and Terminal Operations: Intermediate operations return a new stream, terminal operations product result/side effect
    • Lazy Evaluation: Operations are only evaluated when a terminal operation is invoked, this makes the performance better.

Example Usage

First Class Function

  • First-class functions let Java treat functions as first-class citizens so that they can be treated as any other data type. So functions can be assigned to variables, passed as arguments, and returned as values from functions
  • Lambda expression is a way to represent an anonymous function that can be defined "on the fly" right where it is needed.

Example Usage

Advanced Concepts

  • Partial Application means changing a function's number of arguments and creating a function with fewer arguments
  • Currying is a special case of partial application. here a function with multiple arguments is converted to a sequence of functions each having 1 argument
  • Composition is combining 2 or more functions to produce a new function
  • Purity means a function should not have any side effect(change external state) and will always produce the same output for a particular input

Example Usage