1 Replies - 94 Views - Last Post: 10 November 2019 - 05:53 PM

#1 Neonz27   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 08-May 16

Is there a pure functional way to do persistent state in Javascript?

Posted 10 November 2019 - 05:48 PM

I have a program I am working on in Javascript (ES6+) and I need to allow the users to perform functions that return a new modified copy of the state they receive as a function parameter. I have had people tell me in the past that it is impure to modify the state directly in functional programming, but I need the user to be able to select between performing the functions on the initial state of the program or the output of another. How can I go about doing this in the purest and functional manner possible? I apologize if I have done poorly explaining what it is that I need to be done, but I have provided more explanation in a script that I wrote with some comments and pseudo-code. It can be found on this post or alternatively here: https://jsfiddle.net/r5puh8gv/

Pseudo-Code & Explanation
const initialState = [
	{ name: "Stuff 1", category: 1 },
  { name: "Stuff 2", category: 2 },
  { name: "Stuff 3", category: 1 },
	{ name: "Stuff 4", category: 2 }
];

const filterCategoryOne = state => {
	return state.filter(item => { item.category === 1 });
};

const filterCategoryTwo = state => {
	return state.filter(item => { item.category === 2 });
};

const mapName = state => {
	return state.map(item => { item = item.name });
};

// I am making a program that starts with an initial data set.
// The user can perform a variety of functions on the data set that return a new version.
// I am trying to find a way to allow the the user to make state persist or reset after every change.
// For example, the user could filter the whole array by category with the following:
const filteredState = filterCategoryOne(initialState);
// However, the user should be allowed to have their changes carried over to the next function or not.
// Like if the user has persistent changes turned on they can use the output of a previous function
// as the input to the next one! I wish to do something like this:
let persistentMode = true;
let mappedFilteredState = [];
if(persistentMode) {
	mappedFilteredState = mapName(filteredState);
} else {
	mappedFilteredState = mapName(initialState);
}
// I realize that it is impossible to make pure functional scripts in Javascript.
// However, I am still very curious as to how I can go about implementing this idea functionally.
// I am trying to figure this out to learn the function programming paradigm after all.
// I have been told that I should never modify the state, but return a new copy instead.
// Is it reasonable to have a variable for the current state that can be modified, but can always be reset
// back to the value of the initial state? Like this (please keep in mind that this is pseudo-code): 
let currentState = initialState;
currentState = filterCategoryTwo(currentState);
currentState = mapName(currentState);
// Then the user can simply turn off persistent changes to reset the current state to the inital.
if(!persistentMode) {
	currentState = initialState;
}
// Is this functional at all? I am quite confused if it is as I was told not to touch the state.
// I guess I'm kind of trying to use currentState like I would a point in C++. 
// It can point to initial state or it can point to a copy of the state returned by a function.
// I apologize if I am wording any of this correctly or have made any mistakes.
// I realize that I am not the best at formulating intelligible questions, but I need some help with this.



Is This A Good Question/Topic? 0
  • +

Replies To: Is there a pure functional way to do persistent state in Javascript?

#2 Neonz27   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 08-May 16

Re: Is there a pure functional way to do persistent state in Javascript?

Posted 10 November 2019 - 05:53 PM

I would also like to mention that I have never touched functional programming until now, so please don't bite my head off and chew it up. I have used strictly the object-oriented programming paradigm for years now, and all of this I am learning now is completely foreign territory for me.

This post has been edited by Neonz27: 10 November 2019 - 05:54 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1