-3

From a functional programming view, where I must keep state. Which one of these two approaches is deemed safer? Or are they the same. Also if one is preferred over the other in FP, I'm guessing the variable reassignment.

1.

topics = {
    ...topics,
    topic: { subscribers: [], registerCallback: callback }
};

2.

topics[topic] = { subscribers: [], registerCallback: callback }

The first approach would be create new object and then reassigning the variable. The second approach would be to mutate the object properties.

I do realise that spread does not do a deep copy, but that isn't the point of this question.

basickarl
  • 37,187
  • 64
  • 214
  • 335
  • 2
    #1 is more along the lines of functional programming – TKoL Dec 16 '19 at 10:39
  • Also, shouldn't #2 be `topics['topic']` – TKoL Dec 16 '19 at 10:39
  • @TKoL topic in this instance is a string which holds a value so. – basickarl Dec 16 '19 at 10:40
  • @TKoL Ahh I see, fixed thx! – basickarl Dec 16 '19 at 10:41
  • 2
    Both versions are not functional. However, if you reassign inside a function scope, it doesn't interfere with the parent scope, i.e. the former is slightly better from a functional point of view. –  Dec 16 '19 at 10:45
  • @bob How would one keep state in a program in FP that is accessed asynchronously via an API? I can't think of any ways except making one function (this one) impure. – basickarl Dec 16 '19 at 10:46
  • This is an object declaration, this doesn't really have anything to do with functional programming, if the `callback` is an actual callback then that's not functional at all and very much misses the point of what functional programming is – Liam Dec 16 '19 at 10:46
  • This sounds like an [XY Problem](http://xyproblem.info/). What exactly are you trying to do here? – Aadit M Shah Dec 16 '19 at 10:48
  • If you want to actual do actual functional programming in js I'd recommend you look into the [rxjs library](https://rxjs-dev.firebaseapp.com/guide/overview) – Liam Dec 16 '19 at 10:49
  • @AaditMShah API. I tried ask a question here: https://codereview.stackexchange.com/questions/233983/functional-programming-approach-to-a-rest-api-example-is-this-a-good-example but no answers so.. – basickarl Dec 16 '19 at 10:50
  • @Liam-ReinstateMonica I really dislike libraries that introduce their own things into languages, so I will skip that. Already having a hard enough time accepting TypeScript. – basickarl Dec 16 '19 at 10:52
  • 1
    IMHO, if you want to use express then you'll have to abandon FP. The express framework forces you to write non-functional code, and trying to make it functional is just going to make things more difficult for you. – Aadit M Shah Dec 16 '19 at 10:57
  • @AaditMShah Hmm. Requests from users towards an API are in nature asynchronous, so I doubt it really has anything to do with Express. So what would you use instead for an API? – basickarl Dec 16 '19 at 11:01
  • 1
    Asynchronous code can be modelled using [monadic promises](https://github.com/fluture-js/Fluture) or [observables](https://rxjs.dev/) in FP. However, express doesn't have a monadic interface. You could try to wrap the express framework in a monadic interface. However, that's too much work for too little gain. Instead, I'd advise you to embrace the style of programming that express advocates. Not everything has to be functional. – Aadit M Shah Dec 16 '19 at 11:07
  • asynchronous !== functional – Liam Dec 16 '19 at 11:17

1 Answers1

0

Given that the "normal" paradigm for functional programming is to keep objects immutable, i'd go with option #1:

topics = {
    ...topics,
    topic: { subscribers: [], registerCallback: callback }
};

You want to structure your code in such a way, that a function will take an object as an input and it will return a new object, without changing the original one that was passed in.

Keep your functions pure and your objects immutable and you got your self a functional-style program!

EDIT: Take a look on this SO post, brace your self for some reading thought Click me!

MKougiouris
  • 2,821
  • 1
  • 16
  • 19
  • Indeed! But somewhere in the code state (in my case) must be kept, and this function will hold the code presented (poor impure function I'm sorry, no function deserves this!). I hope you understand what I mean? – basickarl Dec 16 '19 at 10:44
  • I added a link to a kind of relevant SO post. It talks about state in functional-style programs. It will take a bit to read, and a bit longer to dive deep into it, but its a good place to get a grip – MKougiouris Dec 16 '19 at 10:48