-3

Let's say, that we keep track of students entering the auditorium using their IDs(Key) and their check-in time(Value) in a Map. We delete the entries once they move out of the auditorium. I want to know at a given point in time who all are there in the auditorium and when did they check-in. All Imperative languages do the job.

But, The functional programming examples/gurus simply explain like the below: take input map, add the entry and return the output map. similarly for delete. Thus we don't modify the input. All of this advice works for the frozen data sets given the knowledge of what will happen in advance while writing the code.

But in this scenario, how would we know how many variables should I create in advance to store the newer versions of the immutable maps after insert/delete? Let's discuss this from an API perspective where we expose an endpoint to a device that tracks the check-in/check-out of the students.

OR Is there any other smart way to know the current state of the auditorium in Functional programming without doing any of the above?

NOTE: Please do not simply advise that FP is not suitable for doing these kinds of tasks. Let's say we have already taken up a project in FP and encounter the above scenario. What will you do :)?

1

2 Answers 2

2

While functional programming emphasises pure functions, a pure function alone can't define a useful software system. You need side effects to observe its behaviour, and that's impure. In practical terms, input is impure as well. Thus, there's always an impure part of any program, even when written in a strictly functional language. This is explicitly visible in Haskell, where a program's entry point, and all other interaction with the outside world, runs in the IO context.

If the hypothetical check-in/check-out device is written Haskell, each interaction is going to run in IO. This means that side effects can take place there, including state mutation.

What you'd probably do is to still use a pure Map, but once you've exchanged the old map with the new map, you can update a mutable value in memory using IORef, MVar, etc. or you can update a database, file, and so on. These are all impure actions that run in IO.

The rest of your program can be implemented with pure functions; i.e. not in IO.

2
  • Liked it. Could you please enrich your answer with some pseudo-code so that people can relate the parts of the answer clearly?
    – Vicky
    Dec 20, 2019 at 6:29
  • @Vicky Pseudo-code is usually employed to illustrate algorithms - not how a type system or software architecture works. I don't see how I could use pseudo-code to illustrate this. It'd just look like any other code. I do, however, have plenty of articles on this topic. A good place to start could be here or here. Dec 20, 2019 at 6:41
-2

Are you talking about a Map as a Data Structure or Map as a Functor?
For the former; You are not mutating a map by adding an entry or deleting one. SeeScheme Definition of Mutation for more information. So, I am not exactly sure which you are talking about.

Ignoring that, this seems like a straightforward exercise. At no point do you alter a value in the map, or at least you should not be. If they were indexed like an array that would be different, but I'll assume they are not.

You can add and remove students from the map all day long, and still maintain Referential Transparency. If you need to track the state, then use Memoization. If you mean Map as a functor instead of Map as a data structure then FP doesn't always return a new something when you add or pop, or delete. If this is an issue, then perhaps you are not using the right language. Side effects are avoided by matching interfaces to implementations - See Design Patterns: Elements of Reusable Object-Oriented Software pg25.

Matching interfaces to implementations together with polymorphism Functors achieve there breadth and magic. Either you are overthinking this, or you are confused. This is an undergrad exercise in Programming Essentials 101.

I agree there is not any code that needs to be provided here. Please check each of the links I have provided. They will help decide how you need to tackle the problem, no matter the language you are using.

2
  • Hi @Joseph, this answer could use some improvement by fixing the formatting. You are likely just missing the double spaces at the end of the line. Also some grammar issues. I'll try to fix it up for you.
    – Bob
    Dec 24, 2019 at 14:31
  • Now that I formatted it, you might want to also leave comments on the main question for clarification before answering, since you seem to have questions within your answer.
    – Bob
    Dec 24, 2019 at 14:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.