Everybody around is talking about functional programming these days and since everybody wants to go functional way it is indeed on-trend. Alright, if we really want to pursue functional programming let’s not forget the most important part of any programming language which is “Data Structures” and today we will be talking about data structure. Now, everything is about functional programming we will talk about functional data structure using Vavr library(A functional Java library)
We will talk about “What, Why and How” functional data structure in detail which will help you understand why that is so important.
Why are Functional Data Structures so important?
There is this concept called immutability which is directly related to functional programming and immutability, of course, addresses the problems in a multithreaded environment. So immutability is indeed important in functional programming. Now, the question is why functional data structures are so important, they are important because of the fact, functional data structures are immutable and persistent and we already know the benefits of immutability.
What are Functional Data Structures?
Functional data structures are immutable and persistent in nature.
1. Immutable – Object cannot be modified once created/instantiated.
2. Persistent – A persistent data structure is a data structure that always preserves the previous version of itself when it is modified. Such data structures are effectively immutable.
So, If a data structure satisfies the above two condition i.e. a data structure is immutable and persistent, it is a functional data structure, unfortunately, we do not have a functional data structure in Java so far. However, with the help of libraries like Cyclops and Vavr, we can still use functional data structure in our programs. We will be seeing a functional data structure in action when we start using it.
How can we bring the Functional Data Structure in use?
Now that we already know what a functional data structure, let’s use Functional data structures.
The following are examples of functional data structures from the Vavr library.
1.List – List is the most commonly used data structure in programming, let’s see how can we create a List.
// List demo List list = List.of(1,2,3,4,5); // Print list System.out.println(list); // Add element to list list.append(6); // Print list System.out.println(list); // List(1, 2, 3, 4, 5), this shows list is immutable. // Add element to list and store it in a new list List list1 = list.append(6); // Print list System.out.println(list1); // List(1, 2, 3, 4, 5, 6)
This list data structure comes with a lot of methods that can be used to perform various operations on List.
2. Queue – Queue is another functional data structure which we have in Vavr which is based on two linked list where the frontlist holds the elements that are dequeued and rear list holds the elements that are enqueued. Both the operations enqueue and dequeue are performed in O(1).
Let’s see Queue in action.
// Create a queue. Queue queue = Queue.of(1, 2, 3) .enqueue(4) .enqueue(5); // Print a queue. System.out.println(queue);
The initial queue was created with three elements and then two more elements were enqueued on the rear list.
When dequeuing an element we get a pair of the first element and the remaining Queue. Always a new version of the Queue is returned because functional data structures are immutable and persistent. The original Queue is not affected.
// Dequeuing elements Tuple2<Integer, Queue> tuple2 = queue.dequeue(); // Print tuple System.out.println(tuple2);
We can also dequeue element with option tuple, this could be useful to prevent Null Pointer Exception when we have an empty Queue.
// Dequeuing an element with option Tuple Option<Tuple2<Integer, Queue>> tuple2Opt = queue.dequeueOption(); // Print option Tuple System.out.println(tuple2Opt);
3. Sorted Set – Sorted Sets are data structures that are frequently used for storing unique elements. Sorted Sets are created with the help of TreeSet. Like other data structures, Sorted Sets are also functional in nature.
Let’s see Sorted Sets in Action
// Create SortedSet SortedSet sortedSet = TreeSet.of(1,2,3,4,5,5); // Print SortedSet System.out.println(sortedSet); // Print only unique elements // Add element to SortedSet SortedSet sortedSet1 = sortedSet.add(6); // Print SortedSet System.out.println(sortedSet1);
4. Tuple – Tuple is nothing but a data structure that will have a fixed number of elements and can hold objects of heterogeneous types(different types). Also, Tuple is immutable and persistent that’s why it is a part of the functional data structure.
Tuple in action
// Create a Tuple Tuple2 tuple2 = Tuple.of(1, 2); // Print a Tuple System.out.println(tuple2); // Access a tuple value System.out.println(tuple2._1); // 1
What you learned?
Data structures that are used in the functional programming language. Also, why are they so important for functional programming language.
That’s pretty much it from the article, I have a GitHub repository for all the examples related to this article, feel free to fork it and start practicing the examples. If you have any feedback or queries, please do let me know in the comments. Also, if you liked the article, please give me a thumbs up and I will keep writing such blogs for you in the future as well. Keep reading and Keep coding 🙂