WHY FUNCTIONAL PROGRAMMING?
I decided to try "Functional Programming" because I've read, from multiple sources, that functional programming has the following benefits:
Because its core focus is on immutability, programs that implement the paradigm are far less vulnerable to external software affecting, or changing, the code during runtime.
Many people are enticed by functional programming because it's very testable. Because functions must always return the same result for the same argument, the code is highly predictable, making it highly testable.
Writing functions as explained above, creates, as already stated, very predictable code, but it also creates very readable code. Since the code is predictable, and readable, it's also quickly understood, especially by those who know how to implement the concept.
...who wouldn't want that? Of course, I gave it a shot.
My First Failed Attempt:
My first attempt at implementing "Functional Programming" didn't go well, and I don't know if I have a much better understanding of the concept now. In my head, I was thinking of state (the state of my program at any given moment). I wanted to make it to where the state of everything I implemented would be immutable. I only wrote 2 lines of code before I quickly realized I didn't have the slightest clue as to what I was doing. My first idea was to make the variables non-writable, which didn't work out as I had expected. Everything was static, I couldn't figure out how to create a dynamic program, while implementing immutable variables.
Obviously immutable doesn't mean static, but it's not exactly clear to me how one can achieve a dynamic system, when all the values in the system cannot be changed.
To, reiterate, and ask my question in a clear concise way, that doesn't require opinion, I have authored this question.
"How do JavaScript, TypeScipt, &/or Node.js developers implement immutable data structures for managing the state of their applications, when JavaScript doesn't offer any sort of explicit immutable data types, or support?"
An example of Any immutable data structure is what I am looking for; as well as how to implement functions that allow me to make uses of the data structure, to manage the state of a JS Application. If the answer involves the use of 3rd party libraries, other languages, or any other tool, that is perfectly fine by me. An example of actual code would be awesome, that way I have something to interpret and come to understand.
Bellow is my horrible attempt at creating an immutable data structure, that I could implement.
Though its not good code, it demonstrates what I am trying to accomplish
'use strict';
const obj = {};
Object.defineProperties(obj, {
prop_1: {
value: (str) => {this.prop_3 = str};
writable: false,
},
prop_2: {
value: () => this.prop_3;
writable: false,
},
prop_3: {
value: '',
writable: false,
},
});
obj.prop_1('apples & bananas');
console.log(obj.prop_3);
/*
TERMINAL OUTPUT:
Debugger attached.
Waiting for the debugger to disconnect...
file:///home/ajay/Project-Repos/j-commandz/sandbox.js:19
this.prop_3 = str;
^
TypeError: Cannot assign to read only property 'prop_3' of object '#<Object>'
at Object.set (file:///home/ajay/Project-Repos/j-commandz/sandbox.js:19:19)
at file:///home/ajay/Project-Repos/j-commandz/sandbox.js:37:5
*/